-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackUsingLinkedList.c
156 lines (141 loc) · 3.15 KB
/
StackUsingLinkedList.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
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
// Stack Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct StackNode
{
int data;
struct StackNode *next;
};
struct StackNode *Top = NULL;
struct StackNode **Stack = &Top;
// Declaration of function
struct StackNode *getnode();
void push();
void pop();
void peek();
void display();
// creating stack node
struct StackNode *getnode()
{
struct StackNode *newnode;
newnode = (struct StackNode *)malloc(sizeof(struct StackNode));
newnode->next = NULL;
return newnode;
}
//push data into stack
void push()
{
struct StackNode *newnode;
newnode = getnode();
printf("\n\tEnter the data => ");
scanf("%d",&newnode->data);
if( newnode == NULL )
{
printf("\n\n\t\t Stack Overflow..");
return;
}
if ((*Stack) == NULL)
{
(*Stack) = newnode;
}
else
{
newnode->next = (*Stack);
(*Stack) = newnode;
}
printf("\t\t\tData is pushed\n");
printf("\t\t-----------**********-------------\n");
}
//pop data from stack
void pop()
{
if ((*Stack) == NULL)
{
printf("\n\n\t\t\tStack is Empty\n");
}
else
{
struct StackNode *temp;
temp = (*Stack);
printf("\n\n\t\t\tPopped value is %d\n",temp->data);
(*Stack) = (*Stack)->next;
free(temp);
}
printf("\t\t-----------**********-------------\n");
}
//Peek function
void peek()
{
if((*Stack) == NULL)
{
printf("\n\n\t\t\tStack is Empty\n");
}
else
{
printf("\n\n\t\t\tPeek value is %d\n",(*Stack)->data);
}
printf("\t\t-----------**********-------------\n");
}
//Display Stack
void display()
{
struct StackNode *temp;
if ((*Stack) == NULL)
{
printf("\n\n\t\t\tStack is Empty\n");
}
else
{
temp = (*Stack);
printf("\n\n\t\t Elements in the stack: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
printf("\t\t-----------**********-------------\n");
}
void menu()
{
printf("\n\tStack operations using pointers.. ");
printf("\n--------------*********************************--------------\n");
printf("\t1. push\n");
printf("\t2.Pop\n");
printf("\t3. Peek\n");
printf("\t4. Display\n");
printf("\t5. Exit\n");
printf("--------------*********************************--------------\n");
}
int main()
{
int ch;
menu();
while (1)
{
printf("\tEnter your choice => ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid Input\n");
break;
}
}
return 0;
}