-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
96 lines (84 loc) · 2.13 KB
/
stack.cpp
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
96
#include<iostream>
using namespace std;
#define watch(x) cout<<(#x)<<" is "<<x<<"\n"
#define print(x) cout<<x<<"\n"
#define stackcapacity 100
struct Stackdata{
// data to be stored in stack
int data;
// Node* nodeptr; // This is just for example;
};
struct Stack{
Stackdata arr[stackcapacity];
// if you want to allocate memory with malloc then you can store a pointer.
// And then set this pointer to the pointer returned by malloc function.
// Stackdata* ptr; (now in init function write ptr=(Stackdata*)malloc(sizeof(Stackdata*capacity)))
int top;
int size;
int maxsize;
};
Stack* getnewstack(){
Stack* stackptr;
stackptr = (Stack*)malloc(sizeof(Stack));
stackptr->top=0;
stackptr->size=0;
stackptr->maxsize=stackcapacity;
return stackptr;
}
int isempty(Stack* stackptr){
if(stackptr->size==0){
return 1;
}
return 0;
}
int isfull(Stack* stackptr){
if(stackptr->size>=stackptr->maxsize)
return 1;
return 0;
}
int push(Stack* stackptr, Stackdata sdata){
// copy all of your stack data into stack
if(isfull(stackptr)){
return 0;
}
int top = stackptr->top;
stackptr->arr[top].data = sdata.data;
// copy if you have also another data;
stackptr->top++;
stackptr->size++;
return 1;
}
int pop(Stack* stackptr){
if(isempty(stackptr))
return 0;
stackptr->top--;
stackptr->size--;
return 1;
}
Stackdata peek(Stack* stackptr){
int top = stackptr->top - 1; // also you can check if stack is empty or not
return stackptr->arr[top]; // or we can return pointer for faster access; (As this will remove additonal copy of elements)
}
void printstack(Stack* stackptr){
int size = stackptr->size;
print("Printing Stack");
for(int i=0;i<size;i++){
print(stackptr->arr[i].data);
}
print("Printing Finished");
}
int main(){
Stack* ptr;
Stackdata sdata;
ptr = getnewstack();
for(int i=-10;i<399;i++){
sdata.data=i;
push(ptr,sdata);
}
printstack(ptr);
pop(ptr);
pop(ptr);
sdata = peek(ptr);
print(sdata.data);
return 0;
}