doublyLinkedLists
This commit is contained in:
parent
d1727afe46
commit
f2a1667d9d
|
|
@ -10,4 +10,4 @@ M_d_SE2
|
|||
*.drawio
|
||||
.editorconfig
|
||||
*.code-workspace
|
||||
Code/**
|
||||
Code*/**
|
||||
|
|
@ -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; }
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
|
||||
class ExtendedListItem {
|
||||
public:
|
||||
ExtendedListItem(int key);
|
||||
std::string toString();
|
||||
ExtendedListItem *previous;
|
||||
ExtendedListItem *next;
|
||||
int key;
|
||||
};
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,126 @@
|
|||
#include "doublyLinkedList.h"
|
||||
#include <iostream>
|
||||
|
||||
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 << "<Empty List>" << 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;
|
||||
}
|
||||
|
|
@ -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();
|
||||
};
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include "doublyLinkedList.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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();
|
||||
}
|
||||
Loading…
Reference in New Issue