This repository was archived by the owner on Apr 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack(Link_list).cpp
More file actions
94 lines (90 loc) · 1.41 KB
/
Copy pathStack(Link_list).cpp
File metadata and controls
94 lines (90 loc) · 1.41 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
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
T Value;
Node *Next;
};
template <typename T>
class Stack
{
Node<T> *Head;
public:
Stack();
void push(T );
bool pop(T &);
bool top(T &);
bool is_empty();
int size();
void print();
//~Stack();
};
template <typename T>
Stack<T>::Stack()
{
Head = new (Node<T>);
Head->Next = nullptr;
};
template <typename T>
void Stack<T>::print()
{
Node<T> *t;
t = Head;
t = t->Next;
while (t != nullptr)
{
cout << t->Value << " ";
t = t->Next;
}
cout << endl;
}
template <typename T>
bool Stack<T>::is_empty()
{
if (Head->Next == nullptr)
return true;
return false;
}
template <typename T>
void Stack<T>::push(T Value)
{
Node<T> *t;
t = new (Node<T>);
t->Value = Value;
t->Next = Head->Next;
Head->Next = t;
}
template <typename T>
bool Stack<T>::pop(T &out)
{
if (is_empty())
return false;
Node<T> *t;
t = Head->Next;
Head->Next = t->Next;
out = t->Value;
delete t;
return true;
}
template <typename T>
bool Stack<T>::top(T &out)
{
if (is_empty())
return false;
out = (Head->Next)->Value;
return true;
}
template <typename T>
int Stack<T>::size()
{
int Size = 0;
Node<T> *t;
t = Head;
while (t->Next != nullptr)
{
t = t->Next;
Size++;
}
return Size;
}