-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
45 lines (42 loc) · 811 Bytes
/
Stack.c
File metadata and controls
45 lines (42 loc) · 811 Bytes
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
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int top = -1;
//Check if stack is empty or not
int isempty() {
if(top == -1)
return true;
else
return false;
}
//Check stack is full or not
int isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
//retrun the topmost element in an array
int peek() {
return stack[top];
}
//delete and return the topmost element
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
//insert elemnt at one end
int push(int data) {
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}