-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack as a Linked List
78 lines (76 loc) · 1.67 KB
/
Stack as a 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
Write a menu driven program to implement stack as a linked list. Assume that the size of the stack is 5.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data ;
struct node * link ;
};
void push(struct node **q,int n){
struct node *nn;
nn = (struct node*)malloc(sizeof(struct node));
nn->data = n;
if(*q == NULL){
nn->link = NULL;
*q = nn;
}
else{
nn->link = *q;
*q = nn;
}
}
int count (struct node *q){
struct node *t=q;
int i=0;
while(t!=NULL){
i++;
t = t->link;
}
return i;
}
void pop(struct node **q){
if(*q == NULL){
printf("No element in the stack\n");
}
else{
printf("Element Popped is %d\n",(*q)->data);
(*q) = (*q)->link;
}
}
void display(struct node *q){
struct node *t = q;
while(t!=NULL){
printf("%d ",t->data);
t = t->link;
}
}
int main(){
struct node *s=NULL;int choice,t;
printf("Choice 1 : To push data\nChoice 2 : To pop data\nChoice 3 : To display\nChoice 4 : To count\n\n");
do{
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice){
case 1:{
printf("Enter the data to be pushed\n");
scanf("%d",&t);
push(&s,t);
break;
}
case 2:{
pop(&s);
break;
}
case 3:{
printf("Linked List Contents\n");
display(s);
printf("\n");
break;
}
case 4:{
printf("Number of elements in the linked list is %d\n",count(s));
}
}
}while(choice<=4 && choice!=0);
printf("END");
return 0;
}