-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_implementation.cpp
More file actions
68 lines (68 loc) · 1.24 KB
/
Copy pathstack_implementation.cpp
File metadata and controls
68 lines (68 loc) · 1.24 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
#include <stdlib.h>
#include <iostream>
/*
stack is a linear data structure that follows the principle of Last In First Out (LIFO)
last element inserted inside the stack is removed first
*/
using namespace std;
#define MAX 10
int size = 0;
struct stack {
int items[MAX];
int top;
};
typedef struct stack st;
void createEmptyStack(st *s) {
s->top = -1;
}
int isfull(st *s) {
if (s->top == MAX - 1)
return 1;
else
return 0;
}
int isempty(st *s) {
if (s->top == -1)
return 1;
else
return 0;
}
void push(st *s, int newitem) {
if (isfull(s)) {
cout << "STACK FULL";
} else {
s->top++;
s->items[s->top] = newitem;
}
size++;
}
void pop(st *s) {
if (isempty(s)) {
cout << "\n STACK EMPTY \n";
} else {
cout << "Item popped= " << s->items[s->top];
s->top--;
}
size--;
cout << endl;
}
void printStack(st *s) {
printf("Stack: ");
for (int i = 0; i < size; i++) {
cout << s->items[i] << " ";
}
cout << endl;
}
int main() {
int ch;
st *s = (st *)malloc(sizeof(st));
createEmptyStack(s);
push(s, 1);
push(s, 2);
push(s, 3);
push(s, 4);
printStack(s);
pop(s);
cout << "\nAfter popping out\n";
printStack(s);
}