-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
58 lines (52 loc) · 910 Bytes
/
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
#include "Node.h"
#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;
typedef int T;
Stack::Stack() {
top = NULL;
}
void Stack::setTop(Node < T > * h) {
top = h;
}
T Stack::Top() {
return top -> data;
}
bool Stack::isEmpty() {
return (top == NULL);
}
int Stack::count() {
Node < T > * temp;
temp = top;
int c = 0;
while (temp != NULL) {
c++;
temp = temp -> next;
}
return c;
}
void Stack::push(T n) {
Node < T > * New = new Node < int > (n);
if (top != NULL) New -> next = top;
top = New;
}
T Stack::pop() {
Node < T > * result;
result = top;
T data = top -> data;
top = top -> next;
delete result;
return data;
}
void Stack::traverse() {
Node < T > * temp = top;
while (temp != NULL) {
cout << temp -> data << " ";
temp = temp -> next;
}
}
ostream & operator << (ostream & o, Stack s) {
s.traverse();
return o;
}