-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack1.cpp
79 lines (75 loc) · 1.44 KB
/
stack1.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
#include<iostream>
#include "array structure.cpp"
using namespace std;
class stack:private array {
public:
stack(int );
stack(stack &);
void push(int );
int peek();
void pop();
~stack();
virtual bool isFull();
virtual bool isEmpty();
int getStackSize();
void operator=(stack &);
};
bool stack::isEmpty(){
return array::isEmpty();
}
void stack::operator=(stack &s){
array::operator=(s); //calling array class opertaor
}
int stack::getStackSize(){
return getcapacity();
}
stack::stack(stack &s):array(s){
}
void reverseStack(stack &s1){
stack s2(s1.getStackSize());
while(!s1.isEmpty()){
s2.push(s1.peek());
s1.pop();
}
s1=s2; //operator overloading
}
bool stack::isFull(){
return array::isFull();
}
stack::~stack(){}
stack::stack(int cap):array(cap){}
void stack::push(int data){
if(isFull())
cout<<"stack is overflow";
else
appendElement(data);
}
int stack::peek(){
if(!isEmpty())
return getElementbyIndex(countElement()-1);
else{
cout<<"array is empty";
}
return 0;
}
void stack::pop(){
if(isEmpty()){
cout<<"stack is empty";
}
else{
delElement(countElement()-1);
}
}
int main(){
stack s(5);
s.push(20);
s.push(20);
s.push(30);
s.push(40);
s.push(70);
cout<<endl<<s.peek();
reverseStack(s);
cout<<endl<<s.peek();
cout<<endl;
return 0;
}