-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52-stack_linklist.cpp
More file actions
98 lines (92 loc) · 1.61 KB
/
52-stack_linklist.cpp
File metadata and controls
98 lines (92 loc) · 1.61 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
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
int info;
node *link;
};
class list
{
public:
node *first;
int item;
list()
{
first=NULL;
}
void push()//push is nothing but insert rear for stack
{
node *temp=new node;
node *cur=new node;
temp->info=item;
temp->link=NULL;
if(first==NULL)
{
first=temp;
delete temp;
return;
}
cur=first;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
}
void pop()//pop is nothing but delete rear for stack
{
node *prev=new node;
node *cur=new node;
if(first==NULL)
{
cout<<"empty list\n";
return;
}
cur=first;
prev=NULL;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
prev->link=NULL;
delete cur;
}
void display()
{
node *temp=new node;
temp=first;
if(temp==NULL)
cout<<"Empty list";
while(temp!=NULL)
{
cout<<temp->info<<endl;
temp=temp->link;
}
}
};
int main()
{
int choice;
list ob;
for(;;)
{
cout<<"1:push\n2:pop\n3:display \n4:exit"<<endl;
cout<<"Enter choice";
cin>>choice;
switch(choice)
{
case 1: cout<"enter item to be inserted";
cin>>ob.item;
ob.push();
break;
case 2: ob.pop();
break;
case 3:ob.display();
break;
default:exit(0);
}
}
return 0;
}