-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
100 lines (83 loc) · 1.61 KB
/
Stack.c
File metadata and controls
100 lines (83 loc) · 1.61 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
99
100
//
// Created by Prince on 21-08-2024.
//
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
} *top = NULL;
void push(int const value) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (top == NULL) {
top = newNode;
}
else {
newNode->next = top;
top = newNode;
}
}
void pop() {
if (top == NULL) {
printf("\nThe stack is empty.");
}
else {
struct Node* temp = top;
printf("The popped element : %d", top->data);
top = temp->next;
free(temp);
}
}
void display() {
const struct Node* temp = top;
printf("\nElements :");
while (temp != NULL) {
printf("[%d]", temp->data);
temp = temp->next;
}
}
void menu() {
printf("\nChoose :\n1. Push\n2. Pop\n3. Display\n4. Exit\n Enter option: ");
}
int main() {
int option, data, size;
do {
menu();
scanf("%d", &option);
switch (option) {
case 1: {
printf("\nEnter the number of values to be pushed :");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
printf("Enter the #%d of the element :", i + 1);
scanf("%d", &data);
push(data);
}
break;
}
case 2: {
printf("\nEnter the number of values to be popped :");
scanf("%d", &size);
for (int i = 0; i < size; i++) {
pop();
}
break;
}
case 3: {
display();
break;
}
case 4: {
printf("\nExiting program...");
break;
}
default: {
printf("Invalid option");
break;
}
}
} while (option != 4);
return 0;
}