-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nstacks.c++
82 lines (58 loc) · 1.06 KB
/
Nstacks.c++
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
#include<iostream>
using namespace std;
class NStacks {
int n;
int s;
int *arr;
int *next;
int *top;
int freeSpot;
public:
NStacks(int N, int S)
{
n = N;
s = S;
arr = new int[s];
next = new int[s];
top = new int[n];
freeSpot = 0;
for (int i = 0; i < s; i++)
{
next[i] = i+1;
}
next[s-1] = -1;
for (int i = 0; i < n; i++)
{
top[i] = -1;
}
}
bool push(int X, int m)
{
if(freeSpot == -1)
{
return false;
}
int index = freeSpot;
arr[index] = X;
freeSpot = next[index];
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];
}
};
int main()
{
return 0;
}