-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_Stack_array_withInheritence.cpp
More file actions
206 lines (168 loc) · 3.7 KB
/
a_Stack_array_withInheritence.cpp
File metadata and controls
206 lines (168 loc) · 3.7 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class Array{
private:
int capacity;
int lastindex;
int *ptr;
public:
Array();
void createArray(int cap);
void append(int data);
void insert(int index, int data);
void del(int index );
int search(int data);
int edit(int index,int data);
int count();
int getData(int index);
void view();
~Array();
};
Array :: Array(){ // constructor
capacity=0;
lastindex=-1;
ptr=NULL;
}
void Array:: createArray(int cap){
capacity=cap;
lastindex=-1;
ptr =new int[cap]; // ptr pointing to new array formed of size 'cap'
}
void Array:: append(int data){
if(lastindex+1==capacity){
cout<<"\n overflow";
}
else {
++lastindex;
ptr[lastindex]=data;
}
}
void Array::insert(int index, int data){
int i;
if(index<0 || index>(lastindex+1)){
cout<<"\n invalid index";
}
else if(lastindex+1==capacity){
cout<<"\n overflow";
}
else{
for(i=lastindex;i>=index;i--){
ptr[i+1]=ptr[i];
}
ptr[index]=data;
lastindex++;
}
}
void Array:: del(int index)
{
if(index<0 || index>lastindex){
cout<<"\n invalid index";
}
else{
for(int i=index;i<=lastindex;i++){
ptr[i]=ptr[i+1];
lastindex--;
}
}
}
int Array:: search(int data){
for(int i=0;i<=lastindex;i++){
if(ptr[i]==data){
return i;
}
else {
cout<<"not found ";
}
}
}
int Array:: getData(int index){
if(index<0 || index>lastindex+1){
cout<<"invalid index ";
}
else{
return ptr[index];
}
}
int Array:: count(){
return (lastindex+1);
}
int Array::edit(int index,int newdata){
if(index<=0 || index>=lastindex+1){
cout<<"\n invalid index";
}
else if(lastindex+1==capacity){
cout<<"\n overflow";
}
else{
ptr[index]=newdata;
}
}
void Array:: view(){
for(int i=0;i>=lastindex;i++){
cout<<ptr[i]<<" ";
}
}
Array::~Array(){ // detsructor
delete []ptr;
}
class Stack:private Array{
public:
void createStack(int capacity){
createArray(capacity);
}
void push(int data){
append(data);
}
void pop(){
del(count()-1);
}
int peek(){
return getData(count()-1);
}
};
int driver(){
int choice;
cout<<"\n Operations : ";
cout<<"\n 1) Create Stack";
cout<<"\n 2) Push data";
cout<<"\n 3) Pop data";
cout<<"\n 4)To see top element in Stack";
cout<<"\n Press ZERO To exit";
cout<<"\n Enter your Choice : ";
cin>>choice;
return choice;
}
int main(){
int data,capacity;
Stack obj;
while(1){
switch (driver())
{
case 1:
cout<<"\n Enter Capacity for Stack :";
cin>>capacity;
obj.createStack(capacity);
break;
case 2:
cout<<"\n Enter data to push : ";
cin>>data;
obj.push(data);
break;
case 3:
obj.pop();
break;
case 4:
cout<<obj.peek();
break;
case 0:
exit(0);
break;
default:
cout<<"\n Please enter valid choice , Thank you!";
break;
}
}
return 0;
}