klips/cpp/datastructs/doublelist/doublelist.cpp

340 lines
9.8 KiB
C++
Raw Permalink Normal View History

2020-05-01 03:40:35 +00:00
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
2020-05-01 03:43:25 +00:00
## About: An example of a doubly linked list ##
2020-05-01 03:40:35 +00:00
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
2020-05-01 03:43:25 +00:00
## doublelist.cpp
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
#include "doublelist.h"
2020-05-01 03:40:35 +00:00
/******************************************************************************
* Constructors, Destructors, Operators
2020-05-01 03:43:25 +00:00
*****************************************************************************/
2020-05-01 03:40:35 +00:00
/** copy constructor
2020-05-01 03:43:25 +00:00
* @brief Construct a new DoubleList::DoubleList object from an existing one
*
* @param rhs DoubleList object
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
DoubleList::DoubleList(const DoubleList& rhs)
2020-05-01 03:40:35 +00:00
{
Node *cp = rhs.head;
Node *tempHead;
if (cp == NULL) head = NULL;
else {
head = new Node(cp->data);
tempHead = head;
while (cp->next != NULL) {
cp = cp->next;
head->next = new Node(cp->data);
head = head->next;
}
head = tempHead;
}
}
/** operator=
2020-05-01 03:43:25 +00:00
* @brief Assign two DoubleList objects equal using copy constr and class destr
2020-05-01 03:40:35 +00:00
* Pass the rhs by value to create local copy, swap its contents
2020-05-01 03:43:25 +00:00
* Destructor called on previous DoubleList data at the end of this scope
*
* @param rhs DoubleList object passed by value
* @return DoubleList A deep copy of the rhs DoubleList object
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
DoubleList DoubleList::operator=(DoubleList rhs)
2020-05-01 03:40:35 +00:00
{
if (this == &rhs) return *this;
std::swap(head, rhs.head);
return *this;
}
/** destructor
2020-05-01 03:43:25 +00:00
* @brief Destroy the DoubleList::DoubleList object
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
DoubleList::~DoubleList()
2020-05-01 03:40:35 +00:00
{
makeEmpty();
}
2020-05-01 03:43:25 +00:00
2020-05-01 03:40:35 +00:00
/******************************************************************************
* Public Member Functions
2020-05-01 03:43:25 +00:00
*****************************************************************************/
2020-05-01 03:40:35 +00:00
/** insert
* @brief Inserts a value to the head of our linked list
2020-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +00:00
* @param x The value to be inserted
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::insert(int val)
2020-05-01 03:40:35 +00:00
{
bool inserted = insert(val, head);
2020-05-01 03:43:25 +00:00
if (inserted)
2020-05-01 03:40:35 +00:00
std::cout << "[" << val << "] was inserted into the list\n";
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 position
2020-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +00:00
* @param key The value to search for to determine insert location
* @param val The value to be inserted into the list
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::insert(int val, int key)
2020-05-01 03:40:35 +00:00
{
bool inserted = insert(val, key, head);
2020-05-01 03:43:25 +00:00
if (inserted)
2020-05-01 03:40:35 +00:00
std::cout << "[" << val << "] was inserted into the list\n";
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-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +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-05-01 03:43:25 +00:00
bool DoubleList::remove(int val)
2020-05-01 03:40:35 +00:00
{
bool removed = remove(val, head);
2020-05-01 03:43:25 +00:00
if (removed)
2020-05-01 03:40:35 +00:00
std::cout << "[" << val << "] was removed from the list\n";
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-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +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-05-01 03:43:25 +00:00
bool DoubleList::replace(int val, int key)
2020-05-01 03:40:35 +00:00
{
bool replaced = replace(val, key, head);
2020-05-01 03:43:25 +00:00
if (replaced)
2020-05-01 03:40:35 +00:00
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;
}
/** makeEmpty
2020-05-01 03:43:25 +00:00
* @brief Empty this DoubleList object, deleting all associated Nodes
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
void DoubleList::makeEmpty()
2020-05-01 03:40:35 +00:00
{
Node *nextNode, *temp;
if (head == NULL) return;
nextNode = head->next;
delete head;
head = NULL;
while(nextNode != NULL) {
temp = nextNode;
nextNode = nextNode->next;
delete temp;
temp = NULL;
}
}
/** isEmpty
2020-05-01 03:43:25 +00:00
* @brief Determine if the DoubleList is empty
*
* @return true If the DoubleList::head is NULL
* @return false If the DoubleList::head contains data
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::isEmpty() const
2020-05-01 03:40:35 +00:00
{
return head == NULL;
}
/** peek
2020-05-01 03:43:25 +00:00
* @brief returns the value at the DoubleList::head
*
* @return int The value held at the Node pointed to by DoubleList::head
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
int DoubleList::peek() const
2020-05-01 03:40:35 +00:00
{
2020-05-01 03:43:25 +00:00
if (!isEmpty())
2020-05-01 03:40:35 +00:00
std::cout << "[" << head->data << "] is at the top of our list\n";
else std::cout << "Nothing to peek, our list is empty...\n";
// If the list has data we return it, otherwise we return the smallest possible int (error)
return (!isEmpty()) ? head->data : INT32_MIN;
2020-05-01 03:40:35 +00:00
}
/** print
2020-05-01 03:43:25 +00:00
* @brief Output the data held by the DoubleList object
2020-05-01 03:40:35 +00:00
* Calls to the private print()
*/
2020-05-01 03:43:25 +00:00
void DoubleList::print() const
2020-05-01 03:40:35 +00:00
{
if(!isEmpty()) print(head);
else std::cout << "Nothing to print, our list is empty...\n";
}
/** find
* @brief Calls to the private member find() and handles return cases
2020-05-01 03:43:25 +00:00
*
* @param val The value to search for within our DoubleList
* @return true If the value was found in this DoubleList
* @return false If the value was not found in this DoubleList
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::find(int val) const
2020-05-01 03:40:35 +00:00
{
Node *result = find(val, head);
if( result == NULL) {
std::cout << "[" << val << "] Was not found in our list\n";
return false;
}
2020-05-01 03:43:25 +00:00
2020-05-01 03:40:35 +00:00
std::cout << "[" << result->data << "] Was found in our list\n";
return true;
}
2020-05-01 03:43:25 +00:00
2020-05-01 03:40:35 +00:00
/******************************************************************************
* Private Member Functions
2020-05-01 03:43:25 +00:00
*****************************************************************************/
2020-05-01 03:40:35 +00:00
/** insert
* @brief Private member to handle inserting value into the list
2020-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +00:00
* @param val Value to be inserted
* @param head The head of the list to insert the value into
* @return true If the value was inserted
* @return false If the value could not be inserted
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::insert(int val, Node *&head)
2020-05-01 03:40:35 +00:00
{
Node *newNode = new Node(val);
// If the list is not empty, update next pointer to head node
2020-05-01 03:43:25 +00:00
if (!isEmpty()) {
newNode->next = head;
// Create a prev ptr for the head node
head->prev = newNode;
}
2020-05-01 03:40:35 +00:00
// Always set head to our newNode
2020-05-01 03:43:25 +00:00
head = newNode;
2020-05-01 03:40:35 +00:00
return true;
}
/** insert at
* @brief Private member to handle inserting a value at a key within our list
2020-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +00:00
* @param val Value to be inserted
* @param key Key value to search for within the list
* @param head Head node of the list to insert to
* @return true If the value was inserted
* @return false If the value was not inserted
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::insert(int val, int key, Node *&head)
2020-05-01 03:40:35 +00:00
{
Node *newNode = new Node(val);
if (isEmpty()) return false;
// Let insert() handle inserting at the head
else if (head->data == key) return insert(val);
2020-05-01 03:43:25 +00:00
Node *keyNode = find(key, head);
2020-05-01 03:40:35 +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-05-01 03:43:25 +00:00
// Insert the newNode infront of the previous to the keyNode
newNode->prev = keyNode->prev;
keyNode->prev->next = newNode;
// Change the keyNode->prev ptr to newNode
newNode->next = keyNode;
keyNode->prev = newNode;
2020-05-01 03:40:35 +00:00
return true;
}
/** remove
* @brief Private member to remove values from the list
2020-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +00:00
* @param val Value to be removed
* @param head Head of the list to remove the value from
* @return true If the value has been removed from the list
* @return false If the value has not been removed from the list
*/
2020-05-01 03:43:25 +00:00
bool DoubleList::remove(int val, Node *&head)
2020-05-01 03:40:35 +00:00
{
if (head == NULL) return false;
else if (head->data == val) {
head = head->next;
return true;
}
2020-05-01 03:43:25 +00:00
Node *keyNode = find(val, head);
if (keyNode == NULL) return false;
Node *gtfo = keyNode;
if (keyNode->next != NULL) keyNode->next->prev = keyNode->prev;
if (keyNode->prev != NULL) keyNode->prev->next = keyNode->next;
2020-05-01 03:40:35 +00:00
delete gtfo;
2020-05-01 03:43:25 +00:00
gtfo = NULL;
2020-05-01 03:40:35 +00:00
return true;
}
/** replace
* @brief Private member to replace values within the list
2020-05-01 03:43:25 +00:00
*
2020-05-01 03:40:35 +00:00
* @param val Value to insert into the list
* @param key Value to be replaced within the list
* @param head Head of the list to replace the value
* @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-05-01 03:43:25 +00:00
bool DoubleList::replace(int val, int key, Node *&head)
2020-05-01 03:40:35 +00:00
{
Node *replacee = find(key, head);
if (replacee == NULL) return false;
replacee->data = val;
return true;
}
/** find
* @brief Find and return a Node which contains the given value
2020-05-01 03:43:25 +00:00
*
* @param val The value to search for within our DoubleList
* @return DoubleList::Node* A pointer to the Node containing the search value
2020-05-01 03:40:35 +00:00
*/
2020-05-01 03:43:25 +00:00
DoubleList::Node* DoubleList::find(int val, Node *start) const
2020-05-01 03:40:35 +00:00
{
// If given a NULL list, return NULL
// If given a head which contains the requested value, return the foundNode
if (start == NULL || start->data == val) return start;
Node *foundNode = start;
while (foundNode->next != NULL) {
foundNode = foundNode->next;
if (foundNode->data == val) return foundNode;
}
// If we have not yet returned a foundNode, the key is not in our list
return NULL;
}
/** print
2020-05-01 03:43:25 +00:00
* @brief Output the contents of a DoubleList from the given Node to NULL
*
2020-05-01 03:40:35 +00:00
* @param start The Node to begin traversing output from
*/
2020-05-01 03:43:25 +00:00
void DoubleList::print(Node *start) const
2020-05-01 03:40:35 +00:00
{
Node *temp = start;
std::cout << "List Contents: ";
while (temp != NULL) {
std::cout << temp->data << " | ";
temp = temp->next;
}
std::cout << std::endl;
}