From 95890fdb6642ba584c28368126344a44aa69e2f2 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 24 Jun 2020 10:26:55 -0400 Subject: [PATCH 1/4] create copy constructor --- data_structures/stack.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/data_structures/stack.h b/data_structures/stack.h index f4b8992e7..3957b4d8d 100644 --- a/data_structures/stack.h +++ b/data_structures/stack.h @@ -34,6 +34,36 @@ class stack { size = 0; } + /** Copy constructor*/ + explicit stack(const stack &other) { + node *newNode, *current, *last; + + /* If stack is no empty, make it empty */ + if (stackTop != NULL) { + stackTop = NULL; + } + if (otherStack.stackTop == NULL) { + stackTop = NULL; + } else { + current = otherStack.stackTop; + stackTop = new node; + stackTop->data = current->data; + stackTop->next = NULL; + last = stackTop; + current = current->next; + /* Copy the remaining stack */ + while (current != NULL) { + newNode = new node; + newNode->data = current->data; + newNode->next = NULL; + last->next = newNode; + last = newNode; + current = current->next; + } + } + size = otherStack.size; + } + /** Destructor */ ~stack() {} From 94a494edf59a99ec0457e7fa264e7d17b4c5c6ce Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 24 Jun 2020 10:27:23 -0400 Subject: [PATCH 2/4] replace NULL with nullptr --- data_structures/stack.h | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/data_structures/stack.h b/data_structures/stack.h index 3957b4d8d..31a5abe34 100644 --- a/data_structures/stack.h +++ b/data_structures/stack.h @@ -20,7 +20,7 @@ class stack { void display() { node *current = stackTop; std::cout << "Top --> "; - while (current != NULL) { + while (current != nullptr) { std::cout << current->data << " "; current = current->next; } @@ -30,7 +30,7 @@ class stack { /** Default constructor*/ stack() { - stackTop = NULL; + stackTop = nullptr; size = 0; } @@ -39,23 +39,23 @@ class stack { node *newNode, *current, *last; /* If stack is no empty, make it empty */ - if (stackTop != NULL) { - stackTop = NULL; + if (stackTop != nullptr) { + stackTop = nullptr; } - if (otherStack.stackTop == NULL) { - stackTop = NULL; + if (otherStack.stackTop == nullptr) { + stackTop = nullptr; } else { current = otherStack.stackTop; stackTop = new node; stackTop->data = current->data; - stackTop->next = NULL; + stackTop->next = nullptr; last = stackTop; current = current->next; /* Copy the remaining stack */ - while (current != NULL) { + while (current != nullptr) { newNode = new node; newNode->data = current->data; - newNode->next = NULL; + newNode->next = nullptr; last->next = newNode; last = newNode; current = current->next; @@ -68,7 +68,7 @@ class stack { ~stack() {} /** Determine whether the stack is empty */ - bool isEmptyStack() { return (stackTop == NULL); } + bool isEmptyStack() { return (stackTop == nullptr); } /** Add new item to the stack */ void push(Type item) { @@ -82,7 +82,7 @@ class stack { /** Return the top element of the stack */ Type top() { - assert(stackTop != NULL); + assert(stackTop != nullptr); return stackTop->data; } @@ -100,30 +100,30 @@ class stack { } /** Clear stack */ - void clear() { stackTop = NULL; } + void clear() { stackTop = nullptr; } /** Overload "=" the assignment operator */ stack &operator=(const stack &otherStack) { node *newNode, *current, *last; /* If stack is no empty, make it empty */ - if (stackTop != NULL) { - stackTop = NULL; + if (stackTop != nullptr) { + stackTop = nullptr; } - if (otherStack.stackTop == NULL) { - stackTop = NULL; + if (otherStack.stackTop == nullptr) { + stackTop = nullptr; } else { current = otherStack.stackTop; stackTop = new node; stackTop->data = current->data; - stackTop->next = NULL; + stackTop->next = nullptr; last = stackTop; current = current->next; /* Copy the remaining stack */ - while (current != NULL) { + while (current != nullptr) { newNode = new node; newNode->data = current->data; - newNode->next = NULL; + newNode->next = nullptr; last->next = newNode; last = newNode; current = current->next; From 631b50cede4ad0cc5ad350f88c2cc01280d54101 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 24 Jun 2020 10:37:15 -0400 Subject: [PATCH 3/4] update documetnation --- data_structures/stack.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/data_structures/stack.h b/data_structures/stack.h index 31a5abe34..429e0265f 100644 --- a/data_structures/stack.h +++ b/data_structures/stack.h @@ -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_ #define DATA_STRUCTURES_STACK_H_ #include #include -/* 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 struct node { - Type data; - node *next; + Type data; ///< data at current node + node *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 stack { public: @@ -135,7 +144,7 @@ class stack { private: node *stackTop; /**< Pointer to the stack */ - int size; + int size; ///< size of stack }; #endif // DATA_STRUCTURES_STACK_H_ From 9b01676b338ee75d6a2eb06574ed660d0ac9c5ea Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Wed, 24 Jun 2020 10:48:18 -0400 Subject: [PATCH 4/4] fixed function argument --- data_structures/stack.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/stack.h b/data_structures/stack.h index 429e0265f..483648046 100644 --- a/data_structures/stack.h +++ b/data_structures/stack.h @@ -44,7 +44,7 @@ class stack { } /** Copy constructor*/ - explicit stack(const stack &other) { + explicit stack(const stack &otherStack) { node *newNode, *current, *last; /* If stack is no empty, make it empty */