-
Notifications
You must be signed in to change notification settings - Fork 1
/
4) Stack Linked list.cpp
97 lines (73 loc) · 1.45 KB
/
4) Stack Linked list.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
#include<iostream>
#include "Node.cpp"
using namespace std;
// head is pointing to top of stack
class Stack{
Node* head;
int capacity;
int currentsize;
public:
Stack(int c){
capacity = c;
head = NULL;
}
void push(int);
void pop();
void display();
bool isempty();
bool isFull();
};
bool Stack :: isempty(){
return (head==NULL);
}
bool Stack :: isFull(){
return (currentsize==capacity);
}
void Stack :: push(int val){
Node* newnode = new Node(val);
if(isempty()){
// Linked list is empty
head = newnode;
currentsize++;
}
else if(!isFull()){
// insert at head
newnode->next = head;
head = newnode;
currentsize++;
}
else{
cout << "Stack Overflow\n";
}
}
void Stack :: pop(){
if(isempty()){
cout << "Stack empty nothing to pop\n";
}
else{
Node* Nodetobedeleted = head;
head = head->next;
delete Nodetobedeleted;
}
}
void Stack :: display(){
Node* temp = head;
while(temp){
cout << temp->data << endl;
temp = temp->next;
}
cout << "Bottom of stack\n";
}
int main(){
Stack s(5);
for(int i=1;i<=5;i++){
s.push(i);
}
s.display();
s.pop();
s.pop();
s.pop();
s.pop();
s.display();
return 0;
}