-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacksUsingSLL.c
100 lines (96 loc) · 1.45 KB
/
stacksUsingSLL.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
#include <stdio.h>
#include <stdlib.h>
int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}
typedef struct stack
{
int no;
struct stack *next;
}node;
node *top = NULL;
void push(int x)
{
node *temp;
temp = (node *)malloc(sizeof(struct stack));
temp -> no = x;
temp -> next = top;
top = temp;
printf("Successfully pushed.\n");
}
void pop()
{
node *ptr;
if(top == NULL)
{
printf("Stack is underflow.\n");
return;
}
printf("Popped value = %d\n", top->no);
ptr = top;
top = top -> next;
free(ptr);
}
void display()
{
node *ptr;
ptr = top;
if(tp == NULL)
{
printf("Stack is empty.\n");
}
else
{
printf("Elements of the stack are : ");
while(ptr != NULL)
{
printf("%d ", ptr -> no);
ptr = ptr -> next;
}
printf("\n");
}
}
void isEmpty()
{
if(top == NULL)
{
printf("Stack is empty.\n");
return;
}
printf("Stack is not empty.\n");
}
void peek()
{
if(top == NULL)
{
printf("Stack is underflow.\n");
return;
}
printf("Peek value = %d\n", top -> no);
}