mirror of
https://github.com/zach-sb/SENG1120-Assignment-1.git
synced 2025-11-09 02:27:38 +11:00
291 lines
4.8 KiB
C++
291 lines
4.8 KiB
C++
/*
|
|
Name: LinkedList.cpp
|
|
Author: Zach Seibel-Barnes
|
|
Student Number: c3262201
|
|
Last Modified: 16/09/2020
|
|
|
|
Description:
|
|
*/
|
|
//Includes
|
|
#include <iostream>
|
|
#include "LinkedList.h"
|
|
#include <string>
|
|
|
|
LinkedList::LinkedList()
|
|
{
|
|
head = NULL;
|
|
current = NULL;
|
|
tail = NULL;
|
|
}
|
|
|
|
LinkedList::~LinkedList()
|
|
{}
|
|
|
|
//Add to list functions
|
|
void LinkedList::addToHead(const valueType& data)
|
|
{
|
|
Node* newNode = new Node(data);
|
|
|
|
if(head==NULL)
|
|
{
|
|
head = newNode;
|
|
tail = newNode;
|
|
}
|
|
else
|
|
{
|
|
head->setPrev(newNode);
|
|
newNode->setNext(head);
|
|
head = newNode;
|
|
}
|
|
}
|
|
|
|
void LinkedList::addToCurrent(const valueType& data)
|
|
{
|
|
Node* newNode = new Node(data);
|
|
|
|
if(head==NULL)
|
|
{
|
|
head = newNode;
|
|
tail = newNode;
|
|
}
|
|
|
|
else if (head==tail)
|
|
{
|
|
newNode->setPrev(head);
|
|
tail = newNode;
|
|
head->setNext(newNode);
|
|
}
|
|
|
|
else
|
|
{
|
|
newNode->setNext(current);
|
|
newNode->setPrev(current->getPrev());
|
|
|
|
current->setPrev(newNode);
|
|
current->getPrev()->setNext(newNode);
|
|
}
|
|
current = head;
|
|
}
|
|
|
|
void LinkedList::addToTail(const valueType& data)
|
|
{
|
|
Node* newNode = new Node(data);
|
|
|
|
if(head==NULL)
|
|
{
|
|
head = newNode;
|
|
}
|
|
else
|
|
{
|
|
tail->setNext(newNode);
|
|
newNode->setPrev(tail);
|
|
}
|
|
tail = newNode;
|
|
}
|
|
|
|
|
|
//Data Mutators
|
|
void LinkedList::add(const valueType addString)
|
|
{
|
|
valueType word;
|
|
current = head;
|
|
|
|
int stringLength=addString.length();
|
|
|
|
int spaceCount = 0;
|
|
int spacePos=0;
|
|
int spacePosNext=addString.find(' ');
|
|
|
|
//Count the number of spaces in the input string.
|
|
for (int i = 0; i < stringLength; i++)
|
|
{
|
|
if (addString[i]==' ')
|
|
spaceCount++;
|
|
}
|
|
|
|
for (int i=0; i <= spaceCount;i++)
|
|
{
|
|
//Check if we have reached the end of the string
|
|
if (spacePosNext== -1)
|
|
spacePosNext=stringLength;
|
|
|
|
//Build the Word Letter by letter, using spaces to define it
|
|
for (int j = spacePos; j<spacePosNext; j++)
|
|
{
|
|
word+=addString[j];
|
|
}
|
|
|
|
addToTail(word);
|
|
|
|
word.clear();
|
|
|
|
//Move the 'space' boundary markers to either side of the next word.
|
|
spacePos=spacePosNext+1;
|
|
spacePosNext= addString.find(' ',spacePosNext+1);
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
Node* n2; //The node after the starting Node
|
|
valueType nSwap;
|
|
|
|
bool swapFlag; //flag for if a swap happend this loop
|
|
|
|
do
|
|
{
|
|
swapFlag=0;
|
|
for (Node* n1=head; n1->getNext()!=NULL; n1=n1->getNext())
|
|
{
|
|
//Name the next node for readability
|
|
n2 = n1->getNext();
|
|
|
|
if(n1->getData() > n2->getData())
|
|
{
|
|
//std::cout<<"SWAP"<<std::endl;
|
|
|
|
nSwap=n2->getData();
|
|
n2->setData(n1->getData());
|
|
n1->setData(nSwap);
|
|
swapFlag = 1;
|
|
}
|
|
}
|
|
} while (swapFlag == 1);
|
|
}
|
|
|
|
//Queries
|
|
LinkedList::valueType LinkedList::getNodeData(int wordNum) const
|
|
{
|
|
Node* temp=head;
|
|
|
|
for (int i = 0; i<wordNum; i++)
|
|
{
|
|
temp=temp->getNext();
|
|
}
|
|
|
|
return temp->getData();
|
|
}
|
|
|
|
int LinkedList::currentSelect(char option)
|
|
{
|
|
int result = 0;
|
|
|
|
switch(option){
|
|
case 'h':
|
|
current = head;
|
|
break;
|
|
case 'n':
|
|
if(current->getNext()==NULL)
|
|
result=1;
|
|
else
|
|
current = current->getNext();
|
|
break;
|
|
case 'p':
|
|
if(current->getPrev()==NULL)
|
|
result=2;
|
|
else
|
|
current = current->getPrev();
|
|
break;
|
|
case 't':
|
|
current = tail;
|
|
break;
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
int LinkedList::getWordCount() const
|
|
{
|
|
Node* temp = head;
|
|
int count = 0;
|
|
for (temp = head; temp != NULL; temp = temp->getNext())
|
|
{
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
LinkedList::valueType LinkedList::buildString() const
|
|
{
|
|
valueType sentence;
|
|
Node* temp = head;
|
|
|
|
for (temp = head; temp != NULL; temp = temp->getNext())
|
|
{
|
|
if (temp == head)
|
|
sentence+=temp->getData();
|
|
else
|
|
sentence=sentence+" "+temp->getData();
|
|
}
|
|
|
|
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::cout<<list.buildString();
|
|
return out;
|
|
}
|
|
|
|
void LinkedList::operator += (LinkedList& list2)
|
|
{
|
|
for (int i=0; i<list2.getWordCount();i++)
|
|
{
|
|
addToTail(list2.getNodeData(i));
|
|
}
|
|
}
|