forked from azure2103/program-wiz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_array.c
More file actions
72 lines (67 loc) · 1.35 KB
/
stack_array.c
File metadata and controls
72 lines (67 loc) · 1.35 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
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct stack
{
int data[MAX];
int top;
}Stack;
int pop(Stack *s)
{
//UNDERFLOW CONDITION
if(s->top==-1)
{
printf("Stack underflow");
return -1;
}
s->top -= 1;
return(s->data[s->top + 1]); //returns popped element
}
void push(Stack *s, int num)
{
//OVERFLOW CONDITION
if(s->top == MAX-1)
{ printf("Stack overflow");
return ;
}
s->top += 1;
s->data[s->top] = num; //pushes given element
printf("Element pushed. Successful!\n");
}
void print(Stack *s)
{
int temp = s->top;
while(temp != -1)
{
printf("%d ", s->data[temp]);
temp -=1;
}
return ;
}
int main()
{
Stack *s;
s = (Stack *)malloc(sizeof(Stack));
s->top = -1;
int n, num;
printf("Enter the range of elements you want to input: ");
scanf("%d", &n);
printf("Enter %d numbers\n", n);
while(n != 0)
{
scanf("%d", &num);
push(s, num);
n -= 1;
}
print(s);
char choice = 'y';
while(choice == 'y')
{
printf("\nDo you want to pop out an element? y/n: ");
fflush(stdin);
scanf("%c", &choice);
if(choice == 'y')
printf("%d", pop(s));
}
return 0;
}