126 lines
3.0 KiB
C++
126 lines
3.0 KiB
C++
#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;
|
|
} |