-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathStackLinkedList.cpp
107 lines (96 loc) · 1.9 KB
/
StackLinkedList.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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
class Stack{
private:
Node *top;
int size;
int maxSize;
public:
Stack(){
top=NULL;
maxSize=10;
size=0;
}
Stack(int size){
top=NULL;
maxSize=size;
this->size=0;
}
bool isEmpty(){
if(top==NULL && size==0){
return 1;
}
return 0;
}
bool isFull(){
if(size==maxSize){
return 1;
}
return 0;
}
void push( int element){
if(isFull()){
cout<<"____STACK OVERFLOW____"<<endl;
}
else{
size++;
Node *t=new Node;
t->data=element;
t->next=top;
top=t;
}
}
int pop(){
if(isEmpty()){
cout<<"____STACK UNDERFLOW___"<<endl;
}
else{
int prevData;
size--;
Node *t=top;
top=top->next;
prevData=t->data;
delete t;
return prevData;
}
}
int peek(){
if(isEmpty()){
cout<<" STACK IS EMPTY"<<endl;
return -9999999;
}
else{
return top->data;
}
}
void Display(){
if(isEmpty()){
cout<<"NO ELEMENT IN STACK"<<endl;
}
else{
Node *p=top;
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
}
};
int main(){
Stack st;
st.push(10);
st.push(20);
st.pop();
st.pop();
st.push(500);
st.push(100);
st.pop();
st.Display();
return 0;
}