Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "LinkedList.h"
- #include <iostream>
- using namespace std;
- LinkedList::LinkedList() //constructor
- {
- head = NULL;
- tail = NULL;
- }
- LinkedList::~LinkedList() //deconstructor
- {
- node_t* temp = tail;
- if(temp != NULL){
- while(temp->next != NULL){
- temp = temp->next;
- delete[] temp->prev;
- }
- }
- delete[] temp;
- head = NULL;
- tail = NULL;
- }
- LinkedList::LinkedList(const LinkedList& src) //constructor som tar en länkad lista
- {
- head = NULL;
- tail = NULL;
- node_t* temp = src.tail;
- while(temp != NULL){
- push_front(temp->value);
- temp = temp->next;
- }
- }
- LinkedList& LinkedList::operator=(const LinkedList& rhs) //operatoröverlagring som kopierar länkad lista
- {
- delete this;
- node_t* temp = rhs.tail;
- if(temp != NULL){
- while(temp != NULL){
- push_front(temp->value);
- temp = temp->next;
- }
- }
- }
- LinkedList& LinkedList::operator+=(const LinkedList& rhs) //operatoröverlagring som adderar en länkad lista
- {
- node_t* temp = rhs.tail;
- if(temp != NULL){
- while(temp != NULL){
- push_back(temp->value);
- temp = temp->next;
- }
- }
- }
- void LinkedList::insert(double value, size_t pos) //linkedlist funktion som stoppar in value i position
- {
- if(is_empty() || pos >= size()){
- push_front(value);
- }
- else if(pos == 0 && !is_empty()){
- push_back(value);
- }
- else if(pos < size()){
- node_t* insertNode = new node_t;
- insertNode->value = value;
- node_t* temp = tail;
- for(int i = 0; i < pos; i++){
- temp = temp->next;
- }
- insertNode->prev = temp->prev;
- insertNode->next = temp;
- temp->prev->next = insertNode;
- }
- }
- void LinkedList::push_front(double value) //linkedlist funktion som pushar value i början av listan
- {
- node_t* insertNode = new node_t;
- insertNode->value = value;
- if(head != NULL){
- insertNode->prev = head;
- head->next = insertNode;
- head = insertNode;
- }
- else{
- head = insertNode;
- tail = insertNode;
- }
- }
- void LinkedList::push_back(double value) //linkedlist funktion som pushbackar ett value
- {
- node_t* insertNode = new node_t;
- insertNode->value = value;
- if(tail != NULL){
- insertNode->next = tail;
- tail->prev = insertNode;
- tail = insertNode;
- }
- else{
- head = insertNode;
- tail = insertNode;
- }
- }
- //Access
- double LinkedList::front() const //linkedlistfunktion som kollar vad som ligger längst fram i head
- {
- if(head != NULL){
- return head->value;
- }
- }
- double LinkedList::back() const //linkedlistfunktion som kollar vad som ligger längst bak i tail
- {
- if(tail != NULL){
- return tail->value;
- }
- }
- double LinkedList::at(size_t pos) const //linkedlistfunktion som kollar värdet på en position i listan
- {
- if(!is_empty() && pos < size()){
- node_t* temp = tail;
- for(int i = 0; i < pos; i++){
- temp = temp->next;
- }
- return temp->value;
- }
- }
- //remove
- void LinkedList::remove(size_t pos) //linkedlistfunktion som tar bort en angiven position i listan
- {
- if(pos == size()-1){
- pop_front();
- }
- else if(!is_empty() && pos < size()){
- node_t* temp = tail;
- for(int i = 0; i < pos; i++){
- temp = temp->next;
- }
- temp->prev->next = temp->next;
- temp->next->prev = temp->prev;
- delete temp;
- }
- }
- //Remove and access
- double LinkedList::pop_back() //linkedlist funktion som poppar värdet som ligger i tail
- {
- //If tail is null the list is empty
- if(tail != NULL){
- node_t* temp = tail; //Temp copy of tail node to keep for deletion
- double returnValue = tail->value; //Value of the node to return when popped
- if(tail->next == NULL){ //If tail next is NULL this is the only element in the list
- tail = NULL; //Set the head and tail pointers to NULL
- head = NULL;
- return returnValue; //Return the value
- }
- tail = tail->next; //If there is more elements after the tail node make the next one the new tail
- tail->prev = NULL; //Make sure to set this new tail nodes next to NULL
- delete[] temp; //Delete the old tail node
- return returnValue; //Return the value
- }
- }
- double LinkedList::pop_front() //linkedlist funktion som poppar värdet som ligger i head
- {
- if(head != NULL){
- node_t* temp = head;
- double returnValue = head->value;
- if(head->prev == NULL){
- delete[] head;
- tail = NULL;
- head = NULL;
- return returnValue;
- }
- head = head->prev;
- head->next = NULL;
- delete[] temp;
- return returnValue;
- }
- }
- //Informational
- size_t LinkedList::size() const
- {
- if(!is_empty()){
- size_t counter = 1;
- node_t* temp = tail;
- while(temp->next != NULL){
- temp = temp->next;
- counter++;
- }
- return counter;
- }
- return 0;
- }
- bool LinkedList::is_empty() const
- {
- if(head == NULL){
- return true;
- }
- else{
- return false;
- }
- }
- //Output
- void LinkedList::print() const
- {
- if(!is_empty()){
- node_t* temp = tail;
- while(temp->next != NULL){
- cout << temp->value << ", ";
- temp = temp->next;
- }
- cout << temp->value << endl;
- }
- }
- void LinkedList::printReverse() const
- {
- if(!is_empty()){
- node_t* temp = head;
- while(temp->prev != NULL){
- cout << temp->value << ", ";
- temp = temp->prev;
- }
- cout << temp->value << endl;
- }
- }
- bool LinkedList::sort_check(){
- node_t* temp = tail;
- while(temp->next != NULL){
- if(temp->value > temp->next->value){
- return false;
- }
- temp = temp->next;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement