-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplementation of Stack Using Linked List(push,pop,display)).cpp
105 lines (88 loc) · 1.55 KB
/
Implementation of Stack Using Linked List(push,pop,display)).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
#include <iostream>
using namespace std;
//Structure of the Node
struct Node
{
int data;
Node *next;
};
// top pointer to keep track of the top of the stack
Node *top = NULL;
//Function to insert an element in stack
void push (int value)
{
Node *temp = new Node();
temp->data = value;
temp->next = top;
top = temp;
}
//Function to check if stack is empty or not
bool EmptyStack()
{
if(top == NULL)
return true;
else
return false;
}
// Function to show the element at the top of the stack
void tip()
{
if ( EmptyStack() )
cout<<"Stack is Empty";
else
cout<<"Element at top is : "<< top->data;
}
//Function to delete an element from the stack
void pop ( )
{
if ( EmptyStack() )
cout<<"Stack is Empty";
else
{
Node *temp = top;
top = top -> next;
delete(temp);
}
}
// Function to Display the stack
void DisplayStack()
{
if ( EmptyStack() )
cout<<"Stack is Empty";
else
{
Node *temp=top;
while(temp!=NULL)
{ cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"\n";
}
}
// Main function
int main()
{
int input, flag=1, value;
//Menu Driven Program using Switch
while( flag == 1)
{
cout<<"\n 1.Push 2.Pop 3.tip 4.DisplayStack 5.exit\n";
cin>>input;
switch (input)
{
case 1: cout<<"Enter Value:\n";
cin>>value;
push(value);
break;
case 2: pop();
break;
case 3: tip();
break;
case 4: DisplayStack();
break;
case 5: flag = 0;
break;
}
}
return 0;
}