-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.c
More file actions
95 lines (77 loc) · 1.37 KB
/
stack.c
File metadata and controls
95 lines (77 loc) · 1.37 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAX 15
int top = -1;
void push (int ARR[MAX], int val)
{
top++;
ARR[top] = val;
}
int pop(int ARR[MAX])
{
int val = ARR[top];
top--;
return val;
}
void view(int ARR[MAX])
{
int i;
int temp = top;
while(temp>= 0){
printf("%d\t",ARR[temp]);
temp--;
}
printf("\n");
}
void pause_t()
{
printf("Enter a character to continue:");
getchar();
getchar();
}
int menu()
{
int val;
printf("************************* MENU OPTIONS *****************\n");
printf(" Enter 1 to PUSH an Element into STACK\n");
printf(" Enter 2 to POP an element from STACK \n");
printf(" Enter 3 to DISPLAY all the elemnts of STACK\n");
printf(" ENter any other element to exit the program\n");
scanf("%d",&val);
return val;
}
void refresh()
{
write(STDOUT_FILENO, "\x1b[2J",4 );
write(STDOUT_FILENO, "\x1b[H",3);
}
int main()
{
int *ARR = calloc(10,sizeof(int));
refresh();
while(1) {
int data,val,num;
data = menu();
switch(data) {
case 1:
printf("ENter an element to be pushed into the stack:-\n");
scanf("%d",&val);
push(ARR,val);
break;
case 2:
num = pop(ARR);
printf("\n Popped element is = %d\n",num);
pause_t();
break;
case 3:
view(ARR);
pause_t();
break;
default :
exit(1);
}
refresh();
}
return 0;
}