-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_LL.c
86 lines (77 loc) · 1.24 KB
/
Stack_LL.c
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
// CODE FOR STACK OPERATIONS USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node{
int info;
struct node *next;
}nodetype;
nodetype* push(nodetype *tp,int nm);
nodetype* pop(nodetype*tp);
void display(nodetype *tp);
int main()
{
nodetype *top=NULL;
int num,opt;
char choice = 'Y';
do{
printf("enter opt\n");
scanf("%d",&opt);
if(opt==1){
scanf("%d",&num);
top=push(top,num);
}
else if(opt==2){
if(top!=NULL){
top=pop(top);
}
else {
printf("stack is empty\n");
}
}
else if(opt==3){
if(top!=NULL){
display(top);
}
else
printf("stack is empty\n");
}
else{
printf("invalid opt\n");
}
printf("to continue Y/N\n");
//scanf("%c",&choice);
choice = getch();
}while(choice=='Y');
return 0;
}
nodetype* push(nodetype *tp,int nm){
nodetype *p;
p=(nodetype*)malloc(sizeof(nodetype)) ;
if(p==NULL)
printf("not space\n");
else{
p->info=nm;
p->next=tp;
tp=p;
}
return (tp);
}
nodetype* pop(nodetype*tp){
nodetype *temp;
temp=tp;
printf("element popped %d",tp->info);
tp=tp->next;
free(temp);
return tp;
}
void display(nodetype *tp){
if(tp==NULL)
printf("stack empty\n");
else{
while(tp!=NULL){
printf("%d",tp->info);
tp=tp->next;
}
}
}