-
Notifications
You must be signed in to change notification settings - Fork 0
/
STACKSUsingLinkedLists.cpp
112 lines (97 loc) · 2.05 KB
/
STACKSUsingLinkedLists.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#include<stack>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int d){
this->data = d;
this->next = NULL;
}
};
class Stack{
Node* head;
int capacity;
int currSize;
public:
Stack(int c){
this->capacity = c;
this->currSize = 0;
head = NULL;
}
bool isEmpty() {
return this->head == NULL;
}
bool isFull(){
return this->currSize == this->capacity;
}
void push(int data){
if(this->currSize == this->capacity) {
cout<<"Overflow\n";
return;
}
Node* new_node = new Node(data);
new_node->next = this->head;
this->head = new_node;
this->currSize++;
}
int pop(){
if(this->head == NULL){
cout<<"Underflow\n";
return INT_MIN;
}
Node* new_head = this->head->next;
this->head->next = NULL;
Node* tobeRemoved = this->head;
int result = tobeRemoved->data;
delete tobeRemoved;
this->head = new_head;
this->currSize--;
return result;
}
int getTop(){
if(this->head == NULL){
cout<<"Underflow\n";
return INT_MIN;
}
return this->head->data;
}
int size() {
return this->currSize;
}
};
int main() {
Stack st(5);
st.push(1);
st.push(2);
st.push(3);
cout<<st.getTop()<<endl;
st.push(4);
st.push(5);
cout<<st.getTop()<<"\n";
st.push(6);
cout<<st.pop()<<endl;
cout<<st.pop()<<endl;
// cout<<st.getTop()<<"\n";
cout<<st.isEmpty()<<endl;
cout<<st.isFull()<<endl;
cout<<st.size()<<endl;
return 0;
}
// int main() {
// stack<int> st;
// st.push(1);
// st.push(2);
// st.push(3);
// cout<<st.top()<<endl;
// st.push(4);
// st.push(5);
// cout<<st.top()<<"\n";
// st.pop();
// st.pop();
// cout<<st.top()<<endl;
// cout<<st.empty()<<endl;
// cout<<st.size()<<endl;
// return 0;
// }