-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_ll.cpp
86 lines (86 loc) · 1.24 KB
/
stack_ll.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
#include <iostream>
#define endl '\n'
using namespace std;
struct stack
{
int data;
stack* next;
};
void push(stack** top)
{
stack* s=new stack;
if(!s)
{
cout<<"Memory allocation error\n";
return;
}
int data;
cout<<"Enter data\n";
cin>>data;
s->data=data;
s->next=*top;
*top=s;
}
void pop(stack** top)
{
if(*top==NULL)
{
cout<<"Stack underflow\n";
return;
}
stack* temp=new stack;
temp=*top;
*top=temp->next;
cout<<"Popped "<<temp->data<<endl;
delete temp;
}
void printStack(stack** top)
{
if(*top==NULL)
{
cout<<"Stack empty\n";
return;
}
stack* temp=new stack;
temp=*top;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
delete temp;
}
void deleteStack(stack** top)
{
stack *p=new stack;
p=*top;
while(p->next)
{
stack *temp=new stack;
temp=p->next;
p->next=temp->next;
delete temp;
}
delete p;
}
int main()
{
int ch;
stack* top=NULL;
do
{
cout<<"1. Push\n2. Pop\n3. Print\n4. Exit\n";
cin>>ch;
switch (ch)
{
case 1: push(&top);
break;
case 2: pop(&top);
break;
case 3: printStack(&top);
break;
}
}while(ch!=4);
deleteStack(&top);
return 0;
}