-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack1.cpp
More file actions
91 lines (81 loc) · 1.61 KB
/
stack1.cpp
File metadata and controls
91 lines (81 loc) · 1.61 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include "stack.h"
#define MAX 5
using std :: cout;
using std :: endl;
int count {0};
Stack :: Stack(){
HEAD = NULL;
}
Stack :: ~Stack(){};
bool Stack :: isEmpty(){
return (HEAD == NULL);
}
void Stack :: push(int data){
if (count == MAX){
cout << "Stack overflow!" << endl;
}
else{
Node *newNode = new Node();
newNode -> info = data;
newNode -> next = HEAD;
HEAD = newNode;
count++;
}
}
int Stack :: pop(){
if (isEmpty()){
cout << "Stack Underflow" << endl;
return -1;
}
else{
Node *toDelete;
int value;
toDelete = HEAD;
value = toDelete -> info;
HEAD = toDelete -> next;
delete toDelete;
count--;
return value;
}
}
int Stack :: top(){
if(isEmpty()){
cout << "Stack Underflow" << endl;
return -1;
}
else{
return (HEAD -> info);
}
}
void Stack :: traverse(){
Node *temp = HEAD;
while (temp != NULL)
{
cout << temp -> info << endl;
temp = temp -> next;
}
}
int main(){
Stack S;
cout << S.isEmpty() <<endl;
cout << "*****************************" << endl;
S.push(2);
S.push(4);
S.push(6);
S.push(8);
S.push(10);
S.traverse();
S.push(69);
cout << "*****************************" << endl;
cout << S.top() << endl;
cout << S.pop() <<endl;
cout << S.top() << endl;
cout << S.pop() <<endl;
cout << "*****************************" << endl;
S.push(69);
S.push(420);
S.push(699);
S.traverse();
return 0;
}