-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks-linkedlist.cpp
More file actions
97 lines (90 loc) · 2.18 KB
/
Stacks-linkedlist.cpp
File metadata and controls
97 lines (90 loc) · 2.18 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
#include<iostream>
using namespace std;
template<class temp>
class Node
{
public :
temp data;
Node<temp> *next;
};
template<class temp>
class Stack
{
public :
Node<temp> *start, *node;
Stack()
{
start = new Node<temp>();
node = new Node<temp>();
start->next = NULL;
}
void push()
{
temp ele;
cout<<"Enter the element to be inserted : ";
cin>>ele;
node = new Node<temp>();
node->data = ele;
node->next = start;
start = node;
}
void pop()
{
if(start == NULL)
{
cout<<"The Stack is empty."<<endl;
}
else
{
temp ele;
ele = start->data;
start = start->next;
cout<<"The taken element is : "<<ele<<endl;
}
}
void traverse()
{
node = new Node<temp>();
node = start;
cout<<"The traversal of the stack is : ";
while(node->next!=NULL)
{
cout<<node->data<<"\t";
node = node->next;
}
cout<<endl;
}
};
int main()
{
cout<<"-----------Welcome to Stacks----------"<<endl;
bool flag = true;
int cho;
Stack<int> st;
do
{
cout<<"Options avalible are : \n1)Push an element into stack.\n2)Pop an element from a stack.\n3)Print all the elements in the stack.\n4)Exit."<<endl;
cout<<"Enter your choice : ";
cin>>cho;
switch (cho)
{
case 1 :
st.push();
break;
case 2 :
st.pop();
break;
case 3 :
st.traverse();
break;
case 4 :
flag = false;
break;
default:
cout<<"Enter a valid choice."<<endl;
break;
}
cout<<"-------------------------------"<<endl;
}while(flag);
cout<<"-----------End of Program----------"<<endl;
}