Add Node class, work on singly LinkedList class and examples

This commit is contained in:
Shaun Reed 2020-03-27 10:59:28 -04:00
parent 118488b97f
commit 46717def31
7 changed files with 281 additions and 7 deletions

View File

@ -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}")

View File

@ -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)

View File

@ -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()
{

View File

@ -3,6 +3,33 @@
#include <iostream>
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;
};

View File

@ -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

View File

@ -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})

View File

@ -1,5 +1,122 @@
#include <lib-datastruct.h>
// 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)
{