-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked stack
79 lines (73 loc) · 1.1 KB
/
linked stack
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
#include<stdio.h>
#include<stdlib.h>
void display();
void push();
void pop();
struct node
{
int data;
struct node *link;
};
int main()
{
struct node *topptr = NULL;
int choice,data;
while(1)
{
printf("\n ENTER YOUR CHOICE: \n1.PUSH \n2.DISPLAY \n.3.POP \n.4.EXIT");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("ENTER DATA:");
scanf("%d",&data);
push(&topptr,data);
break;
case 2: display(&topptr);
break;
case 3: pop(&topptr);
break;
case 4: exit(0);
default: printf("INVALID CHOICE");
break;
}
}
}
void push(struct node **t, int data)
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode-> data =data;
newnode->link= *t;
*t = newnode;
}
void display( struct node **t)
{
struct node *temp;
if(*t == NULL)
{
printf("empty");
}
else
{
temp= *t;
while(temp ->link != NULL)
{
printf("%d", temp->data);
temp = temp->data;
}
}
}
void pop(struct node **t)
{
struct node *temp;
if(*t=NULL)
{
printf("empty");
}
else
{
temp= *t;
free(temp);
*t = *t->link;
}
}