-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack using linked list.cpp
94 lines (88 loc) · 1.81 KB
/
stack using linked list.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
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *top=0;
void push(int val)
{
struct node *new_node;
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=val;
new_node->next=top;
top=new_node;
}
void peek()
{
if (top==0)
cout<<"List is empty."<<endl;
else
cout<<"Peeked value: "<<top->data<<endl;
}
void pop()
{
struct node *temp;
temp=top;
if (top==0)
cout<<"List is empty."<<endl;
else
{
cout<<"Popped item: "<<temp->data;
top=temp->next;
free(temp);
}
}
void display()
{
struct node *temp;
temp=top;
if (top==0)
cout<<"List is empty."<<endl;
else
{
while (temp->next!=0)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<temp->data<<endl;
}
}
int main ()
{
int choice,value;
do{
cout<<"1 for push"<<endl;
cout<<"2 for pop"<<endl;
cout<<"3 for peek"<<endl;
cout<<"4 for display"<<endl;
cout<<"0 to exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the data: ";
cin>>value;
push(value);
break;
case 2:
pop();
cout<<endl;
break;
case 3:
peek();
cout<<endl;
break;
case 4:
display();
cout<<endl;
break;
case 0:
return 0;
}
}while (choice<5);
return 0;
}