RD of doubly linked list class
This commit is contained in:
parent
98b0d724cf
commit
4eb25a22b1
|
@ -5,15 +5,15 @@ CXXFLAGS=-g -Wall
|
||||||
# Driver
|
# Driver
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
driver: driver.cpp singlelist.o
|
driver: driver.cpp doublelist.o
|
||||||
${CXX} ${CXXFLAGS} driver.cpp singlelist.o -o driver
|
${CXX} ${CXXFLAGS} driver.cpp doublelist.o -o driver
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# SingleList
|
# DoubleList
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
singlelist.o: singlelist.cpp singlelist.h
|
doublelist.o: doublelist.cpp doublelist.h
|
||||||
${CXX} ${CXXFLAGS} -c singlelist.cpp -o singlelist.o
|
${CXX} ${CXXFLAGS} -c doublelist.cpp -o doublelist.o
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Clean
|
# Clean
|
||||||
|
|
|
@ -1,25 +1,26 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## 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 doubly linked list ##
|
||||||
## ##
|
## ##
|
||||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
##############################################################################
|
##############################################################################
|
||||||
## singlelist.cpp
|
## doublelist.cpp
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "singlelist.h"
|
#include "doublelist.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 DoubleList::DoubleList object from an existing one
|
||||||
*
|
*
|
||||||
* @param rhs SingleList object
|
* @param rhs DoubleList object
|
||||||
*/
|
*/
|
||||||
SingleList::SingleList(const SingleList& rhs)
|
DoubleList::DoubleList(const DoubleList& rhs)
|
||||||
{
|
{
|
||||||
Node *cp = rhs.head;
|
Node *cp = rhs.head;
|
||||||
Node *tempHead;
|
Node *tempHead;
|
||||||
|
@ -37,14 +38,14 @@ SingleList::SingleList(const SingleList& rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** operator=
|
/** operator=
|
||||||
* @brief Assign two SingleList objects equal using copy constr and class destr
|
* @brief Assign two DoubleList objects equal using copy constr and class destr
|
||||||
* Pass the rhs by value to create local copy, swap its contents
|
* Pass the rhs by value to create local copy, swap its contents
|
||||||
* Destructor called on previous SingleList data at the end of this scope
|
* Destructor called on previous DoubleList data at the end of this scope
|
||||||
*
|
*
|
||||||
* @param rhs SingleList object passed by value
|
* @param rhs DoubleList object passed by value
|
||||||
* @return SingleList A deep copy of the rhs SingleList object
|
* @return DoubleList A deep copy of the rhs DoubleList object
|
||||||
*/
|
*/
|
||||||
SingleList SingleList::operator=(SingleList rhs)
|
DoubleList DoubleList::operator=(DoubleList rhs)
|
||||||
{
|
{
|
||||||
if (this == &rhs) return *this;
|
if (this == &rhs) return *this;
|
||||||
std::swap(head, rhs.head);
|
std::swap(head, rhs.head);
|
||||||
|
@ -52,13 +53,14 @@ SingleList SingleList::operator=(SingleList rhs)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** destructor
|
/** destructor
|
||||||
* @brief Destroy the SingleList::SingleList object
|
* @brief Destroy the DoubleList::DoubleList object
|
||||||
*/
|
*/
|
||||||
SingleList::~SingleList()
|
DoubleList::~DoubleList()
|
||||||
{
|
{
|
||||||
makeEmpty();
|
makeEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Public Member Functions
|
* Public Member Functions
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
@ -68,7 +70,7 @@ SingleList::~SingleList()
|
||||||
*
|
*
|
||||||
* @param x The value to be inserted
|
* @param x The value to be inserted
|
||||||
*/
|
*/
|
||||||
bool SingleList::insert(int val)
|
bool DoubleList::insert(int val)
|
||||||
{
|
{
|
||||||
bool inserted = insert(val, head);
|
bool inserted = insert(val, head);
|
||||||
if (inserted)
|
if (inserted)
|
||||||
|
@ -84,7 +86,7 @@ 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 DoubleList::insert(int val, int key)
|
||||||
{
|
{
|
||||||
bool inserted = insert(val, key, head);
|
bool inserted = insert(val, key, head);
|
||||||
if (inserted)
|
if (inserted)
|
||||||
|
@ -100,7 +102,7 @@ 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 DoubleList::remove(int val)
|
||||||
{
|
{
|
||||||
bool removed = remove(val, head);
|
bool removed = remove(val, head);
|
||||||
if (removed)
|
if (removed)
|
||||||
|
@ -117,7 +119,7 @@ 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 DoubleList::replace(int val, int key)
|
||||||
{
|
{
|
||||||
bool replaced = replace(val, key, head);
|
bool replaced = replace(val, key, head);
|
||||||
if (replaced)
|
if (replaced)
|
||||||
|
@ -127,9 +129,9 @@ bool SingleList::replace(int val, int key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** makeEmpty
|
/** makeEmpty
|
||||||
* @brief Empty this SingleList object, deleting all associated Nodes
|
* @brief Empty this DoubleList object, deleting all associated Nodes
|
||||||
*/
|
*/
|
||||||
void SingleList::makeEmpty()
|
void DoubleList::makeEmpty()
|
||||||
{
|
{
|
||||||
Node *nextNode, *temp;
|
Node *nextNode, *temp;
|
||||||
|
|
||||||
|
@ -146,33 +148,34 @@ void SingleList::makeEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** isEmpty
|
/** isEmpty
|
||||||
* @brief Determine if the SingleList is empty
|
* @brief Determine if the DoubleList is empty
|
||||||
*
|
*
|
||||||
* @return true If the SingleList::head is NULL
|
* @return true If the DoubleList::head is NULL
|
||||||
* @return false If the SingleList::head contains data
|
* @return false If the DoubleList::head contains data
|
||||||
*/
|
*/
|
||||||
bool SingleList::isEmpty() const
|
bool DoubleList::isEmpty() const
|
||||||
{
|
{
|
||||||
return head == NULL;
|
return head == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** peek
|
/** peek
|
||||||
* @brief returns the value at the SingleList::head
|
* @brief returns the value at the DoubleList::head
|
||||||
*
|
*
|
||||||
* @return int The value held at the Node pointed to by SingleList::head
|
* @return int The value held at the Node pointed to by DoubleList::head
|
||||||
*/
|
*/
|
||||||
void SingleList::peek() const
|
int DoubleList::peek() const
|
||||||
{
|
{
|
||||||
if (!isEmpty())
|
if (!isEmpty())
|
||||||
std::cout << "[" << head->data << "] is at the top of our list\n";
|
std::cout << "[" << head->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";
|
||||||
|
return head->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** print
|
/** print
|
||||||
* @brief Output the data held by the SingleList object
|
* @brief Output the data held by the DoubleList object
|
||||||
* Calls to the private print()
|
* Calls to the private print()
|
||||||
*/
|
*/
|
||||||
void SingleList::print() const
|
void DoubleList::print() const
|
||||||
{
|
{
|
||||||
if(!isEmpty()) print(head);
|
if(!isEmpty()) print(head);
|
||||||
else std::cout << "Nothing to print, our list is empty...\n";
|
else std::cout << "Nothing to print, our list is empty...\n";
|
||||||
|
@ -181,11 +184,11 @@ void SingleList::print() const
|
||||||
/** 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 DoubleList
|
||||||
* @return true If the value was found in this SingleList
|
* @return true If the value was found in this DoubleList
|
||||||
* @return false If the value was not found in this SingleList
|
* @return false If the value was not found in this DoubleList
|
||||||
*/
|
*/
|
||||||
bool SingleList::find(int val) const
|
bool DoubleList::find(int val) const
|
||||||
{
|
{
|
||||||
Node *result = find(val, head);
|
Node *result = find(val, head);
|
||||||
if( result == NULL) {
|
if( result == NULL) {
|
||||||
|
@ -197,6 +200,7 @@ bool SingleList::find(int val) const
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Private Member Functions
|
* Private Member Functions
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
@ -209,11 +213,15 @@ bool SingleList::find(int val) const
|
||||||
* @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 DoubleList::insert(int val, Node *&head)
|
||||||
{
|
{
|
||||||
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 = head;
|
||||||
|
// Create a prev ptr for the head node
|
||||||
|
head->prev = newNode;
|
||||||
|
}
|
||||||
// Always set head to our newNode
|
// Always set head to our newNode
|
||||||
head = newNode;
|
head = newNode;
|
||||||
return true;
|
return true;
|
||||||
|
@ -228,20 +236,24 @@ bool SingleList::insert(int val, Node *&head)
|
||||||
* @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 DoubleList::insert(int val, int key, Node *&head)
|
||||||
{
|
{
|
||||||
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 (head->data == key) return insert(val);
|
||||||
|
|
||||||
Node *keyNode = findPrevious(key, head);
|
Node *keyNode = find(key, head);
|
||||||
// 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
|
// Insert the newNode infront of the previous to the keyNode
|
||||||
newNode->next = keyNode->next;
|
newNode->prev = keyNode->prev;
|
||||||
keyNode->next = newNode;
|
keyNode->prev->next = newNode;
|
||||||
|
|
||||||
|
// Change the keyNode->prev ptr to newNode
|
||||||
|
newNode->next = keyNode;
|
||||||
|
keyNode->prev = newNode;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,20 +265,21 @@ bool SingleList::insert(int val, int key, Node *&head)
|
||||||
* @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 DoubleList::remove(int val, Node *&head)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (head == NULL) return false;
|
if (head == NULL) return false;
|
||||||
else if (head->data == val) {
|
else if (head->data == val) {
|
||||||
head = head->next;
|
head = head->next;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *prevNode = findPrevious(val, head);
|
Node *keyNode = find(val, head);
|
||||||
if (prevNode == NULL) return false;
|
if (keyNode == NULL) return false;
|
||||||
Node *gtfo = prevNode->next;
|
Node *gtfo = keyNode;
|
||||||
prevNode->next = prevNode->next->next;
|
if (keyNode->next != NULL) keyNode->next->prev = keyNode->prev;
|
||||||
|
if (keyNode->prev != NULL) keyNode->prev->next = keyNode->next;
|
||||||
delete gtfo;
|
delete gtfo;
|
||||||
|
gtfo = NULL;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,7 +292,7 @@ bool SingleList::remove(int val, Node *&head)
|
||||||
* @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 DoubleList::replace(int val, int key, Node *&head)
|
||||||
{
|
{
|
||||||
Node *replacee = find(key, head);
|
Node *replacee = find(key, head);
|
||||||
if (replacee == NULL) return false;
|
if (replacee == NULL) return false;
|
||||||
|
@ -290,10 +303,10 @@ 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 DoubleList
|
||||||
* @return SingleList::Node* A pointer to the Node containing the search value
|
* @return DoubleList::Node* A pointer to the Node containing the search value
|
||||||
*/
|
*/
|
||||||
SingleList::Node* SingleList::find(int val, Node *start) const
|
DoubleList::Node* DoubleList::find(int val, Node *start) const
|
||||||
{
|
{
|
||||||
// If given a NULL list, return NULL
|
// If given a NULL list, return NULL
|
||||||
// If given a head which contains the requested value, return the foundNode
|
// If given a head which contains the requested value, return the foundNode
|
||||||
|
@ -308,32 +321,12 @@ SingleList::Node* SingleList::find(int val, Node *start) const
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** findPrevious
|
|
||||||
* @brief Find and return the Node before a given value
|
|
||||||
*
|
|
||||||
* @param val The value to search for within our SingleList
|
|
||||||
* @return SingleList::Node* A pointer to the Node previous to the given value
|
|
||||||
*/
|
|
||||||
SingleList::Node* SingleList::findPrevious(int val, Node *start) const
|
|
||||||
{
|
|
||||||
if (isEmpty()) return NULL;
|
|
||||||
else if (start->data == val) return start;
|
|
||||||
|
|
||||||
Node *temp = start;
|
|
||||||
while (temp->next != NULL) {
|
|
||||||
if (temp->next->data == val) return temp;
|
|
||||||
temp = temp->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** print
|
/** print
|
||||||
* @brief Output the contents of a SingleList from the given Node to NULL
|
* @brief Output the contents of a DoubleList from the given Node to NULL
|
||||||
*
|
*
|
||||||
* @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 DoubleList::print(Node *start) const
|
||||||
{
|
{
|
||||||
Node *temp = start;
|
Node *temp = start;
|
||||||
std::cout << "List Contents: ";
|
std::cout << "List Contents: ";
|
||||||
|
|
|
@ -1,45 +1,40 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## 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 doubly 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
|
## doublelist.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LINKEDLIST_H
|
#ifndef DOUBLELIST_H
|
||||||
#define LINKEDLIST_H
|
#define DOUBLELIST_H
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
class SingleList{
|
class DoubleList {
|
||||||
public:
|
public:
|
||||||
SingleList() : head(NULL) {};
|
DoubleList() : head(NULL) {};
|
||||||
SingleList(const SingleList& rhs);
|
DoubleList(const DoubleList& rhs);
|
||||||
SingleList operator=(SingleList rhs);
|
DoubleList operator=(DoubleList rhs);
|
||||||
~SingleList();
|
~DoubleList();
|
||||||
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);
|
||||||
bool replace(int val, int key);
|
bool replace(int val, int key);
|
||||||
void makeEmpty();
|
void makeEmpty();
|
||||||
bool isEmpty() const;
|
bool isEmpty() const;
|
||||||
void peek() const;
|
int peek() const;
|
||||||
void print() const;
|
void print() const;
|
||||||
bool find(int val) const;
|
bool find(int val) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Node {
|
struct Node {
|
||||||
int data;
|
int data;
|
||||||
Node *next;
|
Node *next, *prev;
|
||||||
Node(): data(), next(NULL) {};
|
Node(): data(), next(NULL), prev(NULL) {};
|
||||||
Node(int val): data(val), next(NULL) {};
|
Node(int val): data(val), next(NULL), prev(NULL) {};
|
||||||
};
|
};
|
||||||
Node *head;
|
Node *head;
|
||||||
bool insert(int val, Node *&head);
|
bool insert(int val, Node *&head);
|
||||||
|
@ -47,7 +42,6 @@ class SingleList{
|
||||||
bool remove(int val, Node *&head);
|
bool remove(int val, Node *&head);
|
||||||
bool replace(int val, int key, Node *&head);
|
bool replace(int val, int key, Node *&head);
|
||||||
Node* find(int val, Node *start) const;
|
Node* find(int val, Node *start) const;
|
||||||
Node* findPrevious(int val, Node *start) const;
|
|
||||||
void print(Node *start) const;
|
void print(Node *start) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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 doubly 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 "doublelist.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
enum OPS {
|
enum OPS {
|
||||||
|
@ -19,7 +19,7 @@ int main()
|
||||||
{
|
{
|
||||||
std::cout << "Driver: \n";
|
std::cout << "Driver: \n";
|
||||||
|
|
||||||
SingleList testList;
|
DoubleList testList;
|
||||||
bool exit = false;
|
bool exit = false;
|
||||||
int choice = -1;
|
int choice = -1;
|
||||||
int val, key;
|
int val, key;
|
||||||
|
|
Loading…
Reference in New Issue