-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_stacks.cpp
109 lines (99 loc) · 2.28 KB
/
n_stacks.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
#include<iostream>
using namespace std;
//pushing into the stack function -->
void push(int item,int index,int *top,int *arr,int size){
if(top[index]<0){
if(top[index]==-1){
top[index]=0;
arr[top[index]]=item;
}
else{
top[index]=-top[index];
arr[top[index]]=item;
}
}
else if(index%2==0 && top[index]>=0){
if(top[index]==top[index+1]-1 || (top[index]==size-1))
cout<<"\noverflow condition.";
else{
top[index]+=1;
arr[top[index]]=item;
}
}
else if(index%2!=0 && top[index]>0){
if(top[index]==top[index-1]+1)
cout<<"\noverflow condition.";
else{
top[index]-=1;
arr[top[index]]=item;
}
}
else
cout<<"invalid";
cout<<"\narray is :\n";
for(int j=0;j<size;j++){
cout<<arr[j]<<"\t";
}
}
//function for getting tops of specified no. of stacks-->
void divide(int no_stacks,int size,int *top){
int k=1;
for(int i=1;i<no_stacks;i++){
if(i%2!=0){
top[i]=(k)*(2*(size/no_stacks))-1;
k++;
}
else
top[i]=top[i-1]+1;
}
cout<<top[0]<<"\t";
for(int j=1;j<no_stacks;j++){
top[j]=-(top[j]);
cout<<top[j]<<"\t";
}
}
//our main function-->
int main(){
int no_stacks ,size,ch,stkno,item;
label:
cout<<"\nenter the no of stacks you want:";
cin>>no_stacks;
cout<<"\nenter the size of the array you want to fit in:";
cin>>size;
if(no_stacks>size || no_stacks<=0 || size<=0){
cout<<"\ninvalid entry enter again:";
goto label;
}
int arr[size]={0};
int top[no_stacks]={-1};
divide(no_stacks,size,top);
cout<<"\nwhat do with the stacks you entered:"
<<"\n1.push element to them."
<<"\n2.pop from them."
<<"\n3.display the stack."
<<"\nenter your choice:";
cin>>ch;
char ch2;
//stkno = 1+index_of_top_array
do{
cout<<"\nenter the stack on which you want to operate:";
cin>>stkno;
switch(ch){
case 1 : cout<<"\nenter the item:";
cin>>item;
push(item,stkno-1,top,arr,size);
cout<<"\nitem added.";
break;
//case 2 : pop(stkno-1,arr);
// cout<<"\ndeletion succesfull";
// break;
//case 3 : display(stkno,arr);
// break;
default: cout<<"invalid entry of choice";
break;
}
cout<<"\nwant to add more:";
cin>>ch2;
}while(ch2=='y' || ch2=='Y');
return 0;
}