forked from sanjaysunil34/hacktober2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-StackUsingArray.c
More file actions
85 lines (78 loc) · 1.61 KB
/
1-StackUsingArray.c
File metadata and controls
85 lines (78 loc) · 1.61 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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct {
/*
The stack should contain an array to hold a maximum of 10 elements.
*/
int arr[10];
} Stack;
/*
Initialising the stack, use this stack variable 's' in your functions.
*/
Stack s;
int top = -1;
void push(int n) {
/*
Push the integer n into the stack.
Ignore if the operation is not possible.
*/
if(top < 9){
s.arr[++top] = n;
}
}
int pop() {
/*
Pop the top element in the stack and return that element.
Return -1 the operation is not possible.
*/
int temp;
if(top == -1){
return -1;
}
else{
temp = s.arr[top--];
return temp;
}
}
bool isEmpty() {
/*
Check if the stack is empty or not. Return true/false.
*/
if(top == -1)
return true;
else
return false;
}
bool isFull() {
/*
Check if the stack is full or not. Return true/false.
*/
if(top == 9)
return true;
else
return false;
}
int main() {
int q, choice, n;
scanf("%d", &q);
while(q--) {
scanf("%d%d", &choice, &n);
switch(choice) {
case 0: push(n);
break;
case 1: printf("%d\n", pop());
break;
case 2: printf("%d\n", isEmpty());
break;
case 3: printf("%d\n", isFull());
break;
// case 4: ;
// Stack temp;
// pop(&temp);
// push(&temp, n);
// break;
}
}
return 0;
}