RD of circular singly linked list

This commit is contained in:
Shaun Reed 2020-04-30 15:01:14 -04:00
parent f37e863e3e
commit 87b3f5a394
4 changed files with 211 additions and 161 deletions

View File

@ -5,15 +5,15 @@ CXXFLAGS=-g -Wall
# Driver # Driver
############################################################################### ###############################################################################
driver: driver.cpp singlelist.o driver: driver.cpp circlesinglelist.o
${CXX} ${CXXFLAGS} driver.cpp singlelist.o -o driver ${CXX} ${CXXFLAGS} driver.cpp circlesinglelist.o -o driver
############################################################################### ###############################################################################
# SingleList # CircleSingleList
############################################################################### ###############################################################################
singlelist.o: singlelist.cpp singlelist.h circlesinglelist.o: circlesinglelist.cpp circlesinglelist.h
${CXX} ${CXXFLAGS} -c singlelist.cpp -o singlelist.o ${CXX} ${CXXFLAGS} -c circlesinglelist.cpp -o circlesinglelist.o
############################################################################### ###############################################################################
# Clean # Clean

View File

@ -1,64 +1,66 @@
/*############################################################################# /*#############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a singly linked list ## ## About: An example of a circular singly linked list ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
############################################################################## ##############################################################################
## singlelist.cpp ## circlesinglelist.cpp
*/ */
#include "singlelist.h" #include "circlesinglelist.h"
/****************************************************************************** /******************************************************************************
* Constructors, Destructors, Operators * Constructors, Destructors, Operators
*****************************************************************************/ *****************************************************************************/
/** copy constructor /** copy constructor
* @brief Construct a new SingleList::SingleList object from an existing one * @brief Construct a new CircleSingleList::CircleSingleList object from an existing one
* *
* @param rhs SingleList object * @param rhs CircleSingleList object
*/ */
SingleList::SingleList(const SingleList& rhs) CircleSingleList::CircleSingleList(const CircleSingleList& rhs)
{ {
Node *cp = rhs.head; Node *cp = rhs.tail;
Node *tempHead; Node *tempTail;
if (cp == NULL) head = NULL; if (cp == NULL) tail = NULL;
else { else {
head = new Node(cp->data); tail = new Node(cp->data);
tempHead = head; tempTail = tail;
while (cp->next != NULL) { while (cp->next != NULL) {
cp = cp->next; cp = cp->next;
head->next = new Node(cp->data); tail->next = new Node(cp->data);
head = head->next; tail = tail->next;
} }
head = tempHead; tail = tempTail;
} }
} }
/** operator= /** operator=
* @brief Assign two SingleList objects equal using copy constr and class destr * @brief Deep copy of the rhs CircleSingleList object
* Pass the rhs by value to create local copy, swap its contents * Pass rhs by value to create local copy, swap its data with our tail
* Destructor called on previous SingleList data at the end of this scope * Swapped local copy now with previous tail data deleted at end of scope using destr
* *
* @param rhs SingleList object passed by value * @param rhs CircleSingleList object
* @return SingleList A deep copy of the rhs SingleList object * @return CircleSingleList& A deep copy of the rhs CircleSingleList
*/ */
SingleList SingleList::operator=(SingleList rhs) CircleSingleList CircleSingleList::operator=(CircleSingleList rhs)
{ {
if (this == &rhs) return *this; if (this == &rhs) return *this;
std::swap(head, rhs.head); else std::swap(tail, rhs.tail);
return *this; return *this;
} }
/** destructor /** destructor
* @brief Destroy the SingleList::SingleList object * @brief Destroy the CircleSingleList::CircleSingleList object
*/ */
SingleList::~SingleList() CircleSingleList::~CircleSingleList()
{ {
makeEmpty(); makeEmpty();
} }
/****************************************************************************** /******************************************************************************
* Public Member Functions * Public Member Functions
*****************************************************************************/ *****************************************************************************/
@ -68,11 +70,10 @@ SingleList::~SingleList()
* *
* @param x The value to be inserted * @param x The value to be inserted
*/ */
bool SingleList::insert(int val) bool CircleSingleList::insert(int val)
{ {
bool inserted = insert(val, head); bool inserted = insert(val, tail);
if (inserted) if (inserted) std::cout << "[" << val << "] was inserted into the list\n";
std::cout << "[" << val << "] was inserted into the list\n";
else std::cout << "[" << val << "] could not be inserted into the list\n"; else std::cout << "[" << val << "] could not be inserted into the list\n";
return inserted; return inserted;
} }
@ -84,11 +85,15 @@ bool SingleList::insert(int val)
* @param key The value to search for to determine insert location * @param key The value to search for to determine insert location
* @param val The value to be inserted into the list * @param val The value to be inserted into the list
*/ */
bool SingleList::insert(int val, int key) bool CircleSingleList::insert(int val, int key)
{ {
bool inserted = insert(val, key, head); if (isEmpty()) {
if (inserted) std::cout << "Cannot insert at an existing value using an empty list...\n";
std::cout << "[" << val << "] was inserted into the list\n"; return false;
}
bool inserted = insert(val, key, tail);
if (inserted) std::cout << "[" << val << "] was inserted into the list\n";
else std::cout << "[" << val << "] could not be inserted into the list\n"; else std::cout << "[" << val << "] could not be inserted into the list\n";
return inserted; return inserted;
} }
@ -100,11 +105,15 @@ bool SingleList::insert(int val, int key)
* @return true If the value was removed from the list * @return true If the value was removed from the list
* @return false If the value was not removed from the list * @return false If the value was not removed from the list
*/ */
bool SingleList::remove(int val) bool CircleSingleList::remove(int val)
{ {
bool removed = remove(val, head); if (isEmpty()) {
if (removed) std::cout << "Cannot remove any values from an empty list...\n";
std::cout << "[" << val << "] was removed from the list\n"; return false;
}
bool removed = remove(val, tail);
if (removed) std::cout << "[" << val << "] was removed from the list\n";
else std::cout << "[" << val << "] could not be removed from the list\n"; else std::cout << "[" << val << "] could not be removed from the list\n";
return removed; return removed;
} }
@ -117,77 +126,94 @@ bool SingleList::remove(int val)
* @return true If the key has been replaced in the list by val * @return true If the key has been replaced in the list by val
* @return false If the key has not been replaced in the list by val * @return false If the key has not been replaced in the list by val
*/ */
bool SingleList::replace(int val, int key) bool CircleSingleList::replace(int val, int key)
{ {
bool replaced = replace(val, key, head); if (isEmpty()) {
if (replaced) std::cout << "Cannot replace any values from an empty list...\n";
std::cout << "[" << key << "] was replaced by [" << val << "] in the list\n"; return false;
else std::cout << "[" << key << "] could not be replaced by [" << val << "] in the list\n"; }
bool replaced = replace(val, key, tail);
if (replaced) std::cout << "[" << key << "] was replaced by "
<< "[" << val << "] in the list\n";
else std::cout << "[" << key << "] could not be replaced by "
<< "[" << val << "] in the list\n";
return replaced; return replaced;
} }
/** makeEmpty /** makeEmpty
* @brief Empty this SingleList object, deleting all associated Nodes * @brief Empty this CircleSingleList object, deleting all associated Nodes
*/ */
void SingleList::makeEmpty() void CircleSingleList::makeEmpty()
{ {
Node *nextNode, *temp; Node *nextNode, *temp;
if (head == NULL) return; // If the list is empty
nextNode = head->next; if (tail == NULL) return;
delete head;
head = NULL; // If the list has members other than itself, keep a pointer to them
while(nextNode != NULL) { if (tail != tail->next) nextNode = tail->next;
// Delete the list nodes until we hit the tail
while(nextNode != tail) {
// Save pointer to delete previous node
temp = nextNode; temp = nextNode;
// Move to the next node
nextNode = nextNode->next; nextNode = nextNode->next;
// Delete the previous node and set it to NULL
delete temp; delete temp;
temp = NULL; temp = NULL;
} }
// Always delete the tail node and set it to NULL
delete tail;
tail = NULL;
} }
/** isEmpty /** isEmpty
* @brief Determine if the SingleList is empty * @brief Determine if the CircleSingleList is empty
* *
* @return true If the SingleList::head is NULL * @return true If the CircleSingleList::tail is NULL
* @return false If the SingleList::head contains data * @return false If the CircleSingleList::tail contains data
*/ */
bool SingleList::isEmpty() const bool CircleSingleList::isEmpty() const
{ {
return head == NULL; return tail == NULL;
} }
/** peek /** peek
* @brief returns the value at the SingleList::head * @brief return the value at the head of the list
* *
* @return int The value held at the Node pointed to by SingleList::head * @return int The value held at the Node at the head of our linked list
*/ */
void SingleList::peek() const void CircleSingleList::peek() const
{ {
// If there is only one data member, a circular list node will point to itself
if (!isEmpty()) if (!isEmpty())
std::cout << "[" << head->data << "] is at the top of our list\n"; std::cout << "[" << tail->next->data << "] is at the top of our list\n";
else std::cout << "Nothing to peek, our list is empty...\n"; else std::cout << "Nothing to peek, our list is empty...\n";
} }
/** print /** print
* @brief Output the data held by the SingleList object * @brief Output the data held by the CircleSingleList object
* Calls to the private print() * Calls to the private print()
*/ */
void SingleList::print() const void CircleSingleList::print() const
{ {
if(!isEmpty()) print(head); // Print the list starting from the head node
if(!isEmpty()) print(tail->next);
else std::cout << "Nothing to print, our list is empty...\n"; else std::cout << "Nothing to print, our list is empty...\n";
} }
/** find /** find
* @brief Calls to the private member find() and handles return cases * @brief Calls to the private member find() and handles return cases
* *
* @param val The value to search for within our SingleList * @param val The value to search for within our CircleSingleList
* @return true If the value was found in this SingleList * @return true If the value was found in this CircleSingleList
* @return false If the value was not found in this SingleList * @return false If the value was not found in this CircleSingleList
*/ */
bool SingleList::find(int val) const bool CircleSingleList::find(int val) const
{ {
Node *result = find(val, head); Node *result = find(val, tail);
if( result == NULL) { if( result == NULL) {
std::cout << "[" << val << "] Was not found in our list\n"; std::cout << "[" << val << "] Was not found in our list\n";
return false; return false;
@ -197,6 +223,7 @@ bool SingleList::find(int val) const
return true; return true;
} }
/****************************************************************************** /******************************************************************************
* Private Member Functions * Private Member Functions
*****************************************************************************/ *****************************************************************************/
@ -205,17 +232,18 @@ bool SingleList::find(int val) const
* @brief Private member to handle inserting value into the list * @brief Private member to handle inserting value into the list
* *
* @param val Value to be inserted * @param val Value to be inserted
* @param head The head of the list to insert the value into * @param tail The tail of the list to insert the value into
* @return true If the value was inserted * @return true If the value was inserted
* @return false If the value could not be inserted * @return false If the value could not be inserted
*/ */
bool SingleList::insert(int val, Node *&head) bool CircleSingleList::insert(int val, Node *&tail)
{ {
Node *newNode = new Node(val); Node *newNode = new Node(val);
// If the list is not empty, update next pointer to head node // If the list is not empty, update next pointer to head node
if (!isEmpty()) newNode->next = head; if (!isEmpty()) newNode->next = tail->next;
else tail = newNode;
// Always set head to our newNode // Always set head to our newNode
head = newNode; tail->next = newNode;
return true; return true;
} }
@ -224,22 +252,25 @@ bool SingleList::insert(int val, Node *&head)
* *
* @param val Value to be inserted * @param val Value to be inserted
* @param key Key value to search for within the list * @param key Key value to search for within the list
* @param head Head node of the list to insert to * @param tail Tail node of the list to insert to
* @return true If the value was inserted * @return true If the value was inserted
* @return false If the value was not inserted * @return false If the value was not inserted
*/ */
bool SingleList::insert(int val, int key, Node *&head) bool CircleSingleList::insert(int val, int key, Node *&tail)
{ {
Node *newNode = new Node(val); Node *newNode = new Node(val);
if (isEmpty()) return false; if (isEmpty()) return false;
// Let insert() handle inserting at the head // Let insert() handle inserting at the head
else if (head->data == key) return insert(val); else if (tail->next->data == key) return insert(val);
Node *keyNode = findPrevious(key, head); Node *keyNode = findPrevious(key);
// If there was no keyNode found, the key does is not in our list // If there was no keyNode found, the key does is not in our list
// Don't insert anything, return false and let caller decide whats next // Don't insert anything, return false and let caller decide whats next
if (keyNode == NULL) return false; if (keyNode == NULL) return false;
// Set the newNode to come BEFORE our keyNode // Set the newNode to come between the keyNode and the Node we are inserting at
// [keyNode]->[next]
// [keyNode]->[newNode]->[next]
// Where (keyNode->next->data == key) and keyNode is given by findPrevious()
newNode->next = keyNode->next; newNode->next = keyNode->next;
keyNode->next = newNode; keyNode->next = newNode;
return true; return true;
@ -249,24 +280,43 @@ bool SingleList::insert(int val, int key, Node *&head)
* @brief Private member to remove values from the list * @brief Private member to remove values from the list
* *
* @param val Value to be removed * @param val Value to be removed
* @param head Head of the list to remove the value from * @param tail Tail of the list to remove the value from
* @return true If the value has been removed from the list * @return true If the value has been removed from the list
* @return false If the value has not been removed from the list * @return false If the value has not been removed from the list
*/ */
bool SingleList::remove(int val, Node *&head) bool CircleSingleList::remove(int val, Node *&tail)
{ {
Node *exile, *tempHead;
Node *prevNode = findPrevious(val);
// If findPrevious returns NULL, the value was not found
if (prevNode == NULL) return false;
if (head == NULL) return false; // If the node found is the only member of the list
else if (head->data == val) { if (prevNode == prevNode->next) {
head = head->next; // Delete, reset list to NULL
delete tail;
tail = NULL;
return true; return true;
} }
// If the node to remove is the tail and the list has > 1 members
else if (prevNode->next == tail) {
tempHead = tail->next;
exile = prevNode->next;
// Create the new tail
tail = prevNode;
// Link the head Node, skipping over the exile Node
tail->next = tempHead;
}
else {
// Get a ptr to the member we are removing from the list
exile = prevNode->next;
// Relink the list, skipping the exile node
prevNode->next = prevNode->next->next;
}
Node *prevNode = findPrevious(val, head); // Delete the dangling ptr to the exile Node we removed from the list
if (prevNode == NULL) return false; delete exile;
Node *gtfo = prevNode->next; exile = NULL;
prevNode->next = prevNode->next->next;
delete gtfo;
return true; return true;
} }
@ -275,13 +325,13 @@ bool SingleList::remove(int val, Node *&head)
* *
* @param val Value to insert into the list * @param val Value to insert into the list
* @param key Value to be replaced within the list * @param key Value to be replaced within the list
* @param head Head of the list to replace the value * @param tail Tail of the list to replace the value
* @return true If the key has been replaced by val within the list * @return true If the key has been replaced by val within the list
* @return false If the key has not been replaced by val within the list * @return false If the key has not been replaced by val within the list
*/ */
bool SingleList::replace(int val, int key, Node *&head) bool CircleSingleList::replace(int val, int key, Node *&tail)
{ {
Node *replacee = find(key, head); Node *replacee = find(key, tail);
if (replacee == NULL) return false; if (replacee == NULL) return false;
replacee->data = val; replacee->data = val;
return true; return true;
@ -290,19 +340,19 @@ bool SingleList::replace(int val, int key, Node *&head)
/** find /** find
* @brief Find and return a Node which contains the given value * @brief Find and return a Node which contains the given value
* *
* @param val The value to search for within our SingleList * @param val The value to search for within our CircleSingleList
* @return SingleList::Node* A pointer to the Node containing the search value * @param start The Node to begin the search at within the linked list, usually the head node
* @return CircleSingleList::Node* A pointer to the Node containing the search value
*/ */
SingleList::Node* SingleList::find(int val, Node *start) const CircleSingleList::Node* CircleSingleList::find(int val, Node *start) const
{ {
// If given a NULL list, return NULL // Check the first Node, return if it is the value requested
// If given a head which contains the requested value, return the foundNode if (start->data == val) return start;
if (start == NULL || start->data == val) return start; Node *foundNode = start->next;
// Move through the list, checking each member until we reach the start Node
Node *foundNode = start; while (foundNode != start) {
while (foundNode->next != NULL) {
foundNode = foundNode->next;
if (foundNode->data == val) return foundNode; if (foundNode->data == val) return foundNode;
foundNode = foundNode->next;
} }
// If we have not yet returned a foundNode, the key is not in our list // If we have not yet returned a foundNode, the key is not in our list
return NULL; return NULL;
@ -311,33 +361,38 @@ SingleList::Node* SingleList::find(int val, Node *start) const
/** findPrevious /** findPrevious
* @brief Find and return the Node before a given value * @brief Find and return the Node before a given value
* *
* @param val The value to search for within our SingleList * @param val The value to search for within our CircleSingleList
* @return SingleList::Node* A pointer to the Node previous to the given value * @return CircleSingleList::Node* A pointer to the Node previous to the given value
*/ */
SingleList::Node* SingleList::findPrevious(int val, Node *start) const CircleSingleList::Node* CircleSingleList::findPrevious(int val) const
{ {
if (isEmpty()) return NULL; if (isEmpty()) return NULL;
else if (start->data == val) return start; else if (tail->next->data == val) return tail;
Node *temp = start; // Create temp ptr to the head of our list
while (temp->next != NULL) { Node *tempHead = tail->next;
if (temp->next->data == val) return temp; // Move through the list until we reach the head node again
temp = temp->next; while (tempHead->next != tail->next) {
// Check the value within the next node, if found return it
if (tempHead->next->data == val) return tempHead;
else tempHead = tempHead->next;
} }
return NULL; return NULL;
} }
/** print /** print
* @brief Output the contents of a SingleList from the given Node to NULL * @brief Output the contents of a CircleSingleList until we hit a matching node
* *
* @param start The Node to begin traversing output from * @param start The Node to begin traversing output from
*/ */
void SingleList::print(Node *start) const void CircleSingleList::print(Node *start) const
{ {
Node *temp = start; Node *temp = start;
std::cout << "List Contents: "; std::cout << "List Contents: " << temp->data << " | ";
while (temp != NULL) { temp = temp->next;
// Until we reach the start node again, print the list contents
while (temp != start) {
std::cout << temp->data << " | "; std::cout << temp->data << " | ";
temp = temp->next; temp = temp->next;
} }

View File

@ -1,29 +1,24 @@
/*############################################################################# /*#############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a singly linked list ## ## About: An example of a circular singly linked list ##
## ##
## Structure: Remove: Insert: Insert at: Replace: ##
## o-o-o-o-o-o o-o--x-->o-o-o o o o ##
## | /| / \ ##
## o-o-o-o-o-o o-o~o-o-o-o o-o~x~o-o-o ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
############################################################################## ##############################################################################
## singlelist.h ## circlesinglelist.h
*/ */
#ifndef LINKEDLIST_H #ifndef CIRCLESINGLELIST_H
#define LINKEDLIST_H #define CIRCLESINGLELIST_H
#include <iostream> #include <iostream>
class SingleList{ class CircleSingleList{
public: public:
SingleList() : head(NULL) {}; CircleSingleList() : tail(NULL) {};
SingleList(const SingleList& rhs); CircleSingleList(const CircleSingleList& rhs);
SingleList operator=(SingleList rhs); CircleSingleList operator=(CircleSingleList rhs);
~SingleList(); ~CircleSingleList();
bool insert(int val); bool insert(int val);
bool insert(int val, int key); bool insert(int val, int key);
bool remove(int val); bool remove(int val);
@ -39,15 +34,15 @@ class SingleList{
int data; int data;
Node *next; Node *next;
Node(): data(), next(NULL) {}; Node(): data(), next(NULL) {};
Node(int val): data(val), next(NULL) {}; Node(int val): data(val), next(this) {};
}; };
Node *head; Node *tail;
bool insert(int val, Node *&head); bool insert(int val, Node *&tail);
bool insert(int val, int key, Node *&head); bool insert(int val, int key, Node *&tail);
bool remove(int val, Node *&head); bool remove(int val, Node *&tail);
bool replace(int val, int key, Node *&head); bool replace(int val, int key, Node *&tail);
Node* find(int val, Node *start) const; Node* find(int val, Node *start) const;
Node* findPrevious(int val, Node *start) const; Node* findPrevious(int val) const;
void print(Node *start) const; void print(Node *start) const;
}; };

View File

@ -1,14 +1,14 @@
/*############################################################################# /*#############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: A driver program to test a singly linked list ## ## About: A driver program to test a circular singly linked list ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
############################################################################## ##############################################################################
## driver.cpp ## driver.cpp
*/ */
#include "singlelist.h" #include "circlesinglelist.h"
#include <iostream> #include <iostream>
enum OPS { enum OPS {
@ -19,14 +19,14 @@ int main()
{ {
std::cout << "Driver: \n"; std::cout << "Driver: \n";
SingleList testList; CircleSingleList testList;
bool exit = false; bool exit = false;
int choice = -1; int choice = -1;
int val, key; int val, key;
while (!exit) while (!exit)
{ {
std::cout << "##### Singly Linked List Menu #####\n\t0. Exit" std::cout << "##### Circular Singly Linked List Menu #####\n\t0. Exit"
<< "\n\t1. Insert\n\t2. Insert at\n\t3. Empty list\n\t4. Peek top of list" << "\n\t1. Insert\n\t2. Insert at\n\t3. Empty list\n\t4. Peek top of list"
<< "\n\t5. Print list\n\t6. Find\n\t7. Remove\n\t8. Replace\n"; << "\n\t5. Print list\n\t6. Find\n\t7. Remove\n\t8. Replace\n";
std::cin >> choice; std::cin >> choice;