-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstack_using_linked_list.c
More file actions
157 lines (130 loc) · 3.16 KB
/
Copy pathstack_using_linked_list.c
File metadata and controls
157 lines (130 loc) · 3.16 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Program: Stack Implementation Using Linked List
* Description: Implements stack operations using linked list (dynamic memory)
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/DATA-STRUCTURES-AND-DATA-STRUCTURES-LAB
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// Node structure
struct Node {
int value;
struct Node* next;
};
// Global top pointer
struct Node* top = NULL;
// Function prototypes
void push();
void pop();
void display();
void freeStack();
int main() {
int choice;
printf("=== Stack Using Linked List ===\n");
while (1) {
printf("\n--- Menu ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
freeStack();
printf("\nExiting...\n");
exit(0);
default:
printf("\nInvalid choice! Please enter 1-4.\n");
}
}
return 0;
}
/*
* Function: push
* Description: Pushes a new element onto the stack
* Note: Creates new node using malloc - no size limit
*/
void push() {
int value;
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("\nCannot push element: Memory allocation failed!\n");
return;
}
printf("\nEnter value to push: ");
scanf("%d", &value);
// Set node data
newNode->value = value;
// If stack is empty
if (top == NULL) {
newNode->next = NULL;
top = newNode;
}
// If stack has elements
else {
newNode->next = top;
top = newNode;
}
printf("Item %d pushed successfully!\n", value);
}
/*
* Function: pop
* Description: Removes and returns top element from stack
*/
void pop() {
struct Node* temp;
int poppedValue;
// Check if stack is empty
if (top == NULL) {
printf("\nStack Underflow! Stack is empty.\n");
return;
}
poppedValue = top->value;
temp = top;
top = top->next;
// Free the popped node
free(temp);
printf("\nItem popped = %d\n", poppedValue);
}
/*
* Function: display
* Description: Displays all elements in the stack from top to bottom
*/
void display() {
struct Node* current;
if (top == NULL) {
printf("\nStack is empty!\n");
return;
}
printf("\nStack elements (top to bottom):\n");
current = top;
while (current != NULL) {
printf(" %d\n", current->value);
current = current->next;
}
}
/*
* Function: freeStack
* Description: Frees all allocated memory in the stack
*/
void freeStack() {
struct Node* current = top;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
top = NULL;
}