Add CMakeLists for all datastructs
+ Reorder, refactor comments
This commit is contained in:
parent
be91573abc
commit
a692a0f631
|
@ -5,7 +5,7 @@
|
|||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## vector.cpp
|
||||
## CMakeLists.txt
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
|
|
@ -15,14 +15,6 @@
|
|||
* Constructors, Destructors, Operators
|
||||
*********************************************************************************/
|
||||
|
||||
/** Default Destructor
|
||||
* @brief Destroy the Binary Search Tree:: Binary Search Tree object
|
||||
*/
|
||||
BinarySearchTree::~BinarySearchTree()
|
||||
{
|
||||
makeEmpty(root);
|
||||
}
|
||||
|
||||
/** Copy Assignment Operator
|
||||
* @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;
|
||||
}
|
||||
|
||||
/** Default Destructor
|
||||
* @brief Destroy the Binary Search Tree:: Binary Search Tree object
|
||||
*/
|
||||
BinarySearchTree::~BinarySearchTree()
|
||||
{
|
||||
makeEmpty(root);
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
* Public Member Functions
|
||||
|
@ -82,12 +82,12 @@ bool BinarySearchTree::contains(const int &x) const
|
|||
}
|
||||
|
||||
/** 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 false If this->root node points to a constructed BinaryNode
|
||||
*/
|
||||
bool BinarySearchTree::isEmpty() // const?
|
||||
bool BinarySearchTree::isEmpty() const
|
||||
{
|
||||
return root == NULL;
|
||||
}
|
||||
|
@ -130,6 +130,7 @@ void BinarySearchTree::makeEmpty()
|
|||
void BinarySearchTree::printInOrder() const
|
||||
{
|
||||
printInOrder(root);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
/** printPostOrder
|
||||
|
@ -139,6 +140,7 @@ void BinarySearchTree::printInOrder() const
|
|||
void BinarySearchTree::printPostOrder() const
|
||||
{
|
||||
printPostOrder(root);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
/** printPreOrder
|
||||
|
@ -148,6 +150,7 @@ void BinarySearchTree::printPostOrder() const
|
|||
void BinarySearchTree::printPreOrder() const
|
||||
{
|
||||
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
|
||||
*
|
||||
* @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
|
||||
{
|
||||
|
@ -244,7 +247,7 @@ BinarySearchTree::BinaryNode * BinarySearchTree::findMin(BinarySearchTree::Binar
|
|||
/** findMax
|
||||
* @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)
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @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 false If the value is not found within the root node or any of its children
|
||||
*/
|
||||
|
@ -336,4 +339,4 @@ void BinarySearchTree::printPreOrder(BinaryNode *t) const
|
|||
printPreOrder(t->left);
|
||||
printPreOrder(t->right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ class BinarySearchTree {
|
|||
const int & findMin() const;
|
||||
const int & findMax() const;
|
||||
bool contains(const int &x) const;
|
||||
bool isEmpty();
|
||||
bool isEmpty() const;
|
||||
void insert(const int &x);
|
||||
void remove(const int &x);
|
||||
void makeEmpty();
|
||||
|
|
|
@ -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})
|
|
@ -247,7 +247,7 @@ bool CircleDoubleList::insert(int val, Node *&tail)
|
|||
// Relink the current head to the node after newNode
|
||||
newNode->next = tail->next;
|
||||
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;
|
||||
newNode->prev = tail;
|
||||
}
|
||||
|
|
|
@ -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})
|
|
@ -40,7 +40,7 @@ CircleSingleList::CircleSingleList(const CircleSingleList& rhs)
|
|||
/** operator=
|
||||
* @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
|
||||
* Swapped local copy deleted at end of scope using destructor
|
||||
*
|
||||
* @param rhs CircleSingleList object
|
||||
* @return CircleSingleList& A deep copy of the rhs CircleSingleList
|
||||
|
@ -53,7 +53,7 @@ CircleSingleList CircleSingleList::operator=(CircleSingleList rhs)
|
|||
}
|
||||
|
||||
/** destructor
|
||||
* @brief Destroy the CircleSingleList::CircleSingleList object
|
||||
* @brief Destroy the CircleSingleList object
|
||||
*/
|
||||
CircleSingleList::~CircleSingleList()
|
||||
{
|
||||
|
@ -80,7 +80,7 @@ bool CircleSingleList::insert(int val)
|
|||
|
||||
/** insert at
|
||||
* @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 val The value to be inserted into the list
|
||||
|
|
|
@ -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})
|
|
@ -81,7 +81,7 @@ bool DoubleList::insert(int val)
|
|||
|
||||
/** insert at
|
||||
* @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 val The value to be inserted into the list
|
||||
|
|
|
@ -26,7 +26,7 @@ int main()
|
|||
|
||||
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\t5. Print list\n\t6. Find\n\t7. Remove\n\t8. Replace\n";
|
||||
std::cin >> choice;
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
#include "maxheap.h"
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
* Constructors, Destructors, Operators
|
||||
*********************************************************************************/
|
||||
|
@ -93,7 +94,7 @@ void MaxHeap::del()
|
|||
/** print
|
||||
* Outputs all values held within the heap
|
||||
*/
|
||||
void MaxHeap::print()
|
||||
void MaxHeap::print() const
|
||||
{
|
||||
print(heap, index);
|
||||
}
|
||||
|
@ -115,7 +116,7 @@ void MaxHeap::makeEmpty()
|
|||
*
|
||||
* @return The smallest value stored in our heap
|
||||
*/
|
||||
int MaxHeap::findMin()
|
||||
int MaxHeap::findMin() const
|
||||
{
|
||||
int min = INT32_MAX;
|
||||
for (int i = ROOT; i < index; i++)
|
||||
|
@ -128,7 +129,7 @@ int MaxHeap::findMin()
|
|||
*
|
||||
* @return The largest value stored in our heap
|
||||
*/
|
||||
int MaxHeap::findMax()
|
||||
int MaxHeap::findMax() const
|
||||
{
|
||||
return heap[ROOT];
|
||||
}
|
||||
|
@ -138,7 +139,7 @@ int MaxHeap::findMax()
|
|||
*
|
||||
* @return true if the heap is empty, false if it has contents
|
||||
*/
|
||||
bool MaxHeap::isEmpty()
|
||||
bool MaxHeap::isEmpty() const
|
||||
{
|
||||
return heap == NULL;
|
||||
}
|
||||
|
@ -148,7 +149,7 @@ bool MaxHeap::isEmpty()
|
|||
*
|
||||
* @return true if the heap is full, false if it is not
|
||||
*/
|
||||
bool MaxHeap::isFull()
|
||||
bool MaxHeap::isFull() const
|
||||
{
|
||||
// Offset for the 0 index
|
||||
return index >= size-1;
|
||||
|
@ -195,7 +196,7 @@ void MaxHeap::del(int* heap)
|
|||
* @param heap Address of the heap 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;
|
||||
for (int i = 0; i < _index; i++)
|
||||
|
|
|
@ -25,17 +25,17 @@ class MaxHeap {
|
|||
MaxHeap operator=(MaxHeap rhs);
|
||||
void insert(int val);
|
||||
void del();
|
||||
void print();
|
||||
void print() const;
|
||||
void makeEmpty();
|
||||
int findMax();
|
||||
int findMin();
|
||||
bool isEmpty();
|
||||
bool isFull();
|
||||
int findMax() const;
|
||||
int findMin() const;
|
||||
bool isEmpty() const;
|
||||
bool isFull() const;
|
||||
|
||||
private:
|
||||
void insert(int*& heap, int _size, int val);
|
||||
void del(int* heap);
|
||||
void print(int* heap, int _index);
|
||||
void print(int* heap, int _index) const;
|
||||
void grow(int*& heap, int _size);
|
||||
void siftUp(int* heap, int _index);
|
||||
void siftDown(int* heap, int currentMax);
|
||||
|
|
|
@ -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})
|
|
@ -65,4 +65,3 @@ int main()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ QueueList::~QueueList()
|
|||
/** enqueue
|
||||
* @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)
|
||||
{
|
||||
|
@ -158,6 +158,7 @@ void QueueList::makeEmpty()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Private Member Functions
|
||||
*****************************************************************************/
|
||||
|
|
|
@ -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})
|
|
@ -68,7 +68,7 @@ SingleList::~SingleList()
|
|||
/** insert
|
||||
* @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)
|
||||
{
|
||||
|
@ -81,7 +81,7 @@ bool SingleList::insert(int val)
|
|||
|
||||
/** insert at
|
||||
* @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 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
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
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
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
SingleList::Node* SingleList::findPrevious(int val, Node *start) const
|
||||
|
|
|
@ -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})
|
|
@ -65,4 +65,3 @@ int main()
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ StackList::~StackList()
|
|||
/** push
|
||||
* @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)
|
||||
{
|
||||
|
@ -146,7 +146,6 @@ bool StackList::isEmpty() const
|
|||
return head == NULL;
|
||||
}
|
||||
|
||||
|
||||
/** print
|
||||
* @brief Output the data held by the StackList object
|
||||
* Calls to the private print()
|
||||
|
@ -180,6 +179,12 @@ bool StackList::push(int val, Node *&head)
|
|||
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)
|
||||
{
|
||||
// We already know the stack is not empty from public pop()
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
##############################################################################
|
||||
## vector.cpp
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
# Define the project name
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
/******************************************************************************
|
||||
* Constructors, Destructors, Operators
|
||||
*****************************************************************************/
|
||||
// TODO: Fix const, destr, op=
|
||||
|
||||
/** copy constructor
|
||||
* @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--]
|
||||
* 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()
|
||||
{
|
||||
|
@ -101,7 +100,7 @@ int Vector::pop()
|
|||
val = pop(data);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -134,8 +133,6 @@ int Vector::peek() const
|
|||
return val;
|
||||
}
|
||||
|
||||
// TODO: Verify that isEmpty works by actually checking data == NULL
|
||||
|
||||
/** isEmpty
|
||||
* @brief Determine if the Vector is empty
|
||||
*
|
||||
|
@ -144,7 +141,7 @@ int Vector::peek() const
|
|||
*/
|
||||
bool Vector::isEmpty() const
|
||||
{
|
||||
return curIndex <= -1;
|
||||
return data == NULL;
|
||||
}
|
||||
|
||||
/** isFull
|
||||
|
@ -214,6 +211,7 @@ int Vector::getValue(int index) const
|
|||
return data[index];
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Private Member Functions
|
||||
*****************************************************************************/
|
||||
|
@ -261,8 +259,7 @@ int Vector::pop(int *&data)
|
|||
/** makeEmpty
|
||||
* @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)
|
||||
{
|
||||
|
@ -275,7 +272,7 @@ void Vector::makeEmpty(int *&data)
|
|||
/** peek
|
||||
* @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
|
||||
*/
|
||||
int Vector::peek(int *data) const
|
||||
|
@ -286,6 +283,8 @@ int Vector::peek(int *data) const
|
|||
|
||||
/** print
|
||||
* @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
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue