klips/cpp/datastructs/circlesinglelist/circlesinglelist.cpp

402 lines
12 KiB
C++
Raw Normal View History

2020-04-30 18:59:05 +00:00
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
2020-04-30 19:01:14 +00:00
## About: An example of a circular singly linked list ##
2020-04-30 18:59:05 +00:00
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
2020-04-30 19:01:14 +00:00
## circlesinglelist.cpp
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
#include "circlesinglelist.h"
2020-04-30 18:59:05 +00:00
/******************************************************************************
* Constructors, Destructors, Operators
2020-04-30 19:01:14 +00:00
*****************************************************************************/
2020-04-30 18:59:05 +00:00
/** copy constructor
2020-04-30 19:01:14 +00:00
* @brief Construct a new CircleSingleList::CircleSingleList object from an existing one
*
* @param rhs CircleSingleList object
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
CircleSingleList::CircleSingleList(const CircleSingleList& rhs)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
Node *cp = rhs.tail;
Node *tempTail;
if (cp == NULL) tail = NULL;
2020-04-30 18:59:05 +00:00
else {
2020-04-30 19:01:14 +00:00
tail = new Node(cp->data);
tempTail = tail;
2020-04-30 18:59:05 +00:00
while (cp->next != NULL) {
cp = cp->next;
2020-04-30 19:01:14 +00:00
tail->next = new Node(cp->data);
tail = tail->next;
2020-04-30 18:59:05 +00:00
}
2020-04-30 19:01:14 +00:00
tail = tempTail;
2020-04-30 18:59:05 +00:00
}
}
/** operator=
2020-04-30 19:01:14 +00:00
* @brief Deep copy of the rhs CircleSingleList object
* Pass rhs by value to create local copy, swap its data with our tail
* Swapped local copy now with previous tail data deleted at end of scope using destr
*
* @param rhs CircleSingleList object
* @return CircleSingleList& A deep copy of the rhs CircleSingleList
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
CircleSingleList CircleSingleList::operator=(CircleSingleList rhs)
2020-04-30 18:59:05 +00:00
{
if (this == &rhs) return *this;
2020-04-30 19:01:14 +00:00
else std::swap(tail, rhs.tail);
2020-04-30 18:59:05 +00:00
return *this;
}
/** destructor
2020-04-30 19:01:14 +00:00
* @brief Destroy the CircleSingleList::CircleSingleList object
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
CircleSingleList::~CircleSingleList()
2020-04-30 18:59:05 +00:00
{
makeEmpty();
}
2020-04-30 19:01:14 +00:00
2020-04-30 18:59:05 +00:00
/******************************************************************************
* Public Member Functions
2020-04-30 19:01:14 +00:00
*****************************************************************************/
2020-04-30 18:59:05 +00:00
/** insert
* @brief Inserts a value to the head of our linked list
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param x The value to be inserted
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::insert(int val)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
bool inserted = insert(val, tail);
if (inserted) std::cout << "[" << val << "] was inserted into the list\n";
2020-04-30 18:59:05 +00:00
else std::cout << "[" << val << "] could not be inserted into the list\n";
return inserted;
}
/** insert at
* @brief Inserts a value in the place of a given key
* Key Node found is moved to the newNode->next positon
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param key The value to search for to determine insert location
* @param val The value to be inserted into the list
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::insert(int val, int key)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
if (isEmpty()) {
std::cout << "Cannot insert at an existing value using an empty list...\n";
return false;
}
bool inserted = insert(val, key, tail);
if (inserted) std::cout << "[" << val << "] was inserted into the list\n";
2020-04-30 18:59:05 +00:00
else std::cout << "[" << val << "] could not be inserted into the list\n";
return inserted;
}
/** remove
* @brief Removes a value in the list by calling a private member and handling output
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param val Value to be 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
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::remove(int val)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
if (isEmpty()) {
std::cout << "Cannot remove any values from an empty list...\n";
return false;
}
bool removed = remove(val, tail);
if (removed) std::cout << "[" << val << "] was removed from the list\n";
2020-04-30 18:59:05 +00:00
else std::cout << "[" << val << "] could not be removed from the list\n";
return removed;
}
/** replace
* @brief Replaces a value in the list by calling a private member and handling output
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param val Value to insert into the list
* @param key Value to be replaced within the list
* @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
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::replace(int val, int key)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
if (isEmpty()) {
std::cout << "Cannot replace any values from an empty list...\n";
return false;
}
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";
2020-04-30 18:59:05 +00:00
return replaced;
}
/** makeEmpty
2020-04-30 19:01:14 +00:00
* @brief Empty this CircleSingleList object, deleting all associated Nodes
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
void CircleSingleList::makeEmpty()
2020-04-30 18:59:05 +00:00
{
Node *nextNode, *temp;
2020-04-30 19:01:14 +00:00
// If the list is empty
if (tail == NULL) return;
// If the list has members other than itself, keep a pointer to them
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
2020-04-30 18:59:05 +00:00
temp = nextNode;
2020-04-30 19:01:14 +00:00
// Move to the next node
2020-04-30 18:59:05 +00:00
nextNode = nextNode->next;
2020-04-30 19:01:14 +00:00
// Delete the previous node and set it to NULL
2020-04-30 18:59:05 +00:00
delete temp;
temp = NULL;
}
2020-04-30 19:01:14 +00:00
// Always delete the tail node and set it to NULL
delete tail;
tail = NULL;
2020-04-30 18:59:05 +00:00
}
/** isEmpty
2020-04-30 19:01:14 +00:00
* @brief Determine if the CircleSingleList is empty
*
* @return true If the CircleSingleList::tail is NULL
* @return false If the CircleSingleList::tail contains data
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::isEmpty() const
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
return tail == NULL;
2020-04-30 18:59:05 +00:00
}
/** peek
2020-04-30 19:01:14 +00:00
* @brief return the value at the head of the list
*
* @return int The value held at the Node at the head of our linked list
2020-04-30 18:59:05 +00:00
*/
2020-05-01 04:02:45 +00:00
int CircleSingleList::peek() const
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
// If there is only one data member, a circular list node will point to itself
if (!isEmpty())
std::cout << "[" << tail->next->data << "] is at the top of our list\n";
2020-04-30 18:59:05 +00:00
else std::cout << "Nothing to peek, our list is empty...\n";
2020-05-01 04:02:45 +00:00
return tail->next->data;
2020-04-30 18:59:05 +00:00
}
/** print
2020-04-30 19:01:14 +00:00
* @brief Output the data held by the CircleSingleList object
2020-04-30 18:59:05 +00:00
* Calls to the private print()
*/
2020-04-30 19:01:14 +00:00
void CircleSingleList::print() const
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
// Print the list starting from the head node
if(!isEmpty()) print(tail->next);
2020-04-30 18:59:05 +00:00
else std::cout << "Nothing to print, our list is empty...\n";
}
/** find
* @brief Calls to the private member find() and handles return cases
2020-04-30 19:01:14 +00:00
*
* @param val The value to search for within our CircleSingleList
* @return true If the value was found in this CircleSingleList
* @return false If the value was not found in this CircleSingleList
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::find(int val) const
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
Node *result = find(val, tail);
2020-04-30 18:59:05 +00:00
if( result == NULL) {
std::cout << "[" << val << "] Was not found in our list\n";
return false;
}
2020-04-30 19:01:14 +00:00
2020-04-30 18:59:05 +00:00
std::cout << "[" << result->data << "] Was found in our list\n";
return true;
}
2020-04-30 19:01:14 +00:00
2020-04-30 18:59:05 +00:00
/******************************************************************************
* Private Member Functions
2020-04-30 19:01:14 +00:00
*****************************************************************************/
2020-04-30 18:59:05 +00:00
/** insert
* @brief Private member to handle inserting value into the list
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param val Value to be inserted
2020-04-30 19:01:14 +00:00
* @param tail The tail of the list to insert the value into
2020-04-30 18:59:05 +00:00
* @return true If the value was inserted
* @return false If the value could not be inserted
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::insert(int val, Node *&tail)
2020-04-30 18:59:05 +00:00
{
Node *newNode = new Node(val);
// If the list is not empty, update next pointer to head node
2020-04-30 19:01:14 +00:00
if (!isEmpty()) newNode->next = tail->next;
else tail = newNode;
2020-04-30 18:59:05 +00:00
// Always set head to our newNode
2020-04-30 19:01:14 +00:00
tail->next = newNode;
2020-04-30 18:59:05 +00:00
return true;
}
/** insert at
* @brief Private member to handle inserting a value at a key within our list
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param val Value to be inserted
* @param key Key value to search for within the list
2020-04-30 19:01:14 +00:00
* @param tail Tail node of the list to insert to
2020-04-30 18:59:05 +00:00
* @return true If the value was inserted
* @return false If the value was not inserted
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::insert(int val, int key, Node *&tail)
2020-04-30 18:59:05 +00:00
{
Node *newNode = new Node(val);
if (isEmpty()) return false;
// Let insert() handle inserting at the head
2020-04-30 19:01:14 +00:00
else if (tail->next->data == key) return insert(val);
2020-04-30 18:59:05 +00:00
2020-04-30 19:01:14 +00:00
Node *keyNode = findPrevious(key);
2020-04-30 18:59:05 +00:00
// 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
if (keyNode == NULL) return false;
2020-04-30 19:01:14 +00:00
// 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()
2020-04-30 18:59:05 +00:00
newNode->next = keyNode->next;
keyNode->next = newNode;
return true;
}
/** remove
* @brief Private member to remove values from the list
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param val Value to be removed
2020-04-30 19:01:14 +00:00
* @param tail Tail of the list to remove the value from
2020-04-30 18:59:05 +00:00
* @return true If the value has been removed from the list
* @return false If the value has not been removed from the list
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::remove(int val, Node *&tail)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
Node *exile, *tempHead;
Node *prevNode = findPrevious(val);
// If findPrevious returns NULL, the value was not found
if (prevNode == NULL) return false;
2020-04-30 18:59:05 +00:00
2020-04-30 19:01:14 +00:00
// If the node found is the only member of the list
if (prevNode == prevNode->next) {
// Delete, reset list to NULL
delete tail;
tail = NULL;
2020-04-30 18:59:05 +00:00
return true;
}
2020-04-30 19:01:14 +00:00
// 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;
}
2020-04-30 18:59:05 +00:00
2020-04-30 19:01:14 +00:00
// Delete the dangling ptr to the exile Node we removed from the list
delete exile;
exile = NULL;
2020-04-30 18:59:05 +00:00
return true;
}
/** replace
* @brief Private member to replace values within the list
2020-04-30 19:01:14 +00:00
*
2020-04-30 18:59:05 +00:00
* @param val Value to insert into the list
* @param key Value to be replaced within the list
2020-04-30 19:01:14 +00:00
* @param tail Tail of the list to replace the value
2020-04-30 18:59:05 +00:00
* @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
*/
2020-04-30 19:01:14 +00:00
bool CircleSingleList::replace(int val, int key, Node *&tail)
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
Node *replacee = find(key, tail);
2020-04-30 18:59:05 +00:00
if (replacee == NULL) return false;
replacee->data = val;
return true;
}
/** find
* @brief Find and return a Node which contains the given value
2020-04-30 19:01:14 +00:00
*
* @param val The value to search for within our CircleSingleList
* @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
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
CircleSingleList::Node* CircleSingleList::find(int val, Node *start) const
2020-04-30 18:59:05 +00:00
{
2020-04-30 19:01:14 +00:00
// Check the first Node, return if it is the value requested
if (start->data == val) return start;
Node *foundNode = start->next;
// Move through the list, checking each member until we reach the start Node
while (foundNode != start) {
2020-04-30 18:59:05 +00:00
if (foundNode->data == val) return foundNode;
2020-04-30 19:01:14 +00:00
foundNode = foundNode->next;
2020-04-30 18:59:05 +00:00
}
// If we have not yet returned a foundNode, the key is not in our list
return NULL;
}
/** findPrevious
* @brief Find and return the Node before a given value
2020-04-30 19:01:14 +00:00
*
* @param val The value to search for within our CircleSingleList
* @return CircleSingleList::Node* A pointer to the Node previous to the given value
2020-04-30 18:59:05 +00:00
*/
2020-04-30 19:01:14 +00:00
CircleSingleList::Node* CircleSingleList::findPrevious(int val) const
2020-04-30 18:59:05 +00:00
{
if (isEmpty()) return NULL;
2020-04-30 19:01:14 +00:00
else if (tail->next->data == val) return tail;
2020-04-30 18:59:05 +00:00
2020-04-30 19:01:14 +00:00
// Create temp ptr to the head of our list
Node *tempHead = tail->next;
// Move through the list until we reach the head node again
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;
2020-04-30 18:59:05 +00:00
}
return NULL;
}
/** print
2020-04-30 19:01:14 +00:00
* @brief Output the contents of a CircleSingleList until we hit a matching node
*
2020-04-30 18:59:05 +00:00
* @param start The Node to begin traversing output from
*/
2020-04-30 19:01:14 +00:00
void CircleSingleList::print(Node *start) const
2020-04-30 18:59:05 +00:00
{
Node *temp = start;
2020-04-30 19:01:14 +00:00
std::cout << "List Contents: " << temp->data << " | ";
temp = temp->next;
// Until we reach the start node again, print the list contents
while (temp != start) {
2020-04-30 18:59:05 +00:00
std::cout << temp->data << " | ";
temp = temp->next;
}
std::cout << std::endl;
}