-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementstackusingll.c
More file actions
82 lines (64 loc) · 1.54 KB
/
implementstackusingll.c
File metadata and controls
82 lines (64 loc) · 1.54 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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Heap Overflow\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("Pushed %d onto stack\n", value);
}
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}
struct Node* temp = top;
top = top->next;
printf("Popped %d from stack\n", temp->data);
free(temp);
}
void display() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node* temp = top;
printf("Stack contents:\n");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int choice, value;
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
} else if (choice == 2) {
pop();
} else if (choice == 3) {
display();
} else if (choice == 4) {
printf("Exiting...\n");
break;
} else {
printf("Invalid choice\n");
}
}
return 0;
}