forked from AugustineAykara/Data-Structure-In-C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-linkedList.c
More file actions
96 lines (80 loc) · 1.17 KB
/
stack-linkedList.c
File metadata and controls
96 lines (80 loc) · 1.17 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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *addr;
}*top = NULL;
void push()
{
int item;
struct node *n;
n = (struct node *) malloc (sizeof(struct node));
printf("\n Enter the element to be pushed : ");
scanf("%d", &item);
n -> data = item;
if(top == NULL)
{
top = n;
n -> addr = NULL;
}
else
{
n -> addr = top;
top = n;
}
}
void pop()
{
if (top == NULL)
{
printf("\n STACK UNDERFLOW !!!");
}
else
{
printf("\n Element %d has been popped out from the stack", top -> data);
top = top -> addr;
}
}
void display()
{
struct node *ptr;
ptr = top;
if (top == NULL)
{
printf("\n Stack is empty !!!");
}
else
{
while(ptr != NULL)
{
printf("\n -> %d ", ptr -> data);
ptr = ptr -> addr;
}
}
printf("\n");
}
void main()
{
int ch;
while(1)
{
printf("\n 1.PUSH to Stack \n 2.POP from Stack \n 3.DISPLAY \n 4.EXIT");
printf("\n Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1: push();
display();
break;
case 2: pop();
display();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\n INVALID CHOICE !!!");
}
}
}