Skip to content

Commit e572318

Browse files
Add files via upload
1 parent a11202b commit e572318

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

stack.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<malloc.h>
4+
#include<stdio.h>
5+
#include<malloc.h>
6+
struct node
7+
{
8+
int data;
9+
struct node *next;
10+
};
11+
struct node *top=NULL;
12+
struct node *push(struct node *);
13+
struct node *display(struct node *);
14+
struct node *pop(struct node *);
15+
int main()
16+
{
17+
int option;
18+
do
19+
{
20+
printf("\n MAIN MENU");
21+
printf("\n 1.push\n2.pop\n3.display");
22+
printf("\n Enter your option:");
23+
scanf("%d",&option);
24+
switch(option)
25+
{
26+
case 1:top=push(top);break;
27+
case 2:top=pop(top);break;
28+
case 3:top=display(top);break;
29+
case 4:exit(0);break;
30+
default:printf("\n enter the correct option");
31+
}
32+
}while(option!=4);
33+
}
34+
35+
36+
struct node *push(struct node *top)
37+
{
38+
struct node *ptr,*new_node;
39+
int num;
40+
printf("\n press -1 to end");
41+
printf("\n enter the value of data:");
42+
scanf("%d",&num);
43+
while(num!=-1)
44+
{
45+
new_node=(struct node *)malloc(sizeof(struct node));
46+
new_node->data=num;
47+
if(top==NULL)
48+
{
49+
new_node->next=NULL;
50+
top=new_node;
51+
}
52+
else
53+
{
54+
new_node->next=top;
55+
top=new_node;
56+
57+
}
58+
printf("\n enter the data:");
59+
scanf("%d",&num);
60+
}
61+
return top;
62+
}
63+
64+
struct node *display(struct node *top)
65+
{
66+
struct node *ptr;
67+
ptr=top;
68+
if(top==NULL)
69+
printf("\n the stack is empty");
70+
while(ptr->next!=NULL)
71+
{
72+
printf("\t %d",ptr->data);
73+
ptr=ptr->next;
74+
}
75+
return top;
76+
}
77+
78+
struct node *pop(struct node *top)
79+
{
80+
struct node * ptr,*preptr;
81+
ptr=top;
82+
if(top==NULL)
83+
printf("\n the stack is empty");
84+
top=ptr->next;
85+
free(ptr);
86+
printf("\n The element is popped");
87+
return top;
88+
}
89+
90+
91+
92+

0 commit comments

Comments
 (0)