From 46717def3104831d416999e7e4f8956577d39657 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 27 Mar 2020 10:59:28 -0400 Subject: [PATCH] Add Node class, work on singly LinkedList class and examples --- plates/cpp-datastruct/CMakeLists.txt | 31 +++++ plates/cpp-datastruct/apps/CMakeLists.txt | 15 +++ plates/cpp-datastruct/apps/driver.cpp | 71 +++++++++++ .../cpp-datastruct/include/lib-datastruct.h | 34 +++-- .../include/lib-datastruct.h.in | 8 ++ plates/cpp-datastruct/src/CMakeLists.txt | 12 ++ plates/cpp-datastruct/src/lib-datastruct.cpp | 117 ++++++++++++++++++ 7 files changed, 281 insertions(+), 7 deletions(-) create mode 100644 plates/cpp-datastruct/apps/CMakeLists.txt create mode 100644 plates/cpp-datastruct/include/lib-datastruct.h.in create mode 100644 plates/cpp-datastruct/src/CMakeLists.txt diff --git a/plates/cpp-datastruct/CMakeLists.txt b/plates/cpp-datastruct/CMakeLists.txt index 0619e30..d4a8136 100644 --- a/plates/cpp-datastruct/CMakeLists.txt +++ b/plates/cpp-datastruct/CMakeLists.txt @@ -30,3 +30,34 @@ target_include_directories(lib-ds PUBLIC ${INCLUDE_DIR}) # configure_file(${INCLUDE_DIR}/lib-datastruct.h.in ${CMAKE_BINARY_DIR}/generated/lib-datastruct.h) # include_directories( ${CMAKE_BINARY_DIR}/generated/ ) + +# # Location of our header files +# # include_directories(./include) +# include_directories(${PROJECT_BINARY_DIR}) +# include_directories( ${CMAKE_BINARY_DIR}/generated/ ) + + +# # Additional directories to expect a CMakeLists.txt +# # add_subdirectory(src) +# # add_subdirectory(apps) + +# # Configure includes to consider cmake variables +# configure_file(${INCLUDE_DIR}/lib-datastruct.h.in ${CMAKE_BINARY_DIR}/generated/lib-datastruct.h) + +# # /apps/CMakeLists.txt +# # Create a variable to reference any sources to add +# set(LIB_DS_SRC ${SRC_DIR}/lib-datastruct.cpp) +# # Create our library +# add_library(lib-datastruct ${LIB_DS_SRC}) +# # Point the library to directories containing any includes it might need +# target_include_directories(lib-datastruct PUBLIC "${INCLUDE_DIR}") + +# # /src/CMakeLists.txt +# # Create a variable to reference our driver program source code +# set(DRIVER_SRC ${APPS_DIR}/driver.cpp) +# # Add the executable to the build list +# add_executable(Driver ${DRIVER_SRC}) +# # Link custom libraries to our executable +# target_link_libraries(Driver lib-datastruct) + +# # target_include_directories(Driver PUBLIC "${INCLUDE_DIR}") diff --git a/plates/cpp-datastruct/apps/CMakeLists.txt b/plates/cpp-datastruct/apps/CMakeLists.txt new file mode 100644 index 0000000..d638b84 --- /dev/null +++ b/plates/cpp-datastruct/apps/CMakeLists.txt @@ -0,0 +1,15 @@ +############################################################################### +## Author: Shaun reserved ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## + +# # Create a variable to reference our source code +# set(DRIVER_SRC driver.cpp) + +# # Add the executable to the build list +# add_executable(driver ${DRIVER_SRC}) + +# # Link custom libraries to our executable +# target_link_libraries(driver lib-datastruct) diff --git a/plates/cpp-datastruct/apps/driver.cpp b/plates/cpp-datastruct/apps/driver.cpp index 73e6592..b35d70d 100644 --- a/plates/cpp-datastruct/apps/driver.cpp +++ b/plates/cpp-datastruct/apps/driver.cpp @@ -4,6 +4,8 @@ // StackArray menu void Stack(); +void List(); + // Main menu int main () { @@ -27,6 +29,7 @@ int main () break; case LISTS: // 1 + List(); break; case STACKS: // 2 @@ -45,6 +48,74 @@ int main () return 0; } +// LinkedList menu +void List() +{ + enum LIST_OPS { + EXIT, APPEND, PUSH, DEQUEUE, POP, DISPLAY + }; + bool exit = false; + int choice; + char val; + LinkedList list; + + while(!exit) { + std::cout << "\n##### LinkedList Menu #####\n" + << "Enter a choice below...\n\t0. Exit" + << "\n\t1. Append\n\t2. Push\n\t3. Dequeue\n\t4. Pop" + << "\n\t5. Display\n\t6. Find\n\t7. Remove\n"; + std::cin >> choice; + std::cin.clear(); + switch(choice) { + case EXIT: // 0 + exit = true; + break; + + case APPEND: // 1 + std::cout << "Enter a character to append to our list: "; + std::cin >> val; + std::cin.clear(); + list.Append(val); + break; + + case PUSH: // 2 + std::cout << "Enter a character to push to our list: "; + std::cin >> val; + std::cin.clear(); + list.Push(val); + break; + + case DEQUEUE: // 3 + std::cout << "Enter a value to remove from the list: "; + std::cin >> val; + std::cin.clear(); + list.Remove(val); + break; + + case POP: // 4 + // list.Pop(); + list.Append('a'); + Node *tem = list.Find('a'); + list.Remove('a'); + if (tem != NULL) { + std::cout << "Found [" << tem->data << "] within the list\n"; + } + delete tem; + break; + + case DISPLAY: // 5 + list.Display(); + break; + + default: + std::cout << "Invalid option selected\n"; + break; + } + } + + return; +} + // StackArray menu void Stack() { diff --git a/plates/cpp-datastruct/include/lib-datastruct.h b/plates/cpp-datastruct/include/lib-datastruct.h index b9c4772..489204a 100644 --- a/plates/cpp-datastruct/include/lib-datastruct.h +++ b/plates/cpp-datastruct/include/lib-datastruct.h @@ -3,6 +3,33 @@ #include +class Node { + public: + Node() : next(NULL) {}; + Node(char val) : data(val), next(NULL) {}; + char data; + Node *next; +}; + +// Linked list + +class LinkedList { + + public: + LinkedList() : head(NULL), tail(NULL) {}; + void Append(char val); + void Push(char val); + void Remove(char val); + void Replace(char remove, char replace); + void Display() const; + bool isEmpty() const; + Node* Find(char val) const; + private: + Node *head, *tail; +}; + +// Array based stack + class StackArray { public: StackArray() : top(EMPTY) {}; @@ -14,13 +41,6 @@ class StackArray { private: enum { EMPTY=-1, MAX=10 }; - class Node { - public: - Node() : next(NULL) {}; - Node(char val) : data(val), next(NULL) {}; - char data; - Node* next; - }; Node stack[MAX]; int top; }; diff --git a/plates/cpp-datastruct/include/lib-datastruct.h.in b/plates/cpp-datastruct/include/lib-datastruct.h.in new file mode 100644 index 0000000..5e1f686 --- /dev/null +++ b/plates/cpp-datastruct/include/lib-datastruct.h.in @@ -0,0 +1,8 @@ +// #ifndef LDS_H +// #define LDS_H + +#define DS_VERSION_MINOR @DataStruct_VERSION_MINOR@ +#define DS_VERSION_MAJOR @DataStruct_VERSION_MAJOR@ +#define DS_VERSION @DataStruct_VERSION@ + +// #endif diff --git a/plates/cpp-datastruct/src/CMakeLists.txt b/plates/cpp-datastruct/src/CMakeLists.txt new file mode 100644 index 0000000..5c38076 --- /dev/null +++ b/plates/cpp-datastruct/src/CMakeLists.txt @@ -0,0 +1,12 @@ +############################################################################### +## Author: Shaun reserved ## +## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## + +# # Create a variable to reference any sources to add +# set(LIB_DS_SRC lib-datastruct.cpp) + +# # Create our library +# add_library(lib-datastruct ${LIB_DS_SRC}) diff --git a/plates/cpp-datastruct/src/lib-datastruct.cpp b/plates/cpp-datastruct/src/lib-datastruct.cpp index f52cc0b..b717103 100644 --- a/plates/cpp-datastruct/src/lib-datastruct.cpp +++ b/plates/cpp-datastruct/src/lib-datastruct.cpp @@ -1,5 +1,122 @@ #include +// LinkedList + +void LinkedList::Append(char val) +{ + Node *temp = new Node(val); + + if (isEmpty()) { + this->tail = this->head = temp; + // head->next = tail; + return; + } + else { + tail->next = temp; + tail = temp; + return; + } +} + +void LinkedList::Push(char val) +{ + Node *temp = new Node(val); + + if (isEmpty()) + { + this->head = this->tail = temp; + return; + } + else { + temp->next = head; + this->head = temp; + return; + } +} + +void LinkedList::Remove(char val) +{ + Node *foundNode = new Node; + + if (isEmpty()) { + // if list is empty + std::cout << "Error: List is empty\n"; + return; + } + else if (this->head->data == val) { + // check the head node, return if found val + foundNode = this->head; + std::cout << "[" << foundNode->data << "] has been removed from our list\n"; + this->head = foundNode->next; + delete foundNode; + // Does this set head to NULL? + // this->head = NULL; + return; + } + + Node *temp = head; + while (temp->next->data != val) { + temp = temp->next; + if (temp->next == NULL) { + std::cout << "Value [" << val << "] not found in our list\n"; + return; + } + } + foundNode = temp->next; + // If we made it our of the loop, we found val at the next node + temp->next = foundNode->next; + std::cout << "[" << foundNode->data << "] has been removed from our list\n"; + delete foundNode; + return; +} + +void LinkedList::Replace(char remove, char replace) +{ + return; +} + +Node* LinkedList::Find(char val) const +{ + Node *foundNode(NULL); + Node *temp = head; + + if(isEmpty()) { + std::cout << "Error: The list is empty\n"; + return foundNode; + } + + bool found = false; + + while (!found && temp->next != NULL) { + if(temp->next->data == val) { + found = true; + foundNode = temp->next; + std::cout << "[" << foundNode->data << "] has been found in our list\n"; + // temp->next = foundNode->next; + // delete foundNode; + } + temp = temp->next; + } + return foundNode; +} + +void LinkedList::Display() const +{ + Node *temp = head; + while (temp != NULL) { + std::cout << "[" << temp->data << "] | "; + temp = temp->next; + } + return; +} + +bool LinkedList::isEmpty() const +{ + return this->head == NULL; +} + +// StackArray + void StackArray::Push(char val) {