-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack4.c
94 lines (88 loc) · 1.59 KB
/
Stack4.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
/*
* Stack4.c
*
* Created on: 13 Jul 2024
* Author: ABHISHEK
*/
#include<stdio.h>
#include<conio.h>
int stack[10],top=0,item,store,a2;
//push function inserts element in stack
void push()
{
if(top==-1)
{
printf("Stack is empty");
}
else if(top==10)
{
printf("Stack is full");
}
else
{
printf("Insert element in stack :");
scanf("%d",&stack[top]);
top=top+1;
}
store=top;
}
//pop function removes function from stack
void pop()
{
if(store==-1)
{
printf("Stack is empty");
}
else
{
item=stack[store];
printf("Item removed = %d",item);
store=store-1;
}
}
//traverse function prints element in stack
void traverse()
{
int i;
if(top==-1)
{
printf("\nStack is empty");
}
else
{
for(i=top;i>=0;i--)
{
printf("\n%d",stack[i]);
}
}
}
int main()
{
char a1;
do
{
int a;
printf("Enter any one operation to perform on stack");
printf("\n1 push \n 2 pop \n 3 traverse");
printf("\n Enter option: ");
scanf("%d",&a);
switch(a)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
default:
printf("\nWrong choice");//default loop if a value is greater than 3
}
printf("\nDo you want to continue enter [y,n] :");
scanf("%s",&a1);//takes input for loop continue or exit
}while(a1=='Y'||a1=='y');//checks condition do you want to continue
getch();
return 0;
}