Add CMakeLists for all datastructs

+ Reorder, refactor comments
This commit is contained in:
Shaun Reed 2020-07-11 20:55:15 -04:00
parent be91573abc
commit a692a0f631
22 changed files with 168 additions and 50 deletions

View File

@ -5,7 +5,7 @@
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
############################################################################## ##############################################################################
## vector.cpp ## CMakeLists.txt
# #
cmake_minimum_required(VERSION 3.2) cmake_minimum_required(VERSION 3.2)

View File

@ -15,14 +15,6 @@
* Constructors, Destructors, Operators * Constructors, Destructors, Operators
*********************************************************************************/ *********************************************************************************/
/** Default Destructor
* @brief Destroy the Binary Search Tree:: Binary Search Tree object
*/
BinarySearchTree::~BinarySearchTree()
{
makeEmpty(root);
}
/** Copy Assignment Operator /** Copy Assignment Operator
* @brief Empty the calling object's root BinaryNode, and copy the rhs data * @brief Empty the calling object's root BinaryNode, and copy the rhs data
* *
@ -41,6 +33,14 @@ const BinarySearchTree& BinarySearchTree::operator=(const BinarySearchTree& rhs)
return *this; return *this;
} }
/** Default Destructor
* @brief Destroy the Binary Search Tree:: Binary Search Tree object
*/
BinarySearchTree::~BinarySearchTree()
{
makeEmpty(root);
}
/******************************************************************************** /********************************************************************************
* Public Member Functions * Public Member Functions
@ -82,12 +82,12 @@ bool BinarySearchTree::contains(const int &x) const
} }
/** isEmpty /** isEmpty
* @brief Determine wheter or not the calling BST object is empty * @brief Determine whether or not the calling BST object is empty
* *
* @return true If this->root node points to an empty tree (NULL) * @return true If this->root node points to an empty tree (NULL)
* @return false If this->root node points to a constructed BinaryNode * @return false If this->root node points to a constructed BinaryNode
*/ */
bool BinarySearchTree::isEmpty() // const? bool BinarySearchTree::isEmpty() const
{ {
return root == NULL; return root == NULL;
} }
@ -130,6 +130,7 @@ void BinarySearchTree::makeEmpty()
void BinarySearchTree::printInOrder() const void BinarySearchTree::printInOrder() const
{ {
printInOrder(root); printInOrder(root);
std::cout << std::endl;
} }
/** printPostOrder /** printPostOrder
@ -139,6 +140,7 @@ void BinarySearchTree::printInOrder() const
void BinarySearchTree::printPostOrder() const void BinarySearchTree::printPostOrder() const
{ {
printPostOrder(root); printPostOrder(root);
std::cout << std::endl;
} }
/** printPreOrder /** printPreOrder
@ -148,6 +150,7 @@ void BinarySearchTree::printPostOrder() const
void BinarySearchTree::printPreOrder() const void BinarySearchTree::printPreOrder() const
{ {
printPreOrder(root); printPreOrder(root);
std::cout << std::endl;
} }
@ -174,7 +177,7 @@ BinarySearchTree::BinaryNode * BinarySearchTree::clone(BinaryNode *t) const
* @brief Insert a value into the BST of the given BinaryNode * @brief Insert a value into the BST of the given BinaryNode
* *
* @param x The value to be inserted * @param x The value to be inserted
* @param t The BinaryNode to beign insertion * @param t The BinaryNode to begin insertion
*/ */
void BinarySearchTree::insert(const int &x, BinarySearchTree::BinaryNode *&t) const void BinarySearchTree::insert(const int &x, BinarySearchTree::BinaryNode *&t) const
{ {
@ -244,7 +247,7 @@ BinarySearchTree::BinaryNode * BinarySearchTree::findMin(BinarySearchTree::Binar
/** findMax /** findMax
* @brief Find the maximum value within the BST of the given BinaryNode * @brief Find the maximum value within the BST of the given BinaryNode
* *
* @param t Te root BinaryNode to begin checking values * @param t The root BinaryNode to begin checking values
* @return BinarySearchTree::BinaryNode* The BinaryNode which contains the largest value (returns NULL if BST is empty) * @return BinarySearchTree::BinaryNode* The BinaryNode which contains the largest value (returns NULL if BST is empty)
*/ */
BinarySearchTree::BinaryNode * BinarySearchTree::findMax(BinarySearchTree::BinaryNode *t) const BinarySearchTree::BinaryNode * BinarySearchTree::findMax(BinarySearchTree::BinaryNode *t) const
@ -265,7 +268,7 @@ BinarySearchTree::BinaryNode * BinarySearchTree::findMax(BinarySearchTree::Binar
* @brief Determines if the value exists within the given BinaryNode and its children * @brief Determines if the value exists within the given BinaryNode and its children
* *
* @param x The value to search for within the BST * @param x The value to search for within the BST
* @param t The root BinaryNode to beign the search * @param t The root BinaryNode to begin the search
* @return true If the value is found within the root node or any of its children * @return true If the value is found within the root node or any of its children
* @return false If the value is not found within the root node or any of its children * @return false If the value is not found within the root node or any of its children
*/ */

View File

@ -24,7 +24,7 @@ class BinarySearchTree {
const int & findMin() const; const int & findMin() const;
const int & findMax() const; const int & findMax() const;
bool contains(const int &x) const; bool contains(const int &x) const;
bool isEmpty(); bool isEmpty() const;
void insert(const int &x); void insert(const int &x);
void remove(const int &x); void remove(const int &x);
void makeEmpty(); void makeEmpty();

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a circular doubly linked list implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## CMakeLists.txt
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
# Define the project name
project(CircleDouble)
# Define source files
set(SRC driver.cpp circledoublelist.cpp)
# Build an executable
add_executable(CircleDoubleDriver ${SRC})

View File

@ -247,7 +247,7 @@ bool CircleDoubleList::insert(int val, Node *&tail)
// Relink the current head to the node after newNode // Relink the current head to the node after newNode
newNode->next = tail->next; newNode->next = tail->next;
tail->next->prev = newNode; tail->next->prev = newNode;
// Relink our head and tail nodes, makine newNode the head node // Relink our head and tail nodes, making newNode the head node
tail->next = newNode; tail->next = newNode;
newNode->prev = tail; newNode->prev = tail;
} }

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a circular singly linked list implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## CMakeLists.txt
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
# Define the project name
project(CircleSingle)
# Define source files
set(SRC driver.cpp circlesinglelist.cpp)
# Build an executable
add_executable(CircleSingleDriver ${SRC})

View File

@ -40,7 +40,7 @@ CircleSingleList::CircleSingleList(const CircleSingleList& rhs)
/** operator= /** operator=
* @brief Deep copy of the rhs CircleSingleList object * @brief Deep copy of the rhs CircleSingleList object
* Pass rhs by value to create local copy, swap its data with our tail * 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 * Swapped local copy deleted at end of scope using destructor
* *
* @param rhs CircleSingleList object * @param rhs CircleSingleList object
* @return CircleSingleList& A deep copy of the rhs CircleSingleList * @return CircleSingleList& A deep copy of the rhs CircleSingleList
@ -53,7 +53,7 @@ CircleSingleList CircleSingleList::operator=(CircleSingleList rhs)
} }
/** destructor /** destructor
* @brief Destroy the CircleSingleList::CircleSingleList object * @brief Destroy the CircleSingleList object
*/ */
CircleSingleList::~CircleSingleList() CircleSingleList::~CircleSingleList()
{ {
@ -80,7 +80,7 @@ bool CircleSingleList::insert(int val)
/** insert at /** insert at
* @brief Inserts a value in the place of a given key * @brief Inserts a value in the place of a given key
* Key Node found is moved to the newNode->next positon * Key Node found is moved to the newNode->next position
* *
* @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

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a doubly linked list implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## CMakeLists.txt
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
# Define the project name
project(DoubleList)
# Define source files
set(SRC driver.cpp doublelist.cpp)
# Build an executable
add_executable(DoubleListDriver ${SRC})

View File

@ -81,7 +81,7 @@ bool DoubleList::insert(int val)
/** insert at /** insert at
* @brief Inserts a value in the place of a given key * @brief Inserts a value in the place of a given key
* Key Node found is moved to the newNode->next positon * Key Node found is moved to the newNode->next position
* *
* @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

View File

@ -26,7 +26,7 @@ int main()
while (!exit) while (!exit)
{ {
std::cout << "##### Singly Linked List Menu #####\n\t0. Exit" std::cout << "##### Doubly 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;

View File

@ -10,6 +10,7 @@
#include "maxheap.h" #include "maxheap.h"
/******************************************************************************** /********************************************************************************
* Constructors, Destructors, Operators * Constructors, Destructors, Operators
*********************************************************************************/ *********************************************************************************/
@ -93,7 +94,7 @@ void MaxHeap::del()
/** print /** print
* Outputs all values held within the heap * Outputs all values held within the heap
*/ */
void MaxHeap::print() void MaxHeap::print() const
{ {
print(heap, index); print(heap, index);
} }
@ -115,7 +116,7 @@ void MaxHeap::makeEmpty()
* *
* @return The smallest value stored in our heap * @return The smallest value stored in our heap
*/ */
int MaxHeap::findMin() int MaxHeap::findMin() const
{ {
int min = INT32_MAX; int min = INT32_MAX;
for (int i = ROOT; i < index; i++) for (int i = ROOT; i < index; i++)
@ -128,7 +129,7 @@ int MaxHeap::findMin()
* *
* @return The largest value stored in our heap * @return The largest value stored in our heap
*/ */
int MaxHeap::findMax() int MaxHeap::findMax() const
{ {
return heap[ROOT]; return heap[ROOT];
} }
@ -138,7 +139,7 @@ int MaxHeap::findMax()
* *
* @return true if the heap is empty, false if it has contents * @return true if the heap is empty, false if it has contents
*/ */
bool MaxHeap::isEmpty() bool MaxHeap::isEmpty() const
{ {
return heap == NULL; return heap == NULL;
} }
@ -148,7 +149,7 @@ bool MaxHeap::isEmpty()
* *
* @return true if the heap is full, false if it is not * @return true if the heap is full, false if it is not
*/ */
bool MaxHeap::isFull() bool MaxHeap::isFull() const
{ {
// Offset for the 0 index // Offset for the 0 index
return index >= size-1; return index >= size-1;
@ -195,7 +196,7 @@ void MaxHeap::del(int* heap)
* @param heap Address of the heap array * @param heap Address of the heap array
* @param _index Last free position in the array * @param _index Last free position in the array
*/ */
void MaxHeap::print(int* heap, int _index) void MaxHeap::print(int* heap, int _index) const
{ {
if (isEmpty()) return; if (isEmpty()) return;
for (int i = 0; i < _index; i++) for (int i = 0; i < _index; i++)

View File

@ -25,17 +25,17 @@ class MaxHeap {
MaxHeap operator=(MaxHeap rhs); MaxHeap operator=(MaxHeap rhs);
void insert(int val); void insert(int val);
void del(); void del();
void print(); void print() const;
void makeEmpty(); void makeEmpty();
int findMax(); int findMax() const;
int findMin(); int findMin() const;
bool isEmpty(); bool isEmpty() const;
bool isFull(); bool isFull() const;
private: private:
void insert(int*& heap, int _size, int val); void insert(int*& heap, int _size, int val);
void del(int* heap); void del(int* heap);
void print(int* heap, int _index); void print(int* heap, int _index) const;
void grow(int*& heap, int _size); void grow(int*& heap, int _size);
void siftUp(int* heap, int _index); void siftUp(int* heap, int _index);
void siftDown(int* heap, int currentMax); void siftDown(int* heap, int currentMax);

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a queue implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## CMakeLists.txt
#
cmake_minimum_required(VERSION 3.2)
# Define the project name
project(Queue)
# Define source files
set(SRC driver.cpp queuelist.cpp)
# Build an executable
add_executable(QueueDriver ${SRC})

View File

@ -65,4 +65,3 @@ int main()
} }
} }
} }

View File

@ -75,7 +75,7 @@ QueueList::~QueueList()
/** enqueue /** enqueue
* @brief Queue a value to the tail of our linked list * @brief Queue a value to the tail of our linked list
* *
* @param x The value to be inserted into the queue * @param val The value to be inserted into the queue
*/ */
bool QueueList::enqueue(int val) bool QueueList::enqueue(int val)
{ {
@ -158,6 +158,7 @@ void QueueList::makeEmpty()
} }
} }
/****************************************************************************** /******************************************************************************
* Private Member Functions * Private Member Functions
*****************************************************************************/ *****************************************************************************/

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a singly linked list implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## CMakeLists.txt
#
cmake_minimum_required(VERSION 3.2)
# Define the project name
project(SingleList)
# Define source files
set(SRC driver.cpp singlelist.cpp)
# Build an executable
add_executable(SingleListDriver ${SRC})

View File

@ -68,7 +68,7 @@ SingleList::~SingleList()
/** insert /** insert
* @brief Inserts a value to the head of our linked list * @brief Inserts a value to the head of our linked list
* *
* @param x The value to be inserted * @param val The value to be inserted
*/ */
bool SingleList::insert(int val) bool SingleList::insert(int val)
{ {
@ -81,7 +81,7 @@ bool SingleList::insert(int val)
/** insert at /** insert at
* @brief Inserts a value in the place of a given key * @brief Inserts a value in the place of a given key
* Key Node found is moved to the newNode->next positon * Key Node found is moved to the newNode->next position
* *
* @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
@ -296,6 +296,7 @@ bool SingleList::replace(int val, int key, Node *&head)
* @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 SingleList
* @param start The Node to start the search from
* @return SingleList::Node* A pointer to the Node containing the search value * @return SingleList::Node* A pointer to the Node containing the search value
*/ */
SingleList::Node* SingleList::find(int val, Node *start) const SingleList::Node* SingleList::find(int val, Node *start) const
@ -317,6 +318,7 @@ SingleList::Node* SingleList::find(int val, Node *start) const
* @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 SingleList
* @param start The Node to start the search from
* @return SingleList::Node* A pointer to the Node previous to the given value * @return SingleList::Node* A pointer to the Node previous to the given value
*/ */
SingleList::Node* SingleList::findPrevious(int val, Node *start) const SingleList::Node* SingleList::findPrevious(int val, Node *start) const

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: An example of a stack implementation using linked lists ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## CMakeLists.txt
#
cmake_minimum_required(VERSION 3.2)
# Define the project name
project(Stack)
# Define source files
set(SRC driver.cpp stacklist.cpp)
# Build an executable
add_executable(StackDriver ${SRC})

View File

@ -65,4 +65,3 @@ int main()
} }
} }
} }

View File

@ -74,7 +74,7 @@ StackList::~StackList()
/** push /** push
* @brief Push a value to the head of our linked list * @brief Push a value to the head of our linked list
* *
* @param x The value to be inserted * @param val The value to be inserted
*/ */
bool StackList::push(int val) bool StackList::push(int val)
{ {
@ -146,7 +146,6 @@ bool StackList::isEmpty() const
return head == NULL; return head == NULL;
} }
/** print /** print
* @brief Output the data held by the StackList object * @brief Output the data held by the StackList object
* Calls to the private print() * Calls to the private print()
@ -180,6 +179,12 @@ bool StackList::push(int val, Node *&head)
return true; return true;
} }
/** pop
* @brief Private member to handle removing the head node from the stack
*
* @param head The head node of the stack
* @return The last known value held at the head node before removal
*/
int StackList::pop(Node *&head) int StackList::pop(Node *&head)
{ {
// We already know the stack is not empty from public pop() // We already know the stack is not empty from public pop()

View File

@ -7,6 +7,7 @@
############################################################################## ##############################################################################
## vector.cpp ## vector.cpp
# #
cmake_minimum_required(VERSION 3.2) cmake_minimum_required(VERSION 3.2)
# Define the project name # Define the project name

View File

@ -14,7 +14,6 @@
/****************************************************************************** /******************************************************************************
* Constructors, Destructors, Operators * Constructors, Destructors, Operators
*****************************************************************************/ *****************************************************************************/
// TODO: Fix const, destr, op=
/** copy constructor /** copy constructor
* @brief Construct a new Vector::Vector object from an existing one * @brief Construct a new Vector::Vector object from an existing one
@ -92,7 +91,7 @@ bool Vector::push(int val)
* Once returned, the curIndex is decremented via data[curIndex--] * Once returned, the curIndex is decremented via data[curIndex--]
* If the vector is empty, returns INT32_MIN * If the vector is empty, returns INT32_MIN
* *
* @return int The value held at the Node pointed to by Vector::data[index -1] * @return int The value held at the Node pointed to by Vector::data[index]
*/ */
int Vector::pop() int Vector::pop()
{ {
@ -101,7 +100,7 @@ int Vector::pop()
val = pop(data); val = pop(data);
std::cout << "[" << val << "] has been popped from our Vector\n"; std::cout << "[" << val << "] has been popped from our Vector\n";
} }
else std::cout << "Nothing to pop, our stack is empty...\n"; else std::cout << "Nothing to pop, our Vector is empty...\n";
return val; return val;
} }
@ -134,8 +133,6 @@ int Vector::peek() const
return val; return val;
} }
// TODO: Verify that isEmpty works by actually checking data == NULL
/** isEmpty /** isEmpty
* @brief Determine if the Vector is empty * @brief Determine if the Vector is empty
* *
@ -144,7 +141,7 @@ int Vector::peek() const
*/ */
bool Vector::isEmpty() const bool Vector::isEmpty() const
{ {
return curIndex <= -1; return data == NULL;
} }
/** isFull /** isFull
@ -214,6 +211,7 @@ int Vector::getValue(int index) const
return data[index]; return data[index];
} }
/****************************************************************************** /******************************************************************************
* Private Member Functions * Private Member Functions
*****************************************************************************/ *****************************************************************************/
@ -261,8 +259,7 @@ int Vector::pop(int *&data)
/** makeEmpty /** makeEmpty
* @brief Private member to empty Vector object, deleting all associated data * @brief Private member to empty Vector object, deleting all associated data
* *
* @param head The head of the stack to be deleted * @param data The data of the Vector to be deleted
*
*/ */
void Vector::makeEmpty(int *&data) void Vector::makeEmpty(int *&data)
{ {
@ -275,7 +272,7 @@ void Vector::makeEmpty(int *&data)
/** peek /** peek
* @brief Private member to display the value at the end of our Vector * @brief Private member to display the value at the end of our Vector
* *
* @param data The Vector data peek * @param data The Vector data to peek
* @return int The value stored at the end of the Vector * @return int The value stored at the end of the Vector
*/ */
int Vector::peek(int *data) const int Vector::peek(int *data) const
@ -286,6 +283,8 @@ int Vector::peek(int *data) const
/** print /** print
* @brief Output the contents of a Vector from the beginning to the end * @brief Output the contents of a Vector from the beginning to the end
*
* @param data The data within the Vector to output
*/ */
void Vector::print(int *data) const void Vector::print(int *data) const
{ {