-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackImplementation.c
122 lines (105 loc) · 1.79 KB
/
StackImplementation.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
//This is the program to implement Stack
#include<stdio.h>
struct stack
{
int arr[5]; //Initialize Stack
int top;
};
void init(struct stack *t) //Initialize top pointer
{
t->top = -1;
}
void push(struct stack *t) //Function for Push operation in Stack
{
int b;
if (t->top==4)
{
printf("\nStack Overflow.");
}
else
{
printf("\nEnter data You Want to enter : ");
scanf("%d",&b);
t->top++;
t->arr[t->top] = b;
}
}
int pop(struct stack *t) //Function for Pop Operation in Stack
{
int x;
if (t->top == -1)
{
printf("\nStack Undreflow.");
return 0;
}
else
{
x = t->arr[t->top];
t->top--;
}
return x;
}
void peep(struct stack *t) //Function for Peep Operation in Stack
{
int x;
printf("\nEnter Position You Want to enter : ");
scanf("%d",&x);
int a = t->top - x;
int pos_sta = t->top + 1 - x;
if (a < -1)
{
printf("\nElement doesn't Find.");
}
else
{
printf("\nAt Postion %d element is %d",x,t->arr[pos_sta]);
}
}
void display(struct stack *t) //Function for display all data of stack
{
int r = t->top;
int i;
printf("\nDisplaying Stack Element : ");
for (i=r;i>=0;i--)
{
printf("\n\n%d",t->arr[i]);
}
}
int main()
{
int op;
struct stack s;
init(&s);
char ans;
do
{
printf("\nThis are the Operation for stack.");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Peep");
printf("\n4.Display");
printf("\n5.Exit");
printf("\nChoose any Operation for Stack : ");
scanf("%d",&op);
switch(op)
{
case 1:
push(&s);
break;
case 2:
pop(&s);
break;
case 3:
peep(&s);
break;
case 4:
display(&s);
break;
case 5:
return 0;
default :
break;
}
} while(1>0);
return 0;
}