Question: Circular Doubly Linked List I am trying to figure out how to modify my doubly linked list so that it can become a circular doubly

Circular Doubly Linked List

I am trying to figure out how to modify my doubly linked list so that it can become a circular doubly linked list.

And what changes would have to be made to handle the new circular nature of the linked list in all the methods?

Here is what I have.

LINKEDLIST.H

#ifndef LINKED_LIST_H #define LINKED_LIST_H #include

struct Node { double value; Node *next; Node *prev; }; class LinkedList { private: Node *head; Node *tail; int count; public: LinkedList(); LinkedList(LinkedList &); ~LinkedList(); void addNode(double); void deleteNode(double); void insertNode(double, double); bool contains(double); Node * getHead(); int size(); friend std::ostream & operator << (std::ostream&, LinkedList&);

}; #endif

LINKEDLIST.CPP

#include "LinkedList.h" #include #include using namespace std;

LinkedList::LinkedList() { head = NULL; tail = NULL; count = 0; }

void LinkedList::addNode(double d) { Node *temp = new Node; temp->value = d; if (head == NULL) { temp->next = NULL; temp->prev = NULL; head = temp; tail = head; } else { temp->next = NULL; temp->prev = tail; tail->next = temp; tail = temp; } count++; }

void LinkedList::deleteNode(double d) { Node * temp; temp = head; while (temp != NULL && temp->value != d) { temp = temp->next; } if (temp == head) { if (head == tail) { tail = NULL; head = NULL; } else { head = temp->next; temp->next->prev = NULL; } delete temp; } else if (temp == tail) { temp->prev->next = NULL; delete temp; } else { temp->prev->next = temp->next; temp->next->prev = temp->prev; delete temp; } count--; }

void LinkedList::insertNode(double before, double val) { Node * temp = new Node; temp->value = val; Node * loc; loc = head; while (loc != NULL && loc->value != val) { loc = loc->next; } temp->prev = loc; temp->next = loc->next; temp->prev->next = temp; if (temp->next != NULL) { temp->next->prev = temp; } count++; }

bool LinkedList::contains(double val) { Node * temp; temp = head; while (temp != NULL) { if (temp->value == val) { return true; } temp = temp->next; } return false; }

Node * LinkedList::getHead() { return head; }

int LinkedList::size() { return count; }

LinkedList::~LinkedList() { Node * temp; while (head != NULL) { temp = head; head = head->next; delete temp; count --; }

}

ostream& operator << (ostream & ost, LinkedList & ll) { Node * temp = ll.head; while (temp != NULL) { ost << "Value = " << temp->value << endl; temp = temp->next; } return ost; }

LinkedList::LinkedList(LinkedList & ll) { head = new Node; head->value = ll.head->value; Node *temp = head; Node *temp2 = ll.head->next; head->next = new Node; while(temp2 != NULL) { temp = temp->next; temp->value = temp2->value; temp->next = new Node; temp2 = temp2->next; } tail = temp; head->prev = NULL; temp = head->next; temp2 = head; while (temp != NULL) { temp->prev = temp2; temp = temp->next; temp2 = temp2->next; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!