-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingLinkedLists.cpp
More file actions
103 lines (95 loc) · 1.67 KB
/
StackUsingLinkedLists.cpp
File metadata and controls
103 lines (95 loc) · 1.67 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
//Stacks using linked lists//
#include <iostream>
using namespace std;
struct stack
{
int data;
stack *link;
} * top;
void push()
{
int n;
stack *node;
node = new stack;
if (node == NULL)
{
cout << "no space avail/n";
}
cout << "enter data\n";
cin >> n;
node->data = n;
node->link = NULL;
if (top == NULL)
top = node;
else
{
node->link = top;
top = node;
}
}
int pop()
{
stack *temp;
int value;
if (top == NULL)
{
cout << "Stack Underflow";
return 0;
}
else
{
temp = top;
value = temp->data;
top = top->link;
delete temp;
return value;
}
}
void display()
{
stack *temp;
temp = top;
if (temp == NULL)
cout << "Stack Underflow";
else
{
cout << "\nElements in stack are: \n";
while (temp != NULL)
{
cout << temp->data << endl;
temp = temp->link;
}
}
}
int main()
{
top = NULL;
int ch, value;
while (1)
{
cout << "\n-------------\n";
cout << "\nPress 1 for Push\n";
cout << "Press 2 for Pop\n";
cout << "Press 3 for Display\n";
cout << "Press any key to exit\n";
cout << "\nenter choice: \n";
cin >> ch;
switch (ch)
{
case 1:
push();
break;
case 2:
value = pop();
if (value != 0)
cout << "\nPopped element is " << value;
break;
case 3:
display();
break;
default:
exit(0);
}
}
return 0;
}