forked from axlerider/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
87 lines (86 loc) · 1.25 KB
/
stack.c
File metadata and controls
87 lines (86 loc) · 1.25 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
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int stack[SIZE];
int top;
int a;
int your_choice;
void push();
void pop();
void print();
int main()
{
printf("TechVidvan Tutorial: Array Implementation of Stack in C!\n\n");
top = -1; // indicating that the stack is empty!
do
{
printf("1. Insert an Element!\n2. Delete an Element!\n3. Display the elements of the stack!\n4. Please Exit!\n\n");
printf("Enter your choice from the above: ");
scanf("%d",&your_choice);
switch(your_choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
print();
break;
case 4:
exit(0);
break;
default:
printf("Soory, Please enter a valid choice!\n");
break;
}
}while(your_choice!=4);
return 0;
}
void push()
{
int value;
if(top == SIZE- 1)
{
printf("The Stack is empty!\n");
}
else
{
printf("Enter the element to push into the stack: ");
scanf("%d", &value);
printf("Element added!\n");
top++;
stack[top]=value;
}
}
void pop()
{
int value;
if(top == -1)
{
printf("The Stack is empty!\n");
}
else
{
value=stack[top];
printf("Deleted the element: %d\n",stack[top]);
print("Deleted!\n");
top--;
}
}
void print()
{
if(top == -1)
{
printf("The Stack is empty!\n");
}
else if(top > 0)
{
printf("Elements of the stack are: \n");
for(a = top; a >= 0; a--)
{
printf("%d\n",stack[a]);
}
}
}