-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
93 lines (92 loc) · 1.87 KB
/
stack.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
#include<stdio.h>
#define MAX 20
int top=-1;
int stack[MAX];
int StackisFull()
{
if(top==MAX)
return 1;
else
return 0;
}
int StackisEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
void push(int x)
{
if(StackisFull())
{
SetColor(11);
printf("Stack overflow\n");
return;
}
stack[++top]=x;
}
int pop()
{
int item;
if(StackisEmpty())
{
SetColor(11);
printf("Stack underflow\n");
return 0;
}
item=stack[top];
top--;
return item;
}
void display()
{
SetColor(11);
int i;
printf("\nStack: \n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
printf("\n");
}
void Stack()
{
int c,x,n;
while(1)
{
int i=0;
system("cls");
SetColor(11);
printf("\t\t\t******STACK******\n\n\n");
SetColor(7);
printf("\t\t1.Push an item\n");
printf("\t\t2.Pop an item\n");
printf("\t\t3.Display the stack\n");
printf("\t\t4.Go to main menu\n\n");
printf("\t\tEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1: printf("\nEnter the no. of values: ");
scanf("%d",&n);
while(i<n)
{
printf("Enter the value: ");
scanf("%d",&x);
push(x);
i++;
}
display();
system("pause");
break;
case 2: x=pop();
SetColor(11);
printf("%d deleted successfully\n",x);
system("pause");
break;
case 3: display();
system("pause");
break;
case 4: return;
}
}
}