From f0257c720dd16b7a7d8de676a01f409971675c0f Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Thu, 11 Jun 2020 18:32:53 -0400 Subject: [PATCH] RD of stack using linked list --- cpp/datastructs/stacklist/Makefile | 23 +++ cpp/datastructs/stacklist/driver.cpp | 68 +++++++ cpp/datastructs/stacklist/stacklist.cpp | 233 ++++++++++++++++++++++++ cpp/datastructs/stacklist/stacklist.h | 44 +++++ 4 files changed, 368 insertions(+) create mode 100644 cpp/datastructs/stacklist/Makefile create mode 100644 cpp/datastructs/stacklist/driver.cpp create mode 100644 cpp/datastructs/stacklist/stacklist.cpp create mode 100644 cpp/datastructs/stacklist/stacklist.h diff --git a/cpp/datastructs/stacklist/Makefile b/cpp/datastructs/stacklist/Makefile new file mode 100644 index 0000000..a0e74d6 --- /dev/null +++ b/cpp/datastructs/stacklist/Makefile @@ -0,0 +1,23 @@ +CXX=g++ +CXXFLAGS=-g -Wall + +############################################################################### +# Driver +############################################################################### + +driver: driver.cpp stacklist.o + ${CXX} ${CXXFLAGS} driver.cpp stacklist.o -o driver + +############################################################################### +# StackList +############################################################################### + +stacklist.o: stacklist.cpp stacklist.h + ${CXX} ${CXXFLAGS} -c stacklist.cpp -o stacklist.o + +############################################################################### +# Clean +############################################################################### + +clean: + rm -f *.o driver diff --git a/cpp/datastructs/stacklist/driver.cpp b/cpp/datastructs/stacklist/driver.cpp new file mode 100644 index 0000000..f97b9a5 --- /dev/null +++ b/cpp/datastructs/stacklist/driver.cpp @@ -0,0 +1,68 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: A driver program to test a stack implementation using linked lists ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +## driver.cpp +*/ + +#include "stacklist.h" +#include + +enum OPS { + EXIT, PUSH, POP, TOP, PRINT, EMPTY +}; + +int main() +{ + std::cout << "Driver: \n"; + + StackList testList; + bool exit = false; + int choice = -1; + int val; + + while (!exit) + { + std::cout << "##### Stacks Using Linked Lists Menu #####\n\t0. Exit" + << "\n\t1. Push\n\t2. Pop\n\t3. Top\n\t4. Print" + << "\n\t5. Empty\n\t"; + std::cin >> choice; + std::cin.clear(); + switch (choice) { + case EXIT: + exit = true; + break; + + case PUSH: + std::cout << "Enter a value to push to our stack: "; + std::cin >> val; + std::cin.clear(); + testList.push(val); + break; + + case POP: + testList.pop(); + break; + + case TOP: + testList.top(); + break; + + case PRINT: + testList.print(); + break; + + case EMPTY: + testList.makeEmpty(); + break; + + default: + std::cout << "Invalid entry...\n"; + break; + } + } +} + diff --git a/cpp/datastructs/stacklist/stacklist.cpp b/cpp/datastructs/stacklist/stacklist.cpp new file mode 100644 index 0000000..df2cbd7 --- /dev/null +++ b/cpp/datastructs/stacklist/stacklist.cpp @@ -0,0 +1,233 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: An example of a stack implementation using linked lists ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +## stacklist.cpp +*/ + +#include "stacklist.h" + + +/****************************************************************************** + * Constructors, Destructors, Operators + *****************************************************************************/ + +/** copy constructor + * @brief Construct a new StackList::StackList object from an existing one + * Creates a new deep copy of a given StackList + * + * @param rhs StackList object + */ +StackList::StackList(const StackList& rhs) +{ + Node *cp = rhs.head; + Node *tempHead; + // If we are copying from an empty stack, create a new StackList with a NULL head + if (cp == NULL) head = NULL; + else { + // If the stack has data, initialize a new stack with the head data + head = new Node(cp->data); + // Keep a temporary head node so we can return to it later + tempHead = head; + while (cp->next != NULL) { + // Until we hit an end, create new nodes with the next node data + cp = cp->next; + head->next = new Node(cp->data); + head = head->next; + } + head = tempHead; + } +} + +/** operator= + * @brief Assign two StackList objects equal using copy constr and class destr + * Pass the rhs by value to create local copy, swap its contents + * Destructor called on previous StackList data at the end of this scope + * + * @param rhs StackList object passed by value + * @return StackList A deep copy of the rhs StackList object + */ +StackList StackList::operator=(StackList rhs) +{ + if (this == &rhs) return *this; + // Swap the pointers, moving the previous head data to the local variable rhs + std::swap(head, rhs.head); + return *this; +} + +/** destructor + * @brief Destroy the StackList::StackList object + */ +StackList::~StackList() +{ + makeEmpty(head); +} + + +/****************************************************************************** + * Public Member Functions + *****************************************************************************/ + +/** push + * @brief Push a value to the head of our linked list + * + * @param x The value to be inserted + */ +bool StackList::push(int val) +{ + bool inserted = push(val, head); + if (inserted) + std::cout << "[" << val << "] was inserted into the stack\n"; + else std::cout << "[" << val << "] could not be inserted into the stack\n"; + return inserted; +} + +/** pop + * @brief returns the value at the StackList::head + * + * @return int The value held at the Node pointed to by StackList::head + */ +int StackList::pop() +{ + if (!isEmpty()) + std::cout << "[" << pop(head) << "] has been popped from our stack\n"; + else std::cout << "Nothing to pop, our stack is empty...\n"; + // If the stack has data we return it, otherwise we return the smallest possible int (error) + return (!isEmpty()) ? head->data : INT32_MIN; +} + +/** top + * @brief returns the value at the StackList::head + * + * @return int The value held at the Node pointed to by StackList::head + */ +int StackList::top() const +{ + if (!isEmpty()) + std::cout << "[" << head->data << "] is at the top of our stack\n"; + else std::cout << "Our stack is empty...\n"; + // If the stack has data we return it, otherwise we return the smallest possible int (error) + return (!isEmpty()) ? head->data : INT32_MIN; +} + +/** makeEmpty + * @brief Empty this StackList object, deleting all associated Nodes + */ +void StackList::makeEmpty() +{ + Node *nextNode, *temp; + + if (head == NULL) std::cout << "Our stack is empty...\n"; + else { + nextNode = head->next; + delete head; + head = NULL; + while(nextNode != NULL) { + temp = nextNode; + nextNode = nextNode->next; + delete temp; + temp = NULL; + } + + } +} + +/** isEmpty + * @brief Determine if the StackList is empty + * + * @return true If the StackList::head is NULL + * @return false If the StackList::head contains data + */ +bool StackList::isEmpty() const +{ + return head == NULL; +} + + +/** print + * @brief Output the data held by the StackList object + * Calls to the private print() + */ +void StackList::print() const +{ + if(!isEmpty()) print(head); + else std::cout << "Nothing to print, our stack is empty...\n"; +} + + +/****************************************************************************** + * Private Member Functions + *****************************************************************************/ + +/** push + * @brief Private member to handle inserting value into the stack + * + * @param val Value to be inserted + * @param head The head of the stack to push the value into + * @return true If the value was inserted + * @return false If the value could not be inserted + */ +bool StackList::push(int val, Node *&head) +{ + Node *newNode = new Node(val); + // If the stack is not empty, update next pointer to head node + if (!isEmpty()) newNode->next = head; + // Always set head to our newNode + head = newNode; + return true; +} + +int StackList::pop(Node *&head) +{ + // We already know the stack is not empty from public pop() + Node *temp = head; + int data = temp->data; + head = head->next; + delete temp; + return data; +} + +/** print + * @brief Output the contents of a StackList from the given Node to NULL + * + * @param start The Node to begin traversing output from + */ +void StackList::print(Node *start) const +{ + Node *temp = start; + std::cout << "Stack Contents: "; + while (temp != NULL) { + std::cout << temp->data << " | "; + temp = temp->next; + } + std::cout << std::endl; +} + +/** makeEmpty + * @brief Private member to empty this StackList object, deleting all associated Nodes + * Does not print any output. Avoids destructors printing to cout + * + * @param head The head of the stack to be deleted + * + */ +void StackList::makeEmpty(Node *&head) +{ + Node *nextNode, *temp; + + if (head == NULL) return; + else { + nextNode = head->next; + delete head; + head = NULL; + while(nextNode != NULL) { + temp = nextNode; + nextNode = nextNode->next; + delete temp; + temp = NULL; + } + + } +} diff --git a/cpp/datastructs/stacklist/stacklist.h b/cpp/datastructs/stacklist/stacklist.h new file mode 100644 index 0000000..e590811 --- /dev/null +++ b/cpp/datastructs/stacklist/stacklist.h @@ -0,0 +1,44 @@ +/*############################################################################# +## Author: Shaun Reed ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## About: An example of a stack implementation using linked lists ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +## stacklist.h +*/ + +#ifndef STACKLIST_H +#define STACKLIST_H + +#include + +class StackList { + public: + StackList() : head(NULL) {}; + StackList(const StackList& rhs); + StackList operator=(StackList rhs); + ~StackList(); + bool push(int val); + int pop(); + int top() const; + bool isEmpty() const; + void print() const; + void makeEmpty(); + + private: + struct Node { + int data; + Node *next; + Node(): data(), next(NULL) {}; + Node(int val): data(val), next(NULL) {}; + }; + Node *head; + bool push(int val, Node *&head); + int pop(Node *&head); + void print(Node *start) const; + void makeEmpty(Node *&head); + +}; + +#endif