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 */
15template <class Type>
16struct 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 */
25template <class Type>
26class 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 }
37 std::cout << "Size of stack: " << size << std::endl;
38 }
39
40 /** Default constructor*/
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_
Definition: stack.h:26
bool isEmptyStack()
Definition: stack.h:80
~stack()
Definition: stack.h:77
Type top()
Definition: stack.h:93
stack< Type > & operator=(const stack< Type > &otherStack)
Definition: stack.h:115
void push(Type item)
Definition: stack.h:83
stack()
Definition: stack.h:41
void clear()
Definition: stack.h:112
void display()
Definition: stack.h:29
void pop()
Definition: stack.h:99
node< Type > * stackTop
Definition: stack.h:146
int size
size of stack
Definition: stack.h:147
stack(const stack< Type > &otherStack)
Definition: stack.h:47
T endl(T... args)
Definition: avltree.cpp:13
node< Type > * next
pointer to the next node instance
Definition: stack.h:18
Type data
data at current node
Definition: stack.h:17