-
Notifications
You must be signed in to change notification settings - Fork 2
/
linked_stacks.c
110 lines (105 loc) · 2.6 KB
/
linked_stacks.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
/* Can also use typedef here*/
struct stack{
int data;
struct stack *next;
};
/*Declaring the pointer with which we will keep track of the stack*/
struct stack *top = NULL;
/* Function to push an element into the stack*/
struct stack *push(struct stack *top,int val){
struct stack *ptr;
ptr = (struct stack *)malloc(sizeof(struct stack));
ptr->data = val;
if(top == NULL){
ptr->next = NULL;
top = ptr;
}
else{
ptr->next = top;
top = ptr;
}
return top;
}
/*Function to pop an element from the stack*/
struct stack *pop(struct stack *top){
struct stack *ptr;
ptr = top;
if(top == NULL){
printf("\nStack UNDERFLOW!");
}
else{
top = top->next;
printf("\nThe element deleted is: %d ",ptr->data);
free(ptr);
}
return top;
}
/*Funtion to return tthe peak element in the stack!*/
int peak(struct stack *top){
if(top == NULL){
return -1;
}
else{
return (top->data);
}
}
/*Function to display elements of the stack*/
struct stack *display(struct stack *top){
struct stack *ptr;
ptr = top;
if(top == NULL){
printf("\nStack is empty!");
}
else{
printf("\nThe elements of the stack are:\t");
while(ptr != NULL){
printf("%d ",ptr->data);
ptr = ptr->next;
}
}
return top;
}
/*The main function*/
void main(){
int val,option;
do{
printf("\n**********Menu!**************");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEAK");
printf("\n4.DISPLAY");
printf("\n5.EXIT");
printf("\nEnter your choice:\t");
scanf("%d",&option);
switch(option){
case 1:
printf("\nEnter the Element");
scanf("%d",&val);
top = push(top,val);
break;
case 2:
top = pop(top);
break;
case 3:
val = peak(top);
if(val != -1){
printf("T\nhe value at the top of the stack is %d",val);
}
else{
printf("\nStack is empty!");
}
break;
case 4:
top = display(top);
break;
case 5:
break;
default:
printf("\nINVALID CHOICE");
}
}while(option != 5);
}