commit 5ad212de530326e6fadc53115b5969990760eebb Author: Zach S-B Date: Fri Sep 18 11:56:45 2020 +1000 FIrst Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29c9040 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Assignment1-s2-2020.pdf +Assignment1-s2-2020_Marking.pdf diff --git a/LinkedList.cpp b/LinkedList.cpp new file mode 100644 index 0000000..f098ddd --- /dev/null +++ b/LinkedList.cpp @@ -0,0 +1,313 @@ +/* +Name: LinkedList.cpp +Author: Zach Seibel-Barnes +Student Number: c3262201 +Last Modified: 16/09/2020 + +Description: +*/ + +#include +#include "LinkedList.h" +#include + +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; jgetNext(); + } + + 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<getData()==countString) + count++; + temp = temp->getNext(); + } + return count; +} + + +void LinkedList::remove(const valueType removeString) +{ + Node* temp=head; + Node* tempToDel; + + for (int i = 0; igetData()==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 (tempgetData()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)<<": "<getData()<<": "<getNext(); + + swapped=0; + + }while(temp->getNext()!=NULL); + + std::cout< +#include +#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 \ No newline at end of file diff --git a/LinkedListDemo.cpp b/LinkedListDemo.cpp new file mode 100644 index 0000000..0428452 --- /dev/null +++ b/LinkedListDemo.cpp @@ -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 +#include +#include +#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; +} diff --git a/Node.cpp b/Node.cpp new file mode 100644 index 0000000..ec1a5ff --- /dev/null +++ b/Node.cpp @@ -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; +} \ No newline at end of file diff --git a/Node.exe b/Node.exe new file mode 100644 index 0000000..c042f89 Binary files /dev/null and b/Node.exe differ diff --git a/Node.h b/Node.h new file mode 100644 index 0000000..cbece4d --- /dev/null +++ b/Node.h @@ -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 + + + +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 \ No newline at end of file diff --git a/a1.exe b/a1.exe new file mode 100644 index 0000000..573fc22 Binary files /dev/null and b/a1.exe differ diff --git a/a1.exe.stackdump b/a1.exe.stackdump new file mode 100644 index 0000000..f51e513 --- /dev/null +++ b/a1.exe.stackdump @@ -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 diff --git a/makefile b/makefile new file mode 100644 index 0000000..106142d --- /dev/null +++ b/makefile @@ -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 diff --git a/makefile - Copy b/makefile - Copy new file mode 100644 index 0000000..106142d --- /dev/null +++ b/makefile - Copy @@ -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