create copy constructor

This commit is contained in:
Krishna Vedala 2020-06-24 10:26:55 -04:00
parent d958eec03b
commit 95890fdb66
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -34,6 +34,36 @@ class stack {
size = 0;
}
/** Copy constructor*/
explicit stack(const stack &other) {
node<Type> *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<Type>;
stackTop->data = current->data;
stackTop->next = NULL;
last = stackTop;
current = current->next;
/* Copy the remaining stack */
while (current != NULL) {
newNode = new node<Type>;
newNode->data = current->data;
newNode->next = NULL;
last->next = newNode;
last = newNode;
current = current->next;
}
}
size = otherStack.size;
}
/** Destructor */
~stack() {}