mirror of
https://github.com/zach-sb/SENG1120-Assignment-1.git
synced 2025-11-09 05:17:38 +11:00
1
This commit is contained in:
parent
5ad212de53
commit
3d42110395
116
LinkedList.cpp
116
LinkedList.cpp
@ -6,7 +6,7 @@ Last Modified: 16/09/2020
|
|||||||
|
|
||||||
Description:
|
Description:
|
||||||
*/
|
*/
|
||||||
|
//Includes
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "LinkedList.h"
|
#include "LinkedList.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -21,7 +21,7 @@ LinkedList::LinkedList()
|
|||||||
LinkedList::~LinkedList()
|
LinkedList::~LinkedList()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
//Add to list functions
|
||||||
void LinkedList::addToHead(const valueType& data)
|
void LinkedList::addToHead(const valueType& data)
|
||||||
{
|
{
|
||||||
Node* newNode = new Node(data);
|
Node* newNode = new Node(data);
|
||||||
@ -85,7 +85,6 @@ void LinkedList::addToTail(const valueType& data)
|
|||||||
|
|
||||||
|
|
||||||
//Data Mutators
|
//Data Mutators
|
||||||
|
|
||||||
void LinkedList::add(const valueType addString)
|
void LinkedList::add(const valueType addString)
|
||||||
{
|
{
|
||||||
valueType word;
|
valueType word;
|
||||||
@ -126,6 +125,45 @@ void LinkedList::add(const valueType addString)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LinkedList::remove(const valueType removeString)
|
||||||
|
{
|
||||||
|
Node* temp=head;
|
||||||
|
Node* tempToDel;
|
||||||
|
|
||||||
|
for (int i = 0; i<getWordCount(); i++)
|
||||||
|
{
|
||||||
|
if ((temp==head)&&(temp->getData()==removeString))
|
||||||
|
{
|
||||||
|
temp->getNext()->setPrev(NULL);
|
||||||
|
head=temp->getNext();
|
||||||
|
tempToDel = temp;
|
||||||
|
temp=temp->getNext();
|
||||||
|
delete tempToDel;
|
||||||
|
}
|
||||||
|
else if ((temp==tail)&&(temp->getData()==removeString))
|
||||||
|
{
|
||||||
|
temp->getPrev()->setNext(NULL);
|
||||||
|
tail=temp->getPrev();
|
||||||
|
tempToDel = temp;
|
||||||
|
temp=temp->getNext();
|
||||||
|
delete tempToDel;
|
||||||
|
}
|
||||||
|
else if (temp->getData()==removeString)
|
||||||
|
{
|
||||||
|
//Connect the nodes around Temp
|
||||||
|
temp->getPrev()->setNext(temp->getNext());
|
||||||
|
temp->getNext()->setPrev(temp->getPrev());
|
||||||
|
|
||||||
|
tempToDel = temp;
|
||||||
|
temp=temp->getNext();
|
||||||
|
delete tempToDel;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
temp=temp->getNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//Queries
|
//Queries
|
||||||
LinkedList::valueType LinkedList::getNodeData(int wordNum) const
|
LinkedList::valueType LinkedList::getNodeData(int wordNum) const
|
||||||
@ -195,6 +233,21 @@ LinkedList::valueType LinkedList::buildString() const
|
|||||||
return sentence;
|
return sentence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int LinkedList::count(valueType countString)
|
||||||
|
{
|
||||||
|
Node* temp=head;
|
||||||
|
int count=0;
|
||||||
|
|
||||||
|
for (int i = 0; i<getWordCount(); i++)
|
||||||
|
{
|
||||||
|
if (temp->getData()==countString)
|
||||||
|
count++;
|
||||||
|
temp = temp->getNext();
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Overload Operators
|
||||||
std::ostream& operator << (std::ostream& out, const LinkedList& list)
|
std::ostream& operator << (std::ostream& out, const LinkedList& list)
|
||||||
{
|
{
|
||||||
std::cout<<list.buildString();
|
std::cout<<list.buildString();
|
||||||
@ -214,60 +267,6 @@ void LinkedList::operator += (LinkedList& list2)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int LinkedList::count(valueType countString)
|
|
||||||
{
|
|
||||||
Node* temp=head;
|
|
||||||
int count=0;
|
|
||||||
|
|
||||||
for (int i = 0; i<getWordCount(); i++)
|
|
||||||
{
|
|
||||||
if (temp->getData()==countString)
|
|
||||||
count++;
|
|
||||||
temp = temp->getNext();
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void LinkedList::remove(const valueType removeString)
|
|
||||||
{
|
|
||||||
Node* temp=head;
|
|
||||||
Node* tempToDel;
|
|
||||||
|
|
||||||
for (int i = 0; i<getWordCount(); i++)
|
|
||||||
{
|
|
||||||
if ((temp==head)&&(temp->getData()==removeString))
|
|
||||||
{
|
|
||||||
temp->getNext()->setPrev(NULL);
|
|
||||||
head=temp->getNext();
|
|
||||||
tempToDel = temp;
|
|
||||||
temp=temp->getNext();
|
|
||||||
delete tempToDel;
|
|
||||||
}
|
|
||||||
else if ((temp==tail)&&(temp->getData()==removeString))
|
|
||||||
{
|
|
||||||
temp->getPrev()->setNext(NULL);
|
|
||||||
tail=temp->getPrev();
|
|
||||||
tempToDel = temp;
|
|
||||||
temp=temp->getNext();
|
|
||||||
delete tempToDel;
|
|
||||||
}
|
|
||||||
else if (temp->getData()==removeString)
|
|
||||||
{
|
|
||||||
//Connect the nodes around Temp
|
|
||||||
temp->getPrev()->setNext(temp->getNext());
|
|
||||||
temp->getNext()->setPrev(temp->getPrev());
|
|
||||||
|
|
||||||
tempToDel = temp;
|
|
||||||
temp=temp->getNext();
|
|
||||||
delete tempToDel;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
temp=temp->getNext();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void LinkedList::sort()
|
void LinkedList::sort()
|
||||||
{
|
{
|
||||||
Node* temp=head;
|
Node* temp=head;
|
||||||
@ -309,5 +308,4 @@ void LinkedList::sort()
|
|||||||
}while(temp->getNext()!=NULL);
|
}while(temp->getNext()!=NULL);
|
||||||
|
|
||||||
std::cout<<std::endl;
|
std::cout<<std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user