-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStackUsingLinkListImplementation.cpp
147 lines (123 loc) · 2.19 KB
/
StackUsingLinkListImplementation.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
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
/*https://practice.geeksforgeeks.org/problems/implement-stack-using-linked-list/1*/
#include<iostream>
using namespace std;
// #define MAX 1000
template<typename T>
class sNode
{
public:
T data;
sNode<T>* next;
sNode(int item)
{
data = item;
}
};
template<typename T>
class MYStack{
sNode<T>* top;
public:
MYStack()
{
top = nullptr;
}
T pop();
T peek();
bool isEmpty();
void push(T);
int size(){ return top+1;}
void Display_St_Lnk()
{
sNode<T> *np = top;
while(np!=nullptr)
{
cout<<endl;
cout<<" "<<np->data<<" -> ";
np=np->next;
}
cout<<"!!!\n";
}
};
template<typename T>
bool MYStack<T>::isEmpty()
{
return (top == nullptr);
}
template<typename T>
T MYStack<T>::peek()
{
if(top == nullptr)
{
cout<<"\nPEEK: Stack Empty";
exit(1);
}
return top->data;
}
template<typename T>
T MYStack<T>::pop()
{
if(top == nullptr)
{
cout<<"\nPOP: Underflow Condition.";
exit(1);
// return -top;
}
sNode<T> *ptr = top;
T item = top->data;
top = top->next;
delete ptr;
cout<<"\nPOP: Item:"<<item<<" popped from Stack";
return item;
}
template<typename T>
void MYStack<T>::push(T item)
{
// if capacity full then increase capacity
sNode<T> *newptr = new sNode<T>(item);
if(newptr==nullptr)
{
cout<<"\n Cannot Create New Node !!!! Aborting \n"; system("pause"); exit(1);
}
newptr->data = item;
newptr->next = nullptr;
newptr->next = top;
top = newptr;
cout<<"\nPUSH: Item:"<<item<<" pushed into Stack";
}
int main()
{
MYStack<int> *st = new MYStack<int>();
int ITEM ;
char choice;
do
{
char ch;
system("pause") ;
system("cls");
cout<<"\n Stack Menu LinkedList";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Quit";
cout<<"\n\n Enter choice : ";
cin>>ch;
switch(ch)
{
case '1' : cout<<"\nEnter ITEM for insertion : ";
cin>>ITEM;
st->push(ITEM);
st->Display_St_Lnk();
break;
case '2' : st->Display_St_Lnk(); st->pop(); st->Display_St_Lnk();
break;
case '3' : st->Display_St_Lnk();
break;
default: cout<<"\n BYE BYE!";
break;
}
choice=ch;
}while(choice!='4');
cin.get();
cin.get();
return 0;
}