-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathStack using linked list
80 lines (77 loc) · 1.51 KB
/
Stack using linked list
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
#include <stdio.h>
#include <malloc.h>
struct node{
int data;
struct node *next;
}*top=NULL;
void push(int);
void pop();
void dis();
int main()
{
int ch,t;
while(1){
printf("\n");
printf("1:PUSH\n");
printf("2:POP\n");
printf("3:DISPLAY\n");
printf("4:EXIT\n");
printf("Enter option\n");
scanf("%d",&ch);
switch (ch)
{
case 1: {
printf("enter data:");
scanf("%d", &t);
push(t);
continue;
}
case 2:{
pop();
continue;
}
case 3:{
dis();
continue;
}
case 4:{
return 0;
}
}
}
return 0;
}
void push(int n)
{
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = n;
if(top==NULL)
{
newnode->next=NULL;
}
else{
newnode->next=top;
}
top = newnode;
}
void pop(){
if(top==NULL)
printf("STACK Underflow: stack is empty.....");
else{
struct node *temp = top;
printf("value deleted %d", temp->data);
top=top->next;
free(temp);
}
}
void dis() {
if (top == NULL) {
printf("No data found in stack!!!!");
} else {
struct node *temp = top;
while (temp != NULL) {
printf("<--%d\t", temp->data);
temp = temp->next;
}
}
}