forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_LinkedList.cpp
66 lines (63 loc) · 1.35 KB
/
Stack_LinkedList.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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int _data) {
data = _data;
next = NULL;
}
};
class Stack {
private:
Node *top;
int size, MAX;
public:
Stack() {
top = NULL;
size = 0;
MAX = 10;
}
~Stack() {
cout << "End of stack";
}
bool isEmpty() {
return top == NULL;
}
void push(int data) {
if (isEmpty()) {
top = new Node(data);
size++;
}
else if (size > MAX) {
cout << "Stack is full." << endl;
}
else {
Node *newNode = new Node(data);
newNode->next = top;
top = newNode;
size++;
}
}
int pop() {
if (isEmpty()) {
cout << "Stack is empty." << endl;
return -1;
}
int data = top->data;
top = top->next;
size--;
return data;
}
};
int main() {
Stack stack;
for (int i=1 ; i<=10 ; i++) {
stack.push(i);
}
while (!stack.isEmpty()) {
cout << stack.pop() << " -> ";
}
cout << "NULL" << endl;
}