-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks.c
More file actions
128 lines (115 loc) · 2.2 KB
/
stacks.c
File metadata and controls
128 lines (115 loc) · 2.2 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
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
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define SIZE 10
int top = -1;
int ttp=-1;
int stack[SIZE];
int arr[SIZE];
void push();
void pop();
void show();
void srch(int x);
int swap(int i,int j);
int main()
{
int choice;
while (1)
{
printf("\nPerform operations on the stack:");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.search and pop a certain element\n5.Exit");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
int a;
printf("Enter no of elements to be added:");
scanf("%d",&a);
for(int i=0;i<a;i++){
push();
}
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
int y;
printf("Enter element to pop:");
scanf("%d",&y);
srch(y);
break;
case 5:
exit(0);
default:
printf("\nInvalid choice!!");
}
}
}
void push()
{
if (top == SIZE - 1)
{
printf("\nOverflow!!");
}
else
{
int x;
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top++;
stack[top] = x;
}
}
void pop( )
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", stack[top]);
top = top - 1;
}
}
void show()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i)
printf("%d\n", stack[i]);
}
}
void srch(int y){
int x;
if (top == -1)
{
printf("\nUnderflow");
}
else{
for (int i=top;i>=0;--i){
if(stack[i]==y)
{
for(int j=i;j<top;j++){
for(int u=j+1;u<top+1;u++){
x = stack[j];
stack[j] = stack[u];
stack[u] = x;
j++;
}
printf("\nPopped element: %d", stack[top]);
top = top - 1;
}
}
}
}
}