-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack Operations.c
110 lines (101 loc) · 1.8 KB
/
Stack Operations.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void display();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n----MENU----");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Dipslay");
printf("\n4.Exit");
create();
while (1)
{
printf("\nEnter option: ");
scanf("%d",&ch);
switch (ch)
{
case 1:
printf("Enter item : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default :
printf("Invalid input!");
break;
}
}
}
void create()
{
top = NULL;
}
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\nSTACK UNDERFLOW");
return;
}
else
top1 = top1->ptr;
printf("\nPopped value: %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}