-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkStack.cpp
More file actions
113 lines (97 loc) · 1.81 KB
/
LinkStack.cpp
File metadata and controls
113 lines (97 loc) · 1.81 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
using namespace std;
template <class Type>
class LinkStack
{
private:
struct Node
{
Type item;
Node *next;
};
Node *first;
public:
LinkStack();
~LinkStack();
bool IsEmpty();
void Push(Type item);
Type Pop();
Type GetTop();
void Display();
};
template <class Type>
LinkStack<Type>::LinkStack()
{
first = NULL;
}
template<class Type>
LinkStack<Type>::~LinkStack()
{
Node *tmp;
while(first != NULL)
{
tmp = first;
first = first->next;
delete tmp;
}
}
template<class Type>
bool LinkStack<Type>::IsEmpty()
{
return (first == NULL);
}
template<class Type>
void LinkStack<Type>::Push(Type item)
{
Node *tmp = new Node;
tmp->item = item;
tmp->next = first;
first = tmp;
}
template<class Type>
Type LinkStack<Type>::Pop()
{
Node *tmp = first;
Type item = tmp->item;
first = first->next;
delete tmp;
return item;
}
template<class Type>
Type LinkStack<Type>::GetTop()
{
return first->item;
}
template <class Type>
void LinkStack<Type>::Display()
{
Node *tmp = first;
while(tmp != NULL)
{
cout << "current node is " << tmp->item << endl;
tmp = tmp->next;
}
}
int main()
{
LinkStack<int> oIntStack;
oIntStack.Push(13);
oIntStack.Push(15);
oIntStack.Push(22);
oIntStack.Pop();
oIntStack.Display();
LinkStack<float> oFloatStack;
oFloatStack.Push(2.3);
oFloatStack.Push(2.5);
oFloatStack.Pop();
oFloatStack.Display();
LinkStack<string> oStringStack;
oStringStack.Push("hello ");
oStringStack.Push("world ");
oStringStack.Push("newbee");
oStringStack.GetTop();
oStringStack.Pop();
oStringStack.Display();
return 1;
}