-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack2.c
103 lines (88 loc) · 2.28 KB
/
stack2.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
//stack using link list using double pointer
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a stack node
struct Node {
int data;
struct Node* next;
};
// Function to push an element onto the stack
void push(struct Node** top, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Stack Overflow\n");
return;
}
newNode->data = value;
newNode->next = *top;
*top = newNode;
printf("%d pushed onto the stack\n", value);
}
// Function to pop an element from the stack
int pop(struct Node** top) {
if (*top == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct Node* temp = *top;
int value = temp->data;
*top = (*top)->next;
free(temp);
return value;
}
// Function to peek the top element of the stack
int peek(struct Node* top) {
if (top == NULL) {
printf("Stack is empty\n");
return -1;
}
return top->data;
}
// Function to display the elements of the stack
void display(struct Node* top) {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
printf("Stack elements are: ");
while (top != NULL) {
printf("%d ", top->data);
top = top->next;
}
printf("\n");
}
int main() {
struct Node* top = NULL;
int choice, value;
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Peek\n5. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&top, value);
break;
case 2:
value = pop(&top);
if (value != -1) {
printf("Popped element: %d\n", value);
}
break;
case 3:
display(top);
break;
case 4:
value = peek(top);
if (value != -1) {
printf("Top element is: %d\n", value);
}
break;
case 5:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}