-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnstacks2.cpp
111 lines (92 loc) · 1.97 KB
/
nstacks2.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
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
//error code..............
#include<iostream>
using namespace std;
//basically implementing queues here in the name of stacks..........
//next is front....
//top is rear.....
class nstacks{
int* arr;
int* next;
int* top;
int stkno,arrsize,free; //to store the begining index of free list.......
public:
nstacks(int stkno,int arrsize){
this->stkno=stkno;
this->arrsize=arrsize;
arr = new int[arrsize];
top = new int[stkno];
next = new int[arrsize];
int i;
for(i=0;i<stkno;i++)
top[i]=-1;
free = 0;
for(i=0;i<arrsize-1;i++)
next[i]=i+1;
next[arrsize-1]=-1;
}
bool isfull(){
return (free == -1);
}
void push(int skno,int item){
if(isfull()){
cout<<"\noverflow.....";
return ;
}
int index;
index=free;
free=next[index];
next[index]=top[skno];
top[skno]=index;
arr[index]=item;
cout<<"\nelement inserted at :"<<index<<"\nin skno:"<<skno<<"\nat position in stack(top[skno]):"<<top[skno];
}
bool isempty(int skno){
return(top[skno]==-1);
}
int pop(int skno){
if(isempty(skno)){
cout<<"\n underflow>>>>......";
return 0;
}
int index;
index=top[skno];
top[skno]=next[index];
next[index]=free;
free=index;
return arr[index];
}
void display(){
for( int i=0;i<arrsize;i++)
cout<<arr[i];
}
};
int main(){
int nostk,maxsize,k;
cout<<"\nenter the no.of stacks:";
cin>>nostk;
cout<<"\nenter no of total elements:";
cin>>maxsize;
nstacks n1(2,4);
n1.push(1,3);
n1.push(2,6);
n1.push(1,7);
n1.push(2,8);
n1.push(2,9);
cout<<"\narray is:\n";
n1.display();
cout<<"\nin 1:";
k=n1.pop(1);
cout<<"\nk is:"<<k;
k=n1.pop(1);
cout<<"\nk is:"<<k;
k=n1.pop(1);
cout<<"\nk is:"<<k;
cout<<"\nin 2:";
k=n1.pop(2);
cout<<"\nk is:"<<k;
k=n1.pop(2);
cout<<"\nk is:"<<k;
k=n1.pop(2);
cout<<"\nk is:"<<k;
return 0;
}