Skip to content

Commit 4dfbdea

Browse files
Create stackusinglinkedlist.cpp
1 parent 1852a21 commit 4dfbdea

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

stackusinglinkedlist.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include<iostream>
2+
#include<process.h>
3+
4+
using namespace std;
5+
6+
struct Node
7+
{
8+
int data;
9+
Node *next;
10+
}*top=NULL,*p;
11+
12+
Node* newnode(int x)
13+
{
14+
p=new Node;
15+
p->data=x;
16+
p->next=NULL;
17+
return(p);
18+
}
19+
20+
void push(Node *q)
21+
{
22+
if(top==NULL)
23+
top=q;
24+
else
25+
{
26+
q->next=top;
27+
top=q;
28+
}
29+
}
30+
31+
void pop(){
32+
if(top==NULL){
33+
cout<<"Stack is empty!!";
34+
}
35+
else{
36+
cout<<"Deleted element is "<<top->data;
37+
p=top;
38+
top=top->next;
39+
delete(p);
40+
}
41+
}
42+
43+
void showstack()
44+
{
45+
Node *q;
46+
q=top;
47+
48+
if(top==NULL){
49+
cout<<"Stack is empty!!";
50+
}
51+
else{
52+
while(q!=NULL)
53+
{
54+
cout<<q->data<<" ";
55+
q=q->next;
56+
}
57+
}
58+
}
59+
60+
int main()
61+
{
62+
int ch,x;
63+
Node *nptr;
64+
while(1)
65+
{
66+
cout<<"\n\n1.Push\n2.Pop\n3.Display\n4.Exit";
67+
cout<<"\nEnter your choice(1-4):";
68+
cin>>ch;
69+
switch(ch){
70+
case 1: cout<<"\nEnter data:";
71+
cin>>x;
72+
nptr=newnode(x);
73+
push(nptr);
74+
break;
75+
case 2: pop();
76+
break;
77+
case 3: showstack();
78+
break;
79+
case 4: exit(0);
80+
default: cout<<"\nWrong choice!!";
81+
}
82+
}
83+
return 0;
84+
}

0 commit comments

Comments
 (0)