-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_stack_using_linked_list.c
More file actions
90 lines (83 loc) · 1.81 KB
/
11_stack_using_linked_list.c
File metadata and controls
90 lines (83 loc) · 1.81 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
#include<stdio.h>
struct Node
{
int data;
struct Node *addr_next
}*top;
// Function to Push Element into Stack
struct Node * push()
{
struct Node *ptr;
ptr = (struct Node *)malloc(sizeof(struct Node));
printf("\nEnter Data : ");
scanf("%d", &ptr->data);
if (top == NULL)
{
top = ptr;
top->addr_next = NULL;
}
else
{
ptr->addr_next = top;
top = ptr;
}
printf("Item Pushed Successfully\n\n");
return top;
}
// Function to Pop Element from Stack
struct Node * pop()
{
if (top == NULL)
{
printf("\nStack Underflow\n\n");
return NULL;
}
else if (top->addr_next == NULL)
{
printf("\nPopped Item : %d\n", top->data);
printf("Item Popped Successfully\n\n");
free(top);
return NULL;
}
else
{
struct Node *ptr;
ptr = top;
top = top->addr_next;
printf("\nPopped Item : %d\n", ptr->data);
printf("Item Popped Successfully\n\n");
free(ptr);
return top;
}
}
// Function to Peek into Stack
struct Node * peek()
{
if (top == NULL)
printf("\nStack Underflow\n\n");
else
printf("\nTop-Most Item : %d\n\n", top->data);
}
int main()
{
int choice;
top = NULL;
while(1)
{
printf("Enter\n");
printf("1. To Push an Element\n");
printf("2. To Pop an Element\n");
printf("3. To Peek into the Stack\n");
printf("0. To Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 0 : return 0;
case 1 : top = push(); break;
case 2 : top = pop(); break;
case 3 : peek(); break;
default : printf("\nInvalid Choice, Try Again\n\n");
}
}
}