-
Notifications
You must be signed in to change notification settings - Fork 56
/
Stack using Linked LIst.c
97 lines (85 loc) · 1.71 KB
/
Stack using Linked LIst.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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int size=0;
struct Node
{
int data;
struct Node* next;
};
struct Node* top = NULL;
typedef struct Node* node;
void push()
{
int data;
if (size == MAX_SIZE)
{
printf("\nOverflow!\n");
return;
}
printf("\nEnter data to push: ");
scanf("%d",&data);
node newNode = (node)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = top;
top = newNode;
size++;
}
void pop()
{
if (top == NULL)
{
printf("\nUnderflow!\n");
return;
}
printf("\nPopped Element: %d",top->data);
node temp = top;
top = top->next;
free(temp);
}
void display()
{
if (top == NULL)
{
printf("Underflow!\n");
return;
}
printf("Stack elements: ");
struct Node* current = top;
while (current != NULL)
{
printf("%d ",current->data);
current=current->next;
}
printf("\n");
}
void main()
{
int choice;
while (1)
{
printf("\nStack Menu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice.\n");
}
}
}