mirror of
https://github.com/zach-sb/SENG1120-Assignment-1.git
synced 2025-11-09 02:37:37 +11:00
FIrst Commit
This commit is contained in:
commit
5ad212de53
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
Assignment1-s2-2020.pdf
|
||||
Assignment1-s2-2020_Marking.pdf
|
||||
313
LinkedList.cpp
Normal file
313
LinkedList.cpp
Normal file
@ -0,0 +1,313 @@
|
||||
/*
|
||||
Name: LinkedList.cpp
|
||||
Author: Zach Seibel-Barnes
|
||||
Student Number: c3262201
|
||||
Last Modified: 16/09/2020
|
||||
|
||||
Description:
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "LinkedList.h"
|
||||
#include <string>
|
||||
|
||||
LinkedList::LinkedList()
|
||||
{
|
||||
head = NULL;
|
||||
current = NULL;
|
||||
tail = NULL;
|
||||
}
|
||||
|
||||
LinkedList::~LinkedList()
|
||||
{}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
//Test
|
||||
//removeWord("was");
|
||||
//std::cout<<std::endl<<"TEST: "<<buildString()<<std::endl;
|
||||
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
Node* temp=head;
|
||||
Node* nextTemp;
|
||||
bool swapped = 0;
|
||||
|
||||
do{
|
||||
|
||||
nextTemp=temp->getNext();
|
||||
|
||||
if (temp<nextTemp)
|
||||
std::cout<<"SWAP";
|
||||
|
||||
/*
|
||||
if (temp->getData()<nextTemp->getData())
|
||||
{
|
||||
nextTemp = temp->getNext();
|
||||
temp->setNext(nextTemp->getNext());
|
||||
nextTemp->setNext(temp);
|
||||
|
||||
nextTemp->setPrev(temp->getPrev());
|
||||
temp->setPrev(nextTemp);
|
||||
}
|
||||
*/
|
||||
/*
|
||||
nextTemp->setPrev(temp->getPrev());
|
||||
temp->setNext(nextTemp->getNext());
|
||||
|
||||
temp->setPrev(nextTemp);
|
||||
nextTemp->setNext(temp);
|
||||
*/
|
||||
|
||||
|
||||
std::cout<<(temp)<<": "<<temp->getData()<<": "<<swapped<<std::endl;
|
||||
temp = temp->getNext();
|
||||
|
||||
swapped=0;
|
||||
|
||||
}while(temp->getNext()!=NULL);
|
||||
|
||||
std::cout<<std::endl;
|
||||
}
|
||||
|
||||
64
LinkedList.h
Normal file
64
LinkedList.h
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
Name: LinkedList.h
|
||||
Author: Zach Seibel-Barnes
|
||||
Student Number: c3262201
|
||||
Last Modified: 16/09/2020
|
||||
|
||||
Description:
|
||||
*/
|
||||
|
||||
#ifndef GUARD_LINKEDLIST
|
||||
#define GUARD_LINKEDLIST
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "Node.h"
|
||||
|
||||
class LinkedList
|
||||
{
|
||||
public:
|
||||
|
||||
typedef Node::valueType valueType; //Continue with our typedef from Node.h
|
||||
|
||||
LinkedList(); //Constructor
|
||||
~LinkedList(); //deconstructor
|
||||
|
||||
//Accessors
|
||||
void addToHead(const valueType& data);
|
||||
void addToCurrent(const valueType& data);
|
||||
void addToTail(const valueType& data);
|
||||
|
||||
|
||||
//Data Mutators
|
||||
void add(const valueType addString);
|
||||
|
||||
void remove(const valueType removeString);
|
||||
|
||||
void sort();
|
||||
|
||||
//Queries
|
||||
int count(const valueType countString);
|
||||
|
||||
int currentSelect(char option='h');
|
||||
|
||||
valueType buildString() const;
|
||||
|
||||
valueType getNodeData(int wordNum) const;
|
||||
int getWordCount() const;
|
||||
|
||||
|
||||
void operator += (LinkedList& list);
|
||||
|
||||
private:
|
||||
Node* head;
|
||||
Node* tail;
|
||||
Node* current;
|
||||
|
||||
};
|
||||
|
||||
//Operator Overloading
|
||||
|
||||
std::ostream& operator << (std::ostream& out, const LinkedList& list);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
74
LinkedListDemo.cpp
Normal file
74
LinkedListDemo.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
//LinkedListDemo - Main Program
|
||||
/******************************/
|
||||
//Author: Alex Mendes
|
||||
//Course: SENG1120
|
||||
//Program Description: This program demonstrates the basic functionality of a linked list that stores strings.
|
||||
//It will demo the functions of a basic linked list.
|
||||
//The program adds content to a linked list, removes individual nodes, among other functionalities.
|
||||
//
|
||||
// v1.01 - corrected typo in bonus section.
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include "LinkedList.h"
|
||||
using namespace std;
|
||||
|
||||
void initialize(LinkedList &l1, LinkedList &l2)
|
||||
{
|
||||
l1.add("the black cat was sitting on the black mat that was on the black floor");
|
||||
l2.add("the dog scared the cat and the cat ran away");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
LinkedList firstList;
|
||||
LinkedList secondList;
|
||||
|
||||
initialize(firstList, secondList);
|
||||
|
||||
cout << "Start lists:" << endl;
|
||||
cout << "List 1: " << firstList << endl;
|
||||
cout << "List 2: " << secondList << endl << endl;
|
||||
|
||||
|
||||
cout << "Concatenating the two lists onto list '1':" << endl;
|
||||
firstList += secondList;
|
||||
cout << "List 1: " << firstList << endl;
|
||||
cout << "List 2: " << secondList << endl << endl;
|
||||
|
||||
cout << "Removing the word 'was' from list '1':" << endl;
|
||||
firstList.remove("was");
|
||||
cout << "List 1: " << firstList << endl;
|
||||
cout << "List 2: " << secondList << endl << endl;
|
||||
|
||||
cout << "Removing the word 'away' from list '2':" << endl;
|
||||
secondList.remove("away");
|
||||
cout << "List 1: " << firstList << endl;
|
||||
cout << "List 2: " << secondList << endl << endl;
|
||||
|
||||
cout << "Removing the word 'cat' from both lists:" << endl;
|
||||
firstList.remove("cat");
|
||||
secondList.remove("cat");
|
||||
cout << "List 1: " << firstList << endl;
|
||||
cout << "List 2: " << secondList << endl << endl;
|
||||
|
||||
|
||||
cout << "Number of occurrences of 'black' in list 1: ";
|
||||
cout << firstList.count("black") << endl << endl;
|
||||
|
||||
|
||||
// Uncomment this section if you are implementing the extended version of the method remove()
|
||||
// cout << "Removing 'on the black' from both lists:" << endl;
|
||||
// firstList.remove("on the black");
|
||||
// secondList.remove("on the black");
|
||||
// cout << "List 1: " << firstList << endl;
|
||||
// cout << "List 2: " << secondList << endl << endl;
|
||||
|
||||
cout << "Sorting list 1:" << endl;
|
||||
firstList.sort();
|
||||
cout << firstList << endl << endl;
|
||||
|
||||
cout << "The program has finished." << endl;
|
||||
return 0;
|
||||
}
|
||||
73
Node.cpp
Normal file
73
Node.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
Name: Node.cpp
|
||||
Author: Zach Seibel-Barnes
|
||||
Student Number: c3262201
|
||||
Last Modified: 16/09/2020
|
||||
|
||||
Description:
|
||||
*/
|
||||
|
||||
#include "Node.h"
|
||||
|
||||
//Construct the Node Class
|
||||
Node::Node(const valueType& initialData, Node* nextLink, Node* prevLink)
|
||||
{
|
||||
data=initialData;
|
||||
next=nextLink;
|
||||
prev=prevLink;
|
||||
}
|
||||
|
||||
Node::Node ()
|
||||
{
|
||||
next = NULL;
|
||||
prev = NULL;
|
||||
}
|
||||
|
||||
Node::~Node()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Getters
|
||||
|
||||
Node::valueType Node::getData() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
//Next
|
||||
Node* Node::getNext()
|
||||
{
|
||||
return next;
|
||||
}
|
||||
|
||||
const Node* Node::getNext() const
|
||||
{
|
||||
return next;
|
||||
}
|
||||
//Prev
|
||||
Node* Node::getPrev()
|
||||
{
|
||||
return prev;
|
||||
}
|
||||
|
||||
const Node* Node::getPrev() const
|
||||
{
|
||||
return prev;
|
||||
}
|
||||
|
||||
//Setters
|
||||
void Node::setData(const valueType& newData)
|
||||
{
|
||||
data = newData;
|
||||
}
|
||||
|
||||
void Node::setNext(Node* nextPtr)
|
||||
{
|
||||
next = nextPtr;
|
||||
}
|
||||
|
||||
void Node::setPrev(Node* prevPtr)
|
||||
{
|
||||
prev = prevPtr;
|
||||
}
|
||||
50
Node.h
Normal file
50
Node.h
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Name: node.cpp
|
||||
Author: Zach Seibel-Barnes
|
||||
Student Number: c3262201
|
||||
Last Modified: 16/09/2020
|
||||
|
||||
Description:
|
||||
*/
|
||||
|
||||
#ifndef GUARD_NODE
|
||||
#define GUARD_NODE
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
class Node
|
||||
{
|
||||
public:
|
||||
//Create typedef
|
||||
typedef std::string valueType;
|
||||
|
||||
//Constructor
|
||||
Node(const valueType& initialData, Node* nextLink=NULL, Node* prevLink=NULL); //Specific
|
||||
Node(); //General
|
||||
|
||||
//Destructor
|
||||
~Node();
|
||||
|
||||
|
||||
//Getters
|
||||
valueType getData () const;
|
||||
|
||||
Node* getNext();
|
||||
const Node* getNext() const;
|
||||
|
||||
Node* getPrev();
|
||||
const Node* getPrev() const;
|
||||
|
||||
//Setters
|
||||
void setData(const valueType& newData);
|
||||
void setNext(Node* nextPtr);
|
||||
void setPrev(Node* prevPtr);
|
||||
|
||||
private:
|
||||
valueType data;
|
||||
Node* next=NULL;
|
||||
Node* prev=NULL;
|
||||
};
|
||||
#endif
|
||||
19
a1.exe.stackdump
Normal file
19
a1.exe.stackdump
Normal file
@ -0,0 +1,19 @@
|
||||
Exception: STATUS_ACCESS_VIOLATION at rip=003FCCD3A85
|
||||
rax=0000000000000000 rbx=000000018023A780 rcx=00000000FFFFCB30
|
||||
rdx=0000000000000000 rsi=0000000000000000 rdi=0000000000000000
|
||||
r8 =00000000FFFFCB1C r9 =0000000000000000 r10=0000000100000000
|
||||
r11=00000003FCCCF7A3 r12=00000000FFFFCC90 r13=00000000FFFFCDF0
|
||||
r14=0000000000000000 r15=0000000000000000
|
||||
rbp=00000000FFFFCAF0 rsp=00000000FFFFCA90
|
||||
program=D:\OneDrive\Zach\University\2020\Semester 2\SENG1120\Assignment 1\a1.exe, pid 7913, thread main
|
||||
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
|
||||
Stack trace:
|
||||
Frame Function Args
|
||||
000FFFFCAF0 003FCCD3A85 (0018023AD70, 000FFFFD680, 001801AE889, 0018023A780)
|
||||
000FFFFCAF0 00100402855 (00000000000, 0018023A780, 0018023A780, 003FCD5DFC0)
|
||||
000FFFFCB90 00100402517 (000FFFFCC20, 00000000001, 001801F6BC0, 00800000440)
|
||||
000FFFFCC00 001004016AF (0018004AEC1, 00180049D60, 00000000000, 00180325BE0)
|
||||
000FFFFCCE0 0018004AF2D (00000000000, 00000000000, 00000000000, 00000000000)
|
||||
000FFFFFFF0 00180048886 (00000000000, 00000000000, 00000000000, 00000000000)
|
||||
000FFFFFFF0 00180048934 (00000000000, 00000000000, 00000000000, 00000000000)
|
||||
End of stack trace
|
||||
17
makefile
Normal file
17
makefile
Normal file
@ -0,0 +1,17 @@
|
||||
CC=g++
|
||||
CFLAGS=-c -Wall -std=c++98
|
||||
LDFLAGS=
|
||||
SOURCES= LinkedListDemo.cpp LinkedList.cpp Node.cpp
|
||||
OBJECTS=$(SOURCES:.cpp=.o)
|
||||
EXECUTABLE=a1
|
||||
|
||||
all: $(SOURCES) $(EXECUTABLE)
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
|
||||
|
||||
%.o : %.cpp
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
clean:
|
||||
rm -rf *.o core
|
||||
17
makefile - Copy
Normal file
17
makefile - Copy
Normal file
@ -0,0 +1,17 @@
|
||||
CC=g++
|
||||
CFLAGS=-c -Wall -std=c++98
|
||||
LDFLAGS=
|
||||
SOURCES= LinkedListDemo.cpp LinkedList.cpp Node.cpp
|
||||
OBJECTS=$(SOURCES:.cpp=.o)
|
||||
EXECUTABLE=a1
|
||||
|
||||
all: $(SOURCES) $(EXECUTABLE)
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
|
||||
|
||||
%.o : %.cpp
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
clean:
|
||||
rm -rf *.o core
|
||||
Loading…
x
Reference in New Issue
Block a user