-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedStack.c
89 lines (83 loc) · 1.92 KB
/
LinkedStack.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
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* link;
};
struct Node* top = NULL;
void push(int data){
struct Node *temp;
temp = (struct Node*)malloc(sizeof(struct Node));
if(temp == NULL){
printf("Insufficient Memory!!\n");
return;
}
temp->data = data;
temp->link = top;
top = temp;
printf("%d pushed to stack\n", data);
}
void pop(){
struct Node *temp;
if(top == NULL){
printf("Stack is empty!!\n");
return;
}
temp = top;
printf("%d popped from stack\n", temp->data);
top = top->link;
free(temp);
temp = NULL;
}
void peek(){
if(top == NULL){
printf("Stack is empty!!\n");
return;
}
printf("Top element of the stack is: %d\n", top->data);
return;
}
void display(){
struct Node *ptr;
ptr = top;
if(ptr == NULL){
printf("Stack is empty!\n");
return;
}
printf("Linked Stack elements are: ");
while(ptr!=NULL){
printf("%d ", ptr->data);
ptr = ptr->link;
}
printf("\n");
}
int main(void){
int ch, data;
printf("-----------Linked Stack-----------\n");
for(;;){
printf("\nLinked Stack Operations:\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter the value to push: ");
scanf("%d",&data);
push(data);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}