-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec58_stackqueshard.cpp
56 lines (48 loc) · 966 Bytes
/
lec58_stackqueshard.cpp
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
class NStack
{
public:
int *arr;
int *next;
int *top;
int n, s;
int freespot;
NStack(int N, int S)
{
n = N;
s = S;
arr = new int[S];
top = new int[N];
next = new int[S];
for (int i = 0; i < n; i++)
{
top[i] = -1;
}
for (int i = 0; i < S; i++)
{
next[i] = i + 1;
}
next[S - 1] = -1;
freespot = 0;
}
bool push(int x, int m)
{
if (freespot == -1)
return false;
int index = freespot;
freespot = next[index];
arr[index] = x;
next[index] = top[m - 1];
top[m - 1] = index;
return true;
}
int pop(int m)
{
if (top[m - 1] == -1)
return -1;
int index = top[m - 1];
top[m - 1] = next[index];
next[index] = freespot;
freespot = index;
return arr[index];
}
};