Algorithms_in_C++
1.0.0
Set of algorithms implemented in C++.
stack.h
Go to the documentation of this file.
1
/**
2
* @file stack.h
3
* @author danghai
4
* @brief This class specifies the basic operation on a stack as a linked list
5
**/
6
#ifndef DATA_STRUCTURES_STACK_H_
7
#define DATA_STRUCTURES_STACK_H_
8
9
#include <cassert>
10
#include <iostream>
11
12
/** Definition of the node as a linked-list
13
* \tparam Type type of data nodes of the linked list should contain
14
*/
15
template
<
class
Type>
16
struct
node
{
17
Type
data
;
///< data at current node
18
node<Type>
*
next
;
///< pointer to the next ::node instance
19
};
20
21
/** Definition of the stack class
22
* \tparam Type type of data nodes of the linked list in the stack should
23
* contain
24
*/
25
template
<
class
Type>
26
class
stack
{
27
public
:
28
/** Show stack */
29
void
display
() {
30
node<Type>
*current =
stackTop
;
31
std::cout
<<
"Top --> "
;
32
while
(current !=
nullptr
) {
33
std::cout
<< current->data <<
" "
;
34
current = current->next;
35
}
36
std::cout
<<
std::endl
;
37
std::cout
<<
"Size of stack: "
<<
size
<<
std::endl
;
38
}
39
40
/** Default constructor*/
41
stack
() {
42
stackTop
=
nullptr
;
43
size
= 0;
44
}
45
46
/** Copy constructor*/
47
explicit
stack
(
const
stack<Type>
&otherStack) {
48
node<Type>
*newNode, *current, *last;
49
50
/* If stack is no empty, make it empty */
51
if
(
stackTop
!=
nullptr
) {
52
stackTop
=
nullptr
;
53
}
54
if
(otherStack.
stackTop
==
nullptr
) {
55
stackTop
=
nullptr
;
56
}
else
{
57
current = otherStack.
stackTop
;
58
stackTop
=
new
node<Type>
;
59
stackTop
->data = current->data;
60
stackTop
->next =
nullptr
;
61
last =
stackTop
;
62
current = current->next;
63
/* Copy the remaining stack */
64
while
(current !=
nullptr
) {
65
newNode =
new
node<Type>
;
66
newNode->data = current->data;
67
newNode->next =
nullptr
;
68
last->next = newNode;
69
last = newNode;
70
current = current->next;
71
}
72
}
73
size
= otherStack.
size
;
74
}
75
76
/** Destructor */
77
~stack
() {}
78
79
/** Determine whether the stack is empty */
80
bool
isEmptyStack
() {
return
(
stackTop
==
nullptr
); }
81
82
/** Add new item to the stack */
83
void
push
(Type item) {
84
node<Type>
*newNode;
85
newNode =
new
node<Type>
;
86
newNode->data = item;
87
newNode->next =
stackTop
;
88
stackTop
= newNode;
89
size
++;
90
}
91
92
/** Return the top element of the stack */
93
Type
top
() {
94
assert(
stackTop
!=
nullptr
);
95
return
stackTop
->data;
96
}
97
98
/** Remove the top element of the stack */
99
void
pop
() {
100
node<Type>
*temp;
101
if
(!
isEmptyStack
()) {
102
temp =
stackTop
;
103
stackTop
=
stackTop
->next;
104
delete
temp;
105
size
--;
106
}
else
{
107
std::cout
<<
"Stack is empty !"
<<
std::endl
;
108
}
109
}
110
111
/** Clear stack */
112
void
clear
() {
stackTop
=
nullptr
; }
113
114
/** Overload "=" the assignment operator */
115
stack<Type>
&
operator=
(
const
stack<Type>
&otherStack) {
116
node<Type>
*newNode, *current, *last;
117
118
/* If stack is no empty, make it empty */
119
if
(
stackTop
!=
nullptr
) {
120
stackTop
=
nullptr
;
121
}
122
if
(otherStack.
stackTop
==
nullptr
) {
123
stackTop
=
nullptr
;
124
}
else
{
125
current = otherStack.
stackTop
;
126
stackTop
=
new
node<Type>
;
127
stackTop
->data = current->data;
128
stackTop
->next =
nullptr
;
129
last =
stackTop
;
130
current = current->next;
131
/* Copy the remaining stack */
132
while
(current !=
nullptr
) {
133
newNode =
new
node<Type>
;
134
newNode->data = current->data;
135
newNode->next =
nullptr
;
136
last->next = newNode;
137
last = newNode;
138
current = current->next;
139
}
140
}
141
size
= otherStack.
size
;
142
return
*
this
;
143
}
144
145
private
:
146
node<Type>
*
stackTop
;
/**< Pointer to the stack */
147
int
size
;
///< size of stack
148
};
149
150
#endif // DATA_STRUCTURES_STACK_H_
stack::pop
void pop()
Definition:
stack.h:99
stack::stack
stack(const stack< Type > &otherStack)
Definition:
stack.h:47
node
Definition:
avltree.cpp:13
node
struct list node
std::queue
STL class.
std::cout
stack_linkedList
Definition:
queue_using_linkedlist.cpp:10
linkedlist
Definition:
queue_using_linkedlist.cpp:6
stack::push
void push(Type item)
Definition:
stack.h:83
stack::size
int size
size of stack
Definition:
stack.h:147
node::next
node< Type > * next
pointer to the next node instance
Definition:
stack.h:18
stack::top
Type top()
Definition:
stack.h:93
data
int data[MAX]
test data
Definition:
hash_search.cpp:24
std::endl
T endl(T... args)
stack::stack
stack()
Definition:
stack.h:41
stack::operator=
stack< Type > & operator=(const stack< Type > &otherStack)
Definition:
stack.h:115
std
STL namespace.
show
void show(const struct tower *const F, const struct tower *const T, const struct tower *const U)
Definition:
tower_of_hanoi.cpp:19
main
int main()
Definition:
avltree.cpp:134
stack
Definition:
stack.h:26
Queue_Array
Definition:
queue_using_array.cpp:13
stack::display
void display()
Definition:
stack.h:29
stack::isEmptyStack
bool isEmptyStack()
Definition:
stack.h:80
node::data
Type data
data at current node
Definition:
stack.h:17
stack::stackTop
node< Type > * stackTop
Definition:
stack.h:146
stack::~stack
~stack()
Definition:
stack.h:77
std::cin
std::exit
T exit(T... args)
stack::clear
void clear()
Definition:
stack.h:112
std::next
T next(T... args)
data_structures
stack.h
Generated on Wed Jun 24 2020 17:40:27 for Algorithms_in_C++ by
1.8.18