mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Bug: LGTM issue with missing copy constructor
Merge pull request #893 from kvedala/fix/stack.h
This commit is contained in:
commit
ddb166e238
@ -1,18 +1,27 @@
|
|||||||
/* This class specifies the basic operation on a stack as a linked list */
|
/**
|
||||||
|
* @file stack.h
|
||||||
|
* @author danghai
|
||||||
|
* @brief This class specifies the basic operation on a stack as a linked list
|
||||||
|
**/
|
||||||
#ifndef DATA_STRUCTURES_STACK_H_
|
#ifndef DATA_STRUCTURES_STACK_H_
|
||||||
#define DATA_STRUCTURES_STACK_H_
|
#define DATA_STRUCTURES_STACK_H_
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
/* Definition of the node */
|
/** Definition of the node as a linked-list
|
||||||
|
* \tparam Type type of data nodes of the linked list should contain
|
||||||
|
*/
|
||||||
template <class Type>
|
template <class Type>
|
||||||
struct node {
|
struct node {
|
||||||
Type data;
|
Type data; ///< data at current node
|
||||||
node<Type> *next;
|
node<Type> *next; ///< pointer to the next ::node instance
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Definition of the stack class */
|
/** Definition of the stack class
|
||||||
|
* \tparam Type type of data nodes of the linked list in the stack should
|
||||||
|
* contain
|
||||||
|
*/
|
||||||
template <class Type>
|
template <class Type>
|
||||||
class stack {
|
class stack {
|
||||||
public:
|
public:
|
||||||
@ -20,7 +29,7 @@ class stack {
|
|||||||
void display() {
|
void display() {
|
||||||
node<Type> *current = stackTop;
|
node<Type> *current = stackTop;
|
||||||
std::cout << "Top --> ";
|
std::cout << "Top --> ";
|
||||||
while (current != NULL) {
|
while (current != nullptr) {
|
||||||
std::cout << current->data << " ";
|
std::cout << current->data << " ";
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
@ -30,15 +39,45 @@ class stack {
|
|||||||
|
|
||||||
/** Default constructor*/
|
/** Default constructor*/
|
||||||
stack() {
|
stack() {
|
||||||
stackTop = NULL;
|
stackTop = nullptr;
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Copy constructor*/
|
||||||
|
explicit stack(const stack<Type> &otherStack) {
|
||||||
|
node<Type> *newNode, *current, *last;
|
||||||
|
|
||||||
|
/* If stack is no empty, make it empty */
|
||||||
|
if (stackTop != nullptr) {
|
||||||
|
stackTop = nullptr;
|
||||||
|
}
|
||||||
|
if (otherStack.stackTop == nullptr) {
|
||||||
|
stackTop = nullptr;
|
||||||
|
} else {
|
||||||
|
current = otherStack.stackTop;
|
||||||
|
stackTop = new node<Type>;
|
||||||
|
stackTop->data = current->data;
|
||||||
|
stackTop->next = nullptr;
|
||||||
|
last = stackTop;
|
||||||
|
current = current->next;
|
||||||
|
/* Copy the remaining stack */
|
||||||
|
while (current != nullptr) {
|
||||||
|
newNode = new node<Type>;
|
||||||
|
newNode->data = current->data;
|
||||||
|
newNode->next = nullptr;
|
||||||
|
last->next = newNode;
|
||||||
|
last = newNode;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size = otherStack.size;
|
||||||
|
}
|
||||||
|
|
||||||
/** Destructor */
|
/** Destructor */
|
||||||
~stack() {}
|
~stack() {}
|
||||||
|
|
||||||
/** Determine whether the stack is empty */
|
/** Determine whether the stack is empty */
|
||||||
bool isEmptyStack() { return (stackTop == NULL); }
|
bool isEmptyStack() { return (stackTop == nullptr); }
|
||||||
|
|
||||||
/** Add new item to the stack */
|
/** Add new item to the stack */
|
||||||
void push(Type item) {
|
void push(Type item) {
|
||||||
@ -52,7 +91,7 @@ class stack {
|
|||||||
|
|
||||||
/** Return the top element of the stack */
|
/** Return the top element of the stack */
|
||||||
Type top() {
|
Type top() {
|
||||||
assert(stackTop != NULL);
|
assert(stackTop != nullptr);
|
||||||
return stackTop->data;
|
return stackTop->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,30 +109,30 @@ class stack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Clear stack */
|
/** Clear stack */
|
||||||
void clear() { stackTop = NULL; }
|
void clear() { stackTop = nullptr; }
|
||||||
|
|
||||||
/** Overload "=" the assignment operator */
|
/** Overload "=" the assignment operator */
|
||||||
stack<Type> &operator=(const stack<Type> &otherStack) {
|
stack<Type> &operator=(const stack<Type> &otherStack) {
|
||||||
node<Type> *newNode, *current, *last;
|
node<Type> *newNode, *current, *last;
|
||||||
|
|
||||||
/* If stack is no empty, make it empty */
|
/* If stack is no empty, make it empty */
|
||||||
if (stackTop != NULL) {
|
if (stackTop != nullptr) {
|
||||||
stackTop = NULL;
|
stackTop = nullptr;
|
||||||
}
|
}
|
||||||
if (otherStack.stackTop == NULL) {
|
if (otherStack.stackTop == nullptr) {
|
||||||
stackTop = NULL;
|
stackTop = nullptr;
|
||||||
} else {
|
} else {
|
||||||
current = otherStack.stackTop;
|
current = otherStack.stackTop;
|
||||||
stackTop = new node<Type>;
|
stackTop = new node<Type>;
|
||||||
stackTop->data = current->data;
|
stackTop->data = current->data;
|
||||||
stackTop->next = NULL;
|
stackTop->next = nullptr;
|
||||||
last = stackTop;
|
last = stackTop;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
/* Copy the remaining stack */
|
/* Copy the remaining stack */
|
||||||
while (current != NULL) {
|
while (current != nullptr) {
|
||||||
newNode = new node<Type>;
|
newNode = new node<Type>;
|
||||||
newNode->data = current->data;
|
newNode->data = current->data;
|
||||||
newNode->next = NULL;
|
newNode->next = nullptr;
|
||||||
last->next = newNode;
|
last->next = newNode;
|
||||||
last = newNode;
|
last = newNode;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
@ -105,7 +144,7 @@ class stack {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
node<Type> *stackTop; /**< Pointer to the stack */
|
node<Type> *stackTop; /**< Pointer to the stack */
|
||||||
int size;
|
int size; ///< size of stack
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DATA_STRUCTURES_STACK_H_
|
#endif // DATA_STRUCTURES_STACK_H_
|
||||||
|
Loading…
x
Reference in New Issue
Block a user