From f2a1667d9de94a6aa441519116db4117519ae91c Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Tue, 31 May 2022 22:09:14 +0200 Subject: [PATCH] doublyLinkedLists --- .gitignore | 2 +- Uebung 2/ExtendedListItem.cpp | 7 ++ Uebung 2/ExtendedListItem.h | 11 +++ Uebung 2/Makefile | 51 ++++++++++++++ Uebung 2/doublyLinkedList.cpp | 126 ++++++++++++++++++++++++++++++++++ Uebung 2/doublyLinkedList.h | 17 +++++ Uebung 2/main.cpp | 32 +++++++++ 7 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 Uebung 2/ExtendedListItem.cpp create mode 100644 Uebung 2/ExtendedListItem.h create mode 100644 Uebung 2/Makefile create mode 100644 Uebung 2/doublyLinkedList.cpp create mode 100644 Uebung 2/doublyLinkedList.h create mode 100644 Uebung 2/main.cpp diff --git a/.gitignore b/.gitignore index 1177781..b3fba6b 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,4 @@ M_d_SE2 *.drawio .editorconfig *.code-workspace -Code/** \ No newline at end of file +Code*/** \ No newline at end of file diff --git a/Uebung 2/ExtendedListItem.cpp b/Uebung 2/ExtendedListItem.cpp new file mode 100644 index 0000000..fec47ae --- /dev/null +++ b/Uebung 2/ExtendedListItem.cpp @@ -0,0 +1,7 @@ +#include "ExtendedListItem.h" + +std::string ExtendedListItem::toString(void) { + return std::to_string(this->key); +} + +ExtendedListItem::ExtendedListItem(int key) { this->key = key; } \ No newline at end of file diff --git a/Uebung 2/ExtendedListItem.h b/Uebung 2/ExtendedListItem.h new file mode 100644 index 0000000..e1ee46a --- /dev/null +++ b/Uebung 2/ExtendedListItem.h @@ -0,0 +1,11 @@ +#pragma once +#include + +class ExtendedListItem { +public: + ExtendedListItem(int key); + std::string toString(); + ExtendedListItem *previous; + ExtendedListItem *next; + int key; +}; diff --git a/Uebung 2/Makefile b/Uebung 2/Makefile new file mode 100644 index 0000000..23dd783 --- /dev/null +++ b/Uebung 2/Makefile @@ -0,0 +1,51 @@ + +# Name of the binary for Development +BINARY = main +# Name of the binary for Release +FINAL = prototyp +# Object files +OBJS = ExtendedListItem.o doublyLinkedList.o main.o +# Compiler flags +CFLAGS = -Werror -Wall -std=c++17 -fsanitize=address,undefined -g +# Linker flags +LFLAGS = -fsanitize=address,undefined +#Which Compiler to use +COMPILER = c++ + + +# all target: builds all important targets +all: binary + +final : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${FINAL} ${OBJS} + +binary : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS} + +# Links the binary +${BINARY} : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS} + + +# Compiles a source-file (any file with file extension .c) into an object-file +# +# "%" is a wildcard which matches every file-name (similar to * in regular expressions) +# Such a rule is called a pattern rule (because it matches a pattern, see https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html), +# which are a form of so called implicit rules (see https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html) +# "$@" and "$<" are so called automatic variables (see https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html) +%.o : %.cpp + ${COMPILER} -c ${CFLAGS} -o $@ $< + + +# Rules can not only be used for compiling a program but also for executing a program +run: ${BINARY} + ./${BINARY} + + +# Delete all build artifacts +clean : + rm -rf ${BINARY} ${OBJS} + + +# all and clean are a "phony" targets, meaning they are no files +.PHONY : all clean diff --git a/Uebung 2/doublyLinkedList.cpp b/Uebung 2/doublyLinkedList.cpp new file mode 100644 index 0000000..9a23ff0 --- /dev/null +++ b/Uebung 2/doublyLinkedList.cpp @@ -0,0 +1,126 @@ +#include "doublyLinkedList.h" +#include + +DoublyLinkedList::DoublyLinkedList() { + this->head = NULL; + this->tail = NULL; +} + +void DoublyLinkedList::insertAtStart(int key) { + ExtendedListItem *temp = new ExtendedListItem(key); + + if (this->head == NULL) { + this->head = temp; + this->tail = temp; + } else { + temp->next = this->head; + head->previous = temp; + this->head = temp; + } +} + +void DoublyLinkedList::insertAtEnd(int key) { + ExtendedListItem *temp = new ExtendedListItem(key); + + if (this->head == NULL) { + this->head = temp; + this->tail = temp; + } else { + this->tail->next = temp; + temp->previous = this->tail; + this->tail = temp; + this->tail->next = NULL; + } +} + +void DoublyLinkedList::print() { + ExtendedListItem *temp = this->head; + + if (temp == NULL) { + std::cout << "" << std::endl; + return; + } + + while (temp != NULL) { + std::cout << temp->toString(); + if (temp->next != NULL) { + std::cout << " -> "; + } + temp = temp->next; + } + std::cout << std::endl; +} + +void DoublyLinkedList::append(DoublyLinkedList *appendingList) { + if (appendingList->head != NULL && appendingList->tail != NULL) { + this->tail->next = appendingList->head; + appendingList->head->previous = this->tail; + this->tail = appendingList->tail; + appendingList->head = NULL; + appendingList->tail = NULL; + } +} + +void DoublyLinkedList::splice(DoublyLinkedList *insertingList, int position) { + if (insertingList->head != NULL & insertingList->tail != NULL) { + ExtendedListItem *prev = NULL; + ExtendedListItem *current = NULL; + current = this->head; + // find the correct position to start spliceing + for (int i = 0; i < position; i++) { + prev = current; + current = current->next; + } + // set the pointers correctly + prev->next = insertingList->head; + insertingList->head->previous = prev; + insertingList->tail->next = current; + current->previous = insertingList->tail; + + // clear the inserting list + insertingList->head = NULL; + insertingList->tail = NULL; + } +} + +void DoublyLinkedList::deleteItem(int key) { + ExtendedListItem *prev = NULL; + ExtendedListItem *current = NULL; + // delete head? + if (key == this->head->key) { + ExtendedListItem *temp = this->head; + this->head = this->head->next; + this->head->previous = NULL; + delete temp; + return; + } + // set prev and current for the first time + // (thx to Mr. Klingenschmid for bug spotting!) + prev = this->head; + current = prev->next; + + // searching the right key to delete + bool deleted = false; + while (deleted == false) { + if (current->key == key) { + deleted = true; + } else { + prev = current; + current = current->next; + } + if (current == NULL) { + return; + } + } + // was element to be deleted the tail? + if (current != this->tail) { + prev->next = current->next; + current->next->previous = prev; + } else { + this->tail = prev; + prev->next = NULL; + } + + delete current; + return; +} \ No newline at end of file diff --git a/Uebung 2/doublyLinkedList.h b/Uebung 2/doublyLinkedList.h new file mode 100644 index 0000000..3b42bc8 --- /dev/null +++ b/Uebung 2/doublyLinkedList.h @@ -0,0 +1,17 @@ +#pragma once +#include "ExtendedListItem.h" + +class DoublyLinkedList { +private: +public: + DoublyLinkedList(); + + ExtendedListItem *head; + ExtendedListItem *tail; + void append(DoublyLinkedList *appendingList); + void splice(DoublyLinkedList *insertingList, int position); + void insertAtStart(int key); + void insertAtEnd(int key); + void deleteItem(int key); + void print(); +}; \ No newline at end of file diff --git a/Uebung 2/main.cpp b/Uebung 2/main.cpp new file mode 100644 index 0000000..76ac70a --- /dev/null +++ b/Uebung 2/main.cpp @@ -0,0 +1,32 @@ +#include "doublyLinkedList.h" +#include +#include + +int main() { + DoublyLinkedList list; + list.insertAtEnd(5); + list.insertAtEnd(7); + list.insertAtEnd(19); + list.insertAtEnd(54); + list.insertAtEnd(1); + list.insertAtEnd(9); + list.insertAtEnd(17); + list.insertAtEnd(17); + list.insertAtEnd(108); + list.insertAtEnd(8); + + list.print(); + + DoublyLinkedList list2; + list2.insertAtEnd(2); + list2.insertAtEnd(3); + list2.insertAtEnd(4); + + list2.print(); + + list.splice(&list2, 4); + + list.print(); + + list2.print(); +} \ No newline at end of file