-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlink_list_stack.cpp
More file actions
99 lines (99 loc) · 1.76 KB
/
link_list_stack.cpp
File metadata and controls
99 lines (99 loc) · 1.76 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
#include<iostream>
using namespace std;
struct node
{
int data;
node* link;
};
typedef node* NODE;
NODE get_node()
{
NODE x;
x=new node;
if(x==NULL)
cout<<"Memory Full";
return x;
}
NODE insert_front(NODE &root, int item,int &n)
{
NODE temp;
temp=get_node();
temp->data=item;
temp->link=root;
n++;
return temp;
}
NODE delete_front(NODE &root,int &n)
{
NODE temp;
if(root==NULL)
{
cout<<"Stack Empty";
return root;
}
else if(root->link==NULL)
{
cout<<"Element Removed : "<<root->data;
delete root;
root=NULL;
n--;
return root;
}
temp=root;
cout<<"Element Removed : "<<temp->data;
delete temp;
root=root->link;
n--;
return root;
}
void display(NODE root,int &n)
{
NODE temp=root;
if(root==NULL)
{
cout<<"\nStack Empty";
return;
}
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->link;
}
cout<<"\nTotal:"<<n;
}
int main()
{
NODE root=NULL;
int ch,n=1,x,nodes=0;
cout<<"Enter choice";
while(n==1)
{
cout<<"\n1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to enter : ";
cin>>x;
root=insert_front(root,x,nodes);
break;
case 2:
root=delete_front(root,nodes);
break;
case 3:
cout<<"\nStack: ";
display(root,nodes);
break;
case 4:
n=0;
break;
default:
cout<<"\nWrong Input.";
break;
}
}
return 0;
}