You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int *next; // To store next entry in all stacks (the index of below the top element in the main array of stack m - useful for pop operation) and free list
9
+
int n; // Number of stacks
10
+
int size; // Size of main array
11
+
int freeSpot; // Index of free spot in main array
12
+
public:
13
+
// constructor
14
+
NStack(int N, int S) : n(N), size(S)
15
+
{
16
+
freeSpot = 0; // Initially all spaces are free
17
+
arr = newint[size]; // Main array of size S
18
+
top = newint[n]; // Stores top element of each stack in the main array thus same size as number of stacks
19
+
next = newint[size]; // Stores next entry in all stacks and free list thus same size as main array
20
+
21
+
// Initialize all stacks as empty
22
+
for (int i = 0; i < n; i++)
23
+
{
24
+
top[i] = -1;
25
+
}
26
+
27
+
// Initialize all spaces as free
28
+
for (int i = 0; i < size - 1; i++)
29
+
{
30
+
next[i] = i + 1;
31
+
}
32
+
next[size - 1] = -1; // -1 is used to indicate end of free list, setting last element of next[] as -1 as it itself is the last free spot
33
+
}
34
+
35
+
// destructor
36
+
~NStack()
37
+
{
38
+
delete[] arr;
39
+
delete[] top;
40
+
delete[] next;
41
+
}
42
+
43
+
// Pushes 'X' into the Mth stack. Returns true if it gets pushed into the stack, and false otherwise.
44
+
// tc: O(1) sc: O(1)
45
+
boolpush(int x, int m)
46
+
{
47
+
if (freeSpot == -1)
48
+
{
49
+
cout << "Stack overflow";
50
+
cout << ". Can't push " << x << " into stack " << m << endl;
51
+
returnfalse; // No free spot left in main array thus stack overflow
52
+
}
53
+
54
+
int i = freeSpot; // 1. Store index of free spot
55
+
freeSpot = next[i]; // 2. Update index of free spot to next free spot
56
+
arr[i] = x; // 3. Put data in free spot
57
+
next[i] = top[m - 1]; // 4. Update next of free spot to index of top element of stack m, m-1 because m is 1-indexed
58
+
top[m - 1] = i; // 5. Update top of stack m to index of free spot, m-1 because m is 1-indexed
59
+
cout << "Pushed " << x << " into stack " << m << endl;
60
+
returntrue; // 6. Return true as data is pushed into stack
61
+
}
62
+
63
+
// Pops top element from Mth Stack. Returns -1 if the stack is empty, otherwise returns the popped element.
64
+
// tc: O(1) sc: O(1)
65
+
intpop(int m)
66
+
{
67
+
if (top[m - 1] == -1)
68
+
{
69
+
cout << "Stack underflow";
70
+
cout << ". Can't pop from stack " << m << endl;
71
+
return -1; // Stack is empty thus stack underflow
72
+
}
73
+
74
+
// reverse process of push
75
+
int i = top[m - 1]; // 1. Store index of top element of stack m
76
+
top[m - 1] = next[i]; // 2. Update top of stack m to index of next element of top element of stack m (the element below the top element in the main array of stack m)
77
+
int popped = arr[i]; // 3. Store popped element
78
+
next[i] = freeSpot; // 4. Update next of popped element to index of free spot
79
+
freeSpot = i; // 5. Update index of free spot to index of popped element
80
+
cout << "Popped " << popped << " from stack " << m << endl;
81
+
return popped; // 6. Return popped element
82
+
}
83
+
};
84
+
85
+
intmain()
86
+
{
87
+
int n = 3, size = 6;
88
+
cout << "Number of stacks: " << n << endl;
89
+
cout << "Size of main array: " << size << endl << endl;
0 commit comments