-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack- Nstacks.cpp
More file actions
72 lines (55 loc) · 1.15 KB
/
Stack- Nstacks.cpp
File metadata and controls
72 lines (55 loc) · 1.15 KB
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
#include<iostream>
#include<stack>
using namespace std;
class Nstack{
private:
int *arr;
int *top;
int *next;
int freespot;
int n, s;
public:
Nstack(int N, int S){
n= N;
s= S;
arr= new int[s];
top= new int[n];
next= new int[s];
//top initialise
for(int i=0; i<n; i++){
top[i]= -1;
}
//next initialise
for(int i=0; i<s; i++){
next[i]= i+1;
}
next[s-1]= -1;
//freespot
freespot= 0;
}
bool push(int x, int m){
if(freespot==-1)
return false;
//find index
int index= freespot;
//update freespot
freespot= next[index];
//insert in array
arr[index]= x;
//update next
next[index]= top[m-1];
//update top
top[m-1]= index;
return true;
}
void printArray(){
for(int i=0; i<s; i++){
cout<<arr[i]<<" ";
}
}
};
int main(){
Nstack A(3, 6);
A.push(2,1);
A.printArray();
}