forked from nitinsultania/CPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
74 lines (68 loc) · 1.18 KB
/
stack.c
File metadata and controls
74 lines (68 loc) · 1.18 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
}*top = NULL;
typedef struct node node;
void push(int data)
{
node* p = (node*) malloc(sizeof(node));
p->next = top;
p->data = data;
top = p;
}
int pop()
{
if(top == NULL)
return 0;
node *p = top;
top = p->next;
int data = p->data;
free(p);
return data;
}
void traverse()
{
node *p = top;
while(p != NULL)
{
printf("%d\n",p->data);
p = p->next;
}
}
int main()
{
int op;
start:
printf("Enter Choice: 1. Push 2. Pop 3. Traverse 4. Exit");
scanf("%d",&op);
while(op != 4)
{
switch(op)
{
case 1: printf("Enter Data : ");
int data;
scanf("%d",&data);
push(data);
break;
case 2: printf("Popped Data : %d\n",pop());
getchar();
break;
case 3: traverse();
getchar();
break;
default: printf("Enter correct choice.\n");
getchar();
break;
}
goto start;
}
while(top != NULL)
{
node* p = top;
top = p->next;
free(p);
}
return 0;
}