-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path(Single Pointer) Stack
More file actions
84 lines (70 loc) · 1.3 KB
/
Copy path(Single Pointer) Stack
File metadata and controls
84 lines (70 loc) · 1.3 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
#ifndef STACK_HPP
#define STACK_HPP
template<typename T>
class Node {
public:
Node();
Node(T&);
private:
T * data;
Node<T> * next;
};
template<typename T>
class Stack{
public:
Stack();
Stack(const Stack<T>&);
~Stack();
bool empty();
bool full();
Stack<T>&operator=(const Stack<T>);
T top();
void push(const T&);
T pop();
private:
Node<T> * tos;
};
template<typename T>
Node<T>::Node(){
data = 0;
next = nullptr;
}
template<typename T>
Node<T>::Node(T&input){
data = input;
next = nullptr;
}
template<typename T>
void Stack<T>::push(const T& input){
Node<T> * temp = tos;
tos = new Node<T>(input);
tos->next = temp;
}
template<typename T>
Stack<T>::Stack(){
tos = nullptr;
}
template<typename T>
Stack<T>::Stack(const Stack<T>&input):Stack(){
Node<T> * current = input.tos;
Node<T>*bottom;
while(current){
if(tos == nullptr){
tos = new Node<T>(current->data);
bottom = tos;
}else{
bottom->next = new Node<T>(current->data);
bottom = bottom->next;
}
current = input.tos->next;
}
}
template<typename T>
T Stack<T>::pop(){
Node<T>*temp = tos;
tos = tos->next;
T value = temp->data;
delete temp;
return value;
}
#endif