-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackUsingLinkedlist.c
86 lines (76 loc) · 1.45 KB
/
stackUsingLinkedlist.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
#include <stdio.h>
#include <stdlib.h>
//struct to create node w data & pointer
struct node{
int info;
struct node* next;
}*top,*top1,*temp;
int count=0;
void push(int data){
if(top==NULL){
top=(struct node*)malloc(1*sizeof(struct node));
top->info=data;
top->next=NULL;
}
else{
temp=(struct node*)malloc(1*sizeof(struct node));
temp->info=data;
temp->next=top;
top=temp;
}
count++;
printf("Node inserted \n");
}
int pop(){
top1=top;
if(top1=NULL){
printf("\n Stack underflow \n");
return -1;
}
else
top1=top1->next;
int popped=top->info;
free(top);
top=top1;
count--;
return popped;
}
void display(){
top1=top;
if(top1==NULL){
printf("\n Stack underflow \n");
return;
}
printf("\n The stack is: \n");
while(top1!=NULL){
printf("%d-->",top1->info);
top1=top1->next;
}
printf("NULL\n");
}
int main(){
int choice, value;
printf("Implementation of Stack Using Linked list \n");
while(1){
printf("Enter required operation here: \n1. Push \n2. Pop \n3. Display \n4. Exit \n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the value to insert:");
scanf("%d",&value);
push(value);
break;
case 2:
printf("\nPopped element is: %d", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Incorrect option selected\n");
}
}
}