-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_4.go
173 lines (133 loc) · 3.31 KB
/
task_4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// To execute Go code, please declare a func main() in a package "main"
package main
import (
"fmt"
"sync"
)
/*
In Golang, there are array and slice
if the array with given size [10]int
```
var a [10]int
fmt.Printf("len=%d cap=%d %v\n", len(a), cap(a), a)
output: len=10 cap=10 [0 0 0 0 0 0 0 0 0 0]
```
So i am using slice here. Slices can be created with the built-in make function.
Slice is dynamically-sized arrays.
make takes three arguments. make(type, len, capacity).
```
var a = make([]int, 10) // length and capacity 10
fmt.Printf("len=%d cap=%d %v\n", len(a), cap(a), a)
output: len=10 cap=10 [0 0 0 0 0 0 0 0 0 0]
```
```
var a = make([]int, 0, 10) // length is 0 and capacity is 10
fmt.Printf("len=%d cap=%d %v\n", len(a), cap(a), a)
output: len=0 cap=10 []
```
Since i am initializing slice of len(0), the memory of of the each stack
will remain 24bytes. This will grow only the stack grows.
*/
func main() {
s := NewStackWithTwo(10000000)
s.pushFirst(1)
s.pushFirst(2)
s.pushSecond(3)
fmt.Println(s.popFirst())
fmt.Println(s.popSecond())
fmt.Println(s.popFirst())
fmt.Println(s.popFirst())
}
// StackWithTwo interface
type StackWithTwo interface {
pushFirst(i int)
popFirst() (last int)
pushSecond(i int)
popSecond() (last int)
}
// Stack struct holds the stack properties
type Stack struct {
stacks map[int][]int
wg sync.RWMutex
}
// NewStackWithTwo returns StackWithTwo interface
func NewStackWithTwo(capacity int) StackWithTwo {
var s Stack
s.stacks = make(map[int][]int)
s.stacks[0] = make([]int, 0, capacity)
s.stacks[1] = make([]int, 0, capacity)
// fmt.Println("Size of s.stacks[0]:", unsafe.Sizeof(s.stacks[0]))
// fmt.Printf("len=%d cap=%d %v\n",
// len(s.stacks[0]), cap(s.stacks[0]), s.stacks[0])
// fmt.Printf("len=%d cap=%d %v\n",
// len(s.stacks[1]), cap(s.stacks[1]), s.stacks[1])
return &s
}
func (s *Stack) pushFirst(i int) {
s.wg.Lock()
s.stacks[0] = append(s.stacks[0], i)
s.wg.Unlock()
// fmt.Printf("len=%d cap=%d %v\n",
// len(s.stacks[0]), cap(s.stacks[0]), s.stacks[0])
// fmt.Println("Size of s.stacks[0]:", unsafe.Sizeof(s.stacks[0]))
}
func (s *Stack) popFirst() (last int) {
s.wg.RLock()
v := s.stacks[0]
s.wg.RUnlock()
if len(v) == 0 {
return
}
last = v[len(v)-1]
s.wg.Lock()
s.stacks[0] = v[:len(v)-1]
s.wg.Unlock()
// fmt.Printf("len=%d cap=%d %v\n",
// len(s.stacks[0]), cap(s.stacks[0]), s.stacks[0])
return
}
func (s *Stack) pushSecond(i int) {
s.wg.Lock()
s.stacks[1] = append(s.stacks[1], i)
s.wg.Unlock()
// fmt.Printf("len=%d cap=%d %v\n",
// len(s.stacks[1]), cap(s.stacks[1]), s.stacks[1])
}
func (s *Stack) popSecond() (last int) {
s.wg.RLock()
v := s.stacks[1]
s.wg.RUnlock()
if len(v) == 0 {
return
}
last = v[len(v)-1]
s.wg.Lock()
s.stacks[1] = v[:len(v)-1]
s.wg.Unlock()
// fmt.Printf("len=%d cap=%d %v\n",
// len(s.stacks[0]), cap(s.stacks[0]), s.stacks[0])
return
}
/*
Your previous Plain Text content is preserved below:
class StackWithTwo(int CAPACITY) {
int [] arr;
void pushFirst(int arg) {
}
int popFirst() {
}
void pushSecond(int arg) {
}
int popSecond() {
}
}
main() {
StackWithTwo s = new StackWithTwo(10);
s.pushFirst(1); []1
s.pushFirst(2); []1,2
s.pushSecond(3); []3
s.popFirst(); //2 []1
s.popSecond(); //3 []
s.popFirst(); //1
}
*/