-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
92 lines (83 loc) · 1.55 KB
/
stack.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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
struct node *push(struct node *);
struct node *display(struct node *);
struct node *pop(struct node *);
int main()
{
int option;
do
{
printf("\n MAIN MENU");
printf("\n 1.push\n2.pop\n3.display");
printf("\n Enter your option:");
scanf("%d",&option);
switch(option)
{
case 1:top=push(top);break;
case 2:top=pop(top);break;
case 3:top=display(top);break;
case 4:exit(0);break;
default:printf("\n enter the correct option");
}
}while(option!=4);
}
struct node *push(struct node *top)
{
struct node *ptr,*new_node;
int num;
printf("\n press -1 to end");
printf("\n enter the value of data:");
scanf("%d",&num);
while(num!=-1)
{
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
if(top==NULL)
{
new_node->next=NULL;
top=new_node;
}
else
{
new_node->next=top;
top=new_node;
}
printf("\n enter the data:");
scanf("%d",&num);
}
return top;
}
struct node *display(struct node *top)
{
struct node *ptr;
ptr=top;
if(top==NULL)
printf("\n the stack is empty");
while(ptr->next!=NULL)
{
printf("\t %d",ptr->data);
ptr=ptr->next;
}
return top;
}
struct node *pop(struct node *top)
{
struct node * ptr,*preptr;
ptr=top;
if(top==NULL)
printf("\n the stack is empty");
top=ptr->next;
free(ptr);
printf("\n The element is popped");
return top;
}