-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingLinkedList.c
More file actions
98 lines (92 loc) · 2.41 KB
/
StackUsingLinkedList.c
File metadata and controls
98 lines (92 loc) · 2.41 KB
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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* push(struct Node *head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL){
head = newNode;
return head;
}
newNode->next = head;
head = newNode;
return head;
}
int pop(struct Node *head) {
if (head == NULL){
printf("Underflow condition");
return -1;
}
struct Node *temp = head;
head = head->next;
return temp->data;
}
int peek(struct Node *head) {
if (head == NULL){
printf("Stack is empty");
return -1;
}
struct Node *temp = head;
return temp->data;
}
void display(struct Node *head) {
if (head == NULL){
printf("Stack is empty");
return;
}
struct Node *curr = head;
while (curr->next != NULL){
printf("%d -> ", curr->data);
curr = curr->next;
}
printf("%d", curr->data);
}
int main() {
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head = NULL;
int choice, value;
printf("\n--- Stack Implementation using Linked List ---\n");
while (1) {
printf("\n---------- MENU ----------\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek (View Top)\n");
printf("4. Display Stack\n");
printf("5. Exit\n");
printf("--------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
head = push(head, value);
break;
case 2:
value = pop(head);
if (value != -1) {
printf("Popped value is: %d\n", value);
}
break;
case 3:
value = peek(head);
if (value != -1) {
printf("The top element is: %d\n", value);
}
break;
case 4:
display(head);
break;
case 5:
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}