forked from khushal87/Data-structures-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
101 lines (84 loc) · 1.27 KB
/
stack.c
File metadata and controls
101 lines (84 loc) · 1.27 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
100
101
//created by akashbhalotia
#include<stdio.h>
#include<stdlib.h>
int isEmpty;
struct stack
{
int data;
struct stack *next;
} *top;
void push(int data)
{
struct stack *newNode=(struct stack*)malloc(sizeof(struct stack*));
isEmpty=0;
newNode->data=data;
newNode->next=top;
top=newNode;
printf("%d inserted.\n",data);
}
int pop()
{
int data;
if(top==NULL)
{
printf("Stack underflow!\n");
data=-1;
isEmpty=1;
}
else
{
struct stack *tmp=top;
top=top->next;
data=tmp->data;
free(tmp);
}
return data;
}
void printStack()
{
struct stack *tmp=top;
if(top==NULL)
printf("Stack is empty.");
while(tmp!=NULL)
{
printf("%d->",tmp->data);
tmp=tmp->next;
}
printf("\n");
}
int main()
{
int choice, data;
top=NULL;
isEmpty=1;
do
{
printf("\nMENU:\n");
printf("1)Push\n2)Pop\n3)Print Stack\n4)Exit\n");
printf("Enter your choice.\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a number\n");
scanf("%d",&data);
push(data);
break;
case 2:
data=pop();
if(!isEmpty)
printf("%d deleted.\n",data);
break;
case 3:
printStack();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
}
}
while(choice!=4);
return 0;
}