Commit 00e229d4 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Project 2

parent 46a997b5
#ifndef LINKED_LIST_HPP
#define LINKED_LIST_HPP
#include "../Exceptions/NotImplementedException.hpp"
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
template <typename T>
struct Node
{
public:
Node();
Node<T>* m_ptrNext;
Node<T>* m_ptrPrev;
T m_data;
};
template <typename T>
class LinkedList
{
private:
/* Member Variables */
//! A pointer to the first item in the list
Node<T>* m_ptrFirst;
//! A pointer to the last item in the list
Node<T>* m_ptrLast;
//! The amount of items stored in the list
int m_itemCount;
public:
/* Member Functions */
LinkedList();
~LinkedList();
//! Add a new item to the front of the list
void PushFront( T newData );
//! Add a new item to the back of the list
void PushBack( T newData );
//! Remove the front-most item
void PopFront() noexcept;
//! Remove the last item
void PopBack() noexcept;
//! Get the data of the front-most item
T& GetFront();
//! Get the data of the back-most item
T& GetBack();
//! Subscript operator to get an item at an arbitrary index
T& operator[]( const int index );
//! Clear all items out of the list
void Clear();
//! Returns true if the list is empty, or false otherwise
bool IsEmpty();
//! Returns the amonut of items stored in the list
int Size();
//! Returns whether the given index is invalid.
bool IsInvalidIndex( int index ) const;
void Panic( string message ) const;
void NotImplemented( string functionName ) const;
friend class Tester;
};
/**
Set the next and previous pointers to nullptr
*/
template <typename T>
Node<T>::Node()
{
m_ptrNext = nullptr;
m_ptrPrev = nullptr;
}
/* -----------------------------------------------------------------------------------------*/
/* ----------------------------------------------------------------------------- LinkedList */
/**
- Initialize the first and last pointers to nullptr.
- Set the item count to 0.
*/
template <typename T>
LinkedList<T>::LinkedList()
{
m_ptrFirst = nullptr;
m_ptrLast = nullptr;
m_itemCount = 0;
}
/* -----------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------- ~LinkedList */
/**
Call the Clear function
*/
template <typename T>
LinkedList<T>::~LinkedList()
{
// Make sure to call the Clear function after Clear has been implemented!!
// Clear();
}
/* -----------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------------- Clear */
/**
@return void
- While the list is not empty...
- Pop the front most item.
*/
template <typename T>
void LinkedList<T>::Clear() /* Clear */
{
NotImplemented( "Clear" );
}
/* -----------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------ PushFront */
/**
@param T newData New item to add to the list
@return void
Add a new item to the beginning of the list.
- Create a Node<T>* pointer, and allocate new memory via it.
- Set the new node's data to the newData.
- Increment the item count
- If the list is currently empty...
- Set the first and last pointers to this new pointer.
- Otherwise...
- Set the first pointer's previous item to this new pointer.
- Set the new node's next pointer to the first item.
- Update the first pointer to point to the new item.
*/
template <typename T>
void LinkedList<T>::PushFront( T newData )
{
NotImplemented( "PushFront" );
}
/* -----------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------- PushBack */
/**
@param T newData New item to add to the list
@return void
Add a new item to the end of the list.
- Create a Node<T>* pointer, and allocate new memory via it.
- Set the new node's data to the newData.
- Increment the item count
- If the list is currently empty...
- Set the first and last pointers to this new pointer.
- Otherwise...
- Set the last pointer's next item to this new pointer.
- Set the new node's previous pointer to the last item.
- Update the last pointer to point to the new item.
*/
template <typename T>
void LinkedList<T>::PushBack( T newData )
{
NotImplemented( "PushBack" );
}
/* -----------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------- PopFront */
/**
@return void
Remove the first-most item in the list, and update the m_ptrFirst to point
to the new first item.
- If the list is Empty... do nothing
- Else, if there is only one item in the list:
- Delete (either the first or last) node in the list (only one).
- Set the first and last pointer to nullptr
- Decrement the item count
- Else:
- Create a pointer to point to the second item.
- Set the second item's previous pointer to nullptr.
- Delete the first item
- Set the m_ptrFirst pointer to that second item.
- Decrement the item count
*/
template <typename T>
void LinkedList<T>::PopFront() noexcept
{
NotImplemented( "PopFront" );
}
/* -----------------------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------------- PopBack */
/**
@return void
Remove the last-most item in the list, and update the m_ptrLast to point
to the new last item.
- If the list is Empty... do nothing
- Else, if there is only one item in the list:
- Delete (either the first or last) node in the list (only one).
- Set the first and last pointer to nullptr
- Decrement the item count
- Else:
- Create a pointer to point to the second-to-last item.
- Set the second-to-last item's next pointer to nullptr.
- Delete the last item
- Set the m_ptrLast pointer to that second-to-last item.
- Decrement the item count
*/
template <typename T>
void LinkedList<T>::PopBack() noexcept
{
NotImplemented( "PopBack" );
}
/* -----------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------- GetFront */
/**
@return T& The front item in the list is returned
Error checks:
- If the list is empty, throw an exception.
Otherwise, return the data that belongs to the front pointer (m_ptrFirst).
*/
template <typename T>
T& LinkedList<T>::GetFront()
{
NotImplemented( "GetFront" );
return m_ptrFirst->m_data; // TEMPORARY
}
/* -----------------------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------------- GetBack */
/**
@return T& The back item in the list is returned
Error checks:
- If the list is empty, throw an exception.
Otherwise, return the data that belongs to the last pointer (m_ptrLast).
*/
template <typename T>
T& LinkedList<T>::GetBack()
{
NotImplemented( "GetBack" );
return m_ptrFirst->m_data; // TEMPORARY
}
/* -----------------------------------------------------------------------------------------*/
/* ----------------------------------------------------------------------------- operator[] */
/**
@param int index The position of the item to return.
@return T& Returns the item at the given index.
Error checks:
- If the list is empty, throw an exception.
- If the index is invalid, throw an exception.
Functionality:
You'll need to traverse through the list, starting at the beginning and moving forward, one-by-one.
- Create a Node<T>* "walker" pointer to walk through the items. Start it at the m_ptrFirst position.
- Make a loop that will loop 'index' amount of times. Within the loop...
- Move your walking pointer forward by one (current = current->m_ptrNext)
- Once done, return the data of the item (access via the walker pointer.)
*/
template <typename T>
T& LinkedList<T>::operator[]( const int index )
{
NotImplemented( "operator[]" );
return m_ptrFirst->m_data; // TEMPORARY
}
/* -----------------------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------------- IsEmpty */
/**
@return bool Return true if there are no items stored in the list, and false otherwise.
*/
template <typename T>
bool LinkedList<T>::IsEmpty()
{
NotImplemented( "IsEmpty" );
return false; // TEMPORARY
}
/* -----------------------------------------------------------------------------------------*/
/* ----------------------------------------------------------------------------------- Size */
/**
@return int The amount of items stored in the List. Use m_itemCount here.
*/
template <typename T>
int LinkedList<T>::Size()
{
NotImplemented( "Size" );
return -1; // TEMPORARY
}
/* -----------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------- IsInvalidIndex */
/**
@param int index The index to look at.
@return bool true if invalid index (less than 0 or >= m_arraySize),
or false if not invalid.
*/
//! Check to see if a given index is invalid (i.e., negative).
template <typename T>
bool LinkedList<T>::IsInvalidIndex( int index ) const
{
NotImplemented( "IsInvalidIndex" );
return false; // TEMPORARY
}
/* ****************************************************************************/
/* ************************************************* FUNCTION TO THROW ERRORS */
/* ****************************************************************************/
/* -----------------------------------------------------------------------------------------*/
/* ---------------------------------------------------------------------------------- Panic */
//! Call this function if something terrible goes wrong.
template <typename T>
void LinkedList<T>::Panic(string message) const
{
throw runtime_error( message );
}
/* -----------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------- NotImplemented */
//! Marks when a function hasn't been implemented yet.
template <typename T>
void LinkedList<T>::NotImplemented( string functionName ) const
{
throw NotImplementedException( functionName + " function not implemented yet!" );
}
#endif
#ifndef _NOT_IMPLEMENTED_EXCEPTION
#define _NOT_IMPLEMENTED_EXCEPTION
#include <stdexcept>
#include <string>
using namespace std;
class NotImplementedException : public runtime_error
{
public:
NotImplementedException( string functionName )
: runtime_error( functionName.c_str() ) { ; }
};
#endif
#ifndef _PROGRAM_HPP
#define _PROGRAM_HPP
#endif
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="CS250-Project02-List" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/CS250-Project01-Vector" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/CS250-Project01-Vector" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="../DataStructure/LinkedList.hpp" />
<Unit filename="../Exceptions/NotImplementedException.hpp" />
<Unit filename="../Program.hpp" />
<Unit filename="../Tester.hpp" />
<Unit filename="../cuTEST/Menu.hpp" />
<Unit filename="../cuTEST/StringUtil.hpp" />
<Unit filename="../cuTEST/TesterBase.cpp" />
<Unit filename="../cuTEST/TesterBase.hpp" />
<Unit filename="../main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="CS250-Project02-List" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/CS250-Project01-Vector" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/CS250-Project01-Vector" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="../DataStructure/LinkedList.hpp" />
<Unit filename="../Exceptions/NotImplementedException.hpp" />
<Unit filename="../Program.hpp" />
<Unit filename="../Tester.hpp" />
<Unit filename="../cuTEST/Menu.hpp" />
<Unit filename="../cuTEST/StringUtil.hpp" />
<Unit filename="../cuTEST/TesterBase.cpp" />
<Unit filename="../cuTEST/TesterBase.hpp" />
<Unit filename="../main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1600668541 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/cuTEST/TesterBase.cpp
"TesterBase.hpp"
"StringUtil.hpp"
<iostream>
<iomanip>
<cstdlib>
"Menu.hpp"
1600668536 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/cuTEST/TesterBase.hpp
<string>
<list>
<vector>
<functional>
<fstream>
1600668543 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/cuTEST/StringUtil.hpp
<string>
<sstream>
1587657999 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/cuTEST/Menu.hpp
<iostream>
<string>
<vector>
<cstdlib>
<limits>
1587657999 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/main.cpp
"Tester.hpp"
"Program.hpp"
"cuTEST/Menu.hpp"
"DataStructure/LinkedList.hpp"
<iostream>
1600671003 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/Tester.hpp
<iostream>
<string>
"cuTEST/TesterBase.hpp"
"cuTEST/Menu.hpp"
"cuTEST/StringUtil.hpp"
"DataStructure/LinkedList.hpp"
"Exceptions/NotImplementedException.hpp"
1600670277 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/DataStructure/LinkedList.hpp
"../Exceptions/NotImplementedException.hpp"
<iostream>
<string>
<stdexcept>
1600669193 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/Exceptions/NotImplementedException.hpp
<stdexcept>
<string>
1587657999 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/Program.hpp
1600668541 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/Project 2 - Linked List Starter/cuTEST/TesterBase.cpp
"TesterBase.hpp"
"StringUtil.hpp"
<iostream>
<iomanip>
<cstdlib>
"Menu.hpp"
1600668536 /home/wilsha/RACHEL/_ADULTING/TEACHING/PRIVATE/cs-250-data-structures/2020/Projects/Project 2 - Linked List/Project 2 - Linked List Starter/cuTEST/TesterBase.hpp
<string>
<list>