diff --git a/Data Structures C++/Queue.h b/Data Structures C++/Queue.h new file mode 100644 index 0000000..80e5773 --- /dev/null +++ b/Data Structures C++/Queue.h @@ -0,0 +1,163 @@ +#pragma once + +#include "stdafx.h" + +/* + Queues use FIFO (First In, First Out) ordering + LILO (Last In, Last Out) + + Ex: car wash, midnight release of a game, a line at an amusement park, etc. + +*/ + + +// Keeps the class generic +//typedef int Item; + + +template +class Queue +{ +private: + + struct Node + { + Item m_data; // The value to store + Node* m_next; // The next one in the list + + // Ctor + Node(const Item& _info); + }; + + Node* m_head; // The oldest node in the list + Node* m_tail; // The newest node in the list + + int m_currSize; // Current number of nodes allocated + const int m_maxSize;// Max number to store at any given time + +public: + + // Def ctor + // In: _maxSize The max size for *THIS* particular queue + Queue(int _maxSize = 0); // 0 will represent unlimited + + // Dtor + ~Queue(); + +private: + + // Disabled copy ctor + Queue(const Queue& _q) : m_maxSize(0) { } + + // Disabled assignment operator + Queue& operator=(const Queue& q) { return *this; } + +public: + + + + int GetCurrSize() const { return m_currSize; } + + // Add an Item to the end of the queue + // In: _info The Item to add + // + // Return: True, if something was added + bool Enqueue(const Item& _info); + + // Remove the Item from the front of the queue + // In: _info A "blank" Item + // + // Out: _info The Item at the front + // Return: True, if something was removed + bool Dequeue(Item& _info); +}; + +// Ctor +template +Queue::Node::Node(const Item& _info) +{ + m_data = _info; + m_next = NULL; +} + +// Def ctor +// +// Const data members have to use the member initializer +template +Queue::Queue(int _maxSize) : m_maxSize(_maxSize) +{ + m_head = m_tail = NULL; + m_currSize = 0; +} + +// Dtor +template +Queue::~Queue() +{ + for (Node* temp = m_head; temp; temp = m_head) + { + m_head = m_head->m_next; + delete temp; + } +} + + +// Add an Item to the end of the queue +// In: _info The Item to add +// +// Return: True, if something was added +template +bool Queue::Enqueue(const Item& _info) +{ + // Is the list full? + if (m_maxSize != 0 && m_maxSize == m_currSize) + return false; + + // Allocate space to store the information + Node* newest = new Node(_info); + + // Did we run out of memory? + if (NULL == newest) + return false; + + // If the list is currently empty, set the head and tail pointer + if (!m_head) + { + m_head = newest; + m_tail = newest; + } + // Otherwise, go to the end of the list + else + { + m_tail->m_next = newest; + m_tail = newest; + } + + ++m_currSize; + return true; + +} + +// Remove the Item from the front of the queue +// In: _info A "blank" Item +// +// Out: _info The Item at the front +// Return: True, if something was removed +template +bool Queue::Dequeue(Item& _info) +{ + // If the list is empty, GTFO + if (!m_head) + return false; + + // Copy over our value + _info = m_head->m_data; + + // Update the list + Node* temp = m_head; + m_head = m_head->m_next; + delete temp; + + m_currSize--; + return true; +} \ No newline at end of file diff --git a/Data Structures C++/SLList.h b/Data Structures C++/SLList.h new file mode 100644 index 0000000..20348ac --- /dev/null +++ b/Data Structures C++/SLList.h @@ -0,0 +1,243 @@ +#pragma once +template class SLLIter; + +template +class SLList +{ +private: + + struct Node + { + Type Data; + Node* m_next = nullptr; + Node(){ Data = nullptr; } + Node(Type _data){ Data = _data; } + }; + Node* m_head; + int m_size; +public: + friend class SLLIter; + SLList(); + ~SLList(); + SLList& operator=(const SLList& that); + SLList(const SLList& that); + void addHead(const Type& v); + void clear(); + void insert(SLLIter& index, const Type& v); + void remove(SLLIter& index); + unsigned int size() const{ return m_size; } + + +}; +template +SLList::SLList() +{ + m_head = nullptr; + m_size = 0; + +} +template +SLList::~SLList() +{ + clear(); +} +template +SLList& SLList::operator=(const SLList& that) +{ + if (this != &that ) + { + clear(); + if (that.m_size > 0) + { + + m_head = new Node(that.m_head->Data); + Node* temp2; + Node* temp3; + temp2 = that.m_head->m_next; + temp3 = m_head; + for (int loop = 0; loop < that.m_size - 1; loop++) + { + Node* temp = new Node(temp2->Data); + temp2 = temp2->m_next; + temp3->m_next = temp; + temp3 = temp3->m_next; + + + } + m_size = that.m_size; + } + } + return *this; +} +template +SLList::SLList(const SLList& that) +{ + if (m_size > 0) + clear(); + if (that.m_size > 0) + { + + m_head = new Node(that.m_head->Data); + Node* temp2; + Node* temp3; + temp2 = that.m_head->m_next; + temp3 = m_head; + for (int loop = 0; loop < that.m_size - 1; loop++) + { + Node* temp = new Node(temp2->Data); + temp2 = temp2->m_next; + temp3->m_next = temp; + temp3 = temp3->m_next; + + + } + m_size = that.m_size; + } + +} +template +void SLList::addHead(const Type& v) +{ + Node* temp = new Node(v); + temp->m_next = m_head; + m_head = temp; + m_size++; +} +template +void SLList::clear() +{ + if (m_head != nullptr) + { + Node* temp; + temp = m_head; + while (m_head) + { + m_head = m_head->m_next; + delete temp; + temp = m_head; + m_size--; + } + } +} + +template +void SLList::insert(SLLIter& index, const Type& v) +{ + if (m_size > 0) + { + Node* temp = new Node(v); + if (index.m_cur == nullptr) + { + if (index.m_prev != nullptr) + { + index.m_prev->m_next = temp; + } + else + delete temp; + } + else if (index.m_cur == m_head) + { + + temp->m_next = index.m_cur; + m_head = temp; + index.m_cur = m_head; + + } + else + { + index.m_prev->m_next = temp; + temp->m_next = index.m_cur; + index.m_cur = index.m_prev->m_next; + } + m_size++; + } +} + +template +void SLList::remove(SLLIter& index) +{ + if (index.m_cur != nullptr || index.m_prev != nullptr) + { + if (m_size > 0) + { + if (index.m_cur == m_head) + { + index.m_cur = index.m_cur->m_next; + delete m_head; + m_head = index.m_cur; + } + + else if (index.m_cur == nullptr) + { + delete index.m_prev->m_next; + index.m_prev = nullptr; + } + else + { + index.m_prev->m_next = index.m_cur->m_next; + delete index.m_cur; + index.m_cur = index.m_prev->m_next; + } + m_size--; + } + } +} + +template +class SLLIter +{ +private: + typename SLList::Node * m_prev, *m_cur; + typename SLList& m_temp; + friend class SLList; + int count = 0; +public: + + SLLIter(SLList& listToIterate); + void begin(); + bool end() const; + SLLIter& operator++(); + Type& current() const; +}; + +template +SLLIter::SLLIter(SLList& listToIterate) : m_temp(listToIterate) +{ + m_cur = m_temp.m_head; + m_prev = nullptr; +} +template +void SLLIter::begin() +{ + m_prev = nullptr; + m_cur = m_temp.m_head; +} +template +bool SLLIter::end() const +{ + + if (m_cur == nullptr) + return true; + else + return false; +} +template +SLLIter& SLLIter::operator++() +{ + if (m_cur != nullptr) + { + m_cur = m_cur->m_next; + + if (m_prev == nullptr) + m_prev = m_temp.m_head; + else + m_prev = m_prev->m_next; + } + + return *this; +} +template +Type& SLLIter::current() const +{ + return m_cur->Data; +} \ No newline at end of file diff --git a/Data Structures C++/Stack.h b/Data Structures C++/Stack.h new file mode 100644 index 0000000..9cb1342 --- /dev/null +++ b/Data Structures C++/Stack.h @@ -0,0 +1,124 @@ +#pragma once +#include "Card.h" + + + +typedef Card Item; + +// A singly linked list with FILO ordering +template +class Stack +{ +private: + + struct Node + { + Item m_data; + Node* m_next; + }; + + Node* m_top; + int m_currSize; + +public: + + // Default ctor + Stack(); + + // Dtor + ~Stack(); + + + int GetCurrSize() const { return m_currSize; } + + + bool Push(const Item& _info); + + + bool Pop(Item& _info); + + + void Clear(); + + + const Item* Peek() const; +}; + +// Default ctor +template +Stack::Stack() +{ + m_currSize = 0; + m_top = NULL; +} + +// Dtor +template +Stack::~Stack() +{ + Clear(); +} + +template +bool Stack::Push(const Item& _info) +{ + // Make space for this new data + Node* newTop = new Node; + + // Did new fail? + if (!newTop) + return false; + + // Fill in the node + newTop->m_data = _info; + newTop->m_next = m_top; + + // Update the top pointer + m_top = newTop; + + m_currSize++; + return true; +} + +template +bool Stack::Pop(Item& _info) +{ + // Is the list empty? + if (NULL == m_top) + return false; + + // Copy over the value + _info = m_top->m_data; + + // Update the stack + Node* temp = m_top; + m_top = m_top->m_next; + delete temp; + + m_currSize--; + return true; +} + +// Empty out the stack for re-use +template +void Stack::Clear() +{ + Node* temp = m_top; + + // Loop until all nodes have been de + while (m_top) + { + m_top = m_top->m_next; + delete temp; + temp = m_top; + } + + m_currSize = 0; +} + +// Look at the top thing without removing it +template +const Item* Stack::Peek() const +{ + return (m_top ? &m_top->m_data : NULL); +} \ No newline at end of file