Question

In: Computer Science

class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...

class DoubleLinkedList {

public:

//Implement ALL following methods.

//Constructor.

DoubleLinkedList();

//Copy constructor.

DoubleLinkedList(const DoubleLinkedList & rhs);

//Destructor. Clear all nodes.

~DoubleLinkedList();

// Insert function. Returns true if item is inserted,

// false if the item it a duplicate value

bool insert(int x);

// Removes the first occurrence of x from the list,

// If x is not found, the list remains unchanged.

void remove(int x);

//Assignment operator.

const DoubleLinkedList& operator=(const DoubleLinkedList & rhs);

private:

struct node{

int data;

node* next;

node* prev;

};

node* head;

node* tail;

};

For the double linked list class above, implement all of the methods.

Solutions

Expert Solution

Complete code in C++:-

I've added main function just for testing purpose, its not part of your 'DoubleLinkedList' class, you can remove it from code.

#include <bits/stdc++.h>

using namespace std;

class DoubleLinkedList {

private:
   typedef struct node{
       int data;
       struct node* next;
       struct node* prev;
   }node;
   node* head;
   node* tail;

public:
   //Implement ALL following methods.
   //Constructor.
   DoubleLinkedList();
   //Copy constructor.
   DoubleLinkedList(const DoubleLinkedList & rhs);
   //Destructor. Clear all nodes.
   ~DoubleLinkedList();
   // Insert function. Returns true if item is inserted,
   // false if the item it a duplicate value
   bool insert(int x);
   // Removes the first occurrence of x from the list,
   // If x is not found, the list remains unchanged.
   void remove(int x);
   void print();
   //Assignment operator.
   const DoubleLinkedList& operator=(const DoubleLinkedList & rhs) {
       return rhs;
   }
};

DoubleLinkedList::DoubleLinkedList() {
   // Default constructor, Initialize as NULL
   this->head = NULL;
   this->tail = NULL;
}

DoubleLinkedList::DoubleLinkedList(const DoubleLinkedList & rhs) {
   // Making new copy of rhs.
   this->head = NULL;
   this->tail = NULL;
   node *temp = rhs.head;
   // Copying node by node.
   while(temp != NULL) {
       insert(temp->data);
       temp = temp->next;
   }
}

DoubleLinkedList::~DoubleLinkedList() {
   // Destructor, deleting head and tail pointer.
   delete this->head;
   delete this->tail;
}

bool DoubleLinkedList::insert(int x) {
   // Creating new node with data.
   node *p1 = (node*)malloc(sizeof(node));
   p1->data = x;
   p1->next = NULL;
   p1->prev = NULL;
   // If currently list is empty.
   if(this->head == NULL) {
       this->head = p1;
       this->tail = this->head;
       return true;
   }
   // Otherwise add this new node in the end of list.
   node *temp = this->head;
   while(temp) {
       // If node is present return false.
       if(temp->data == x) {
           return false;
       }
       temp = temp->next;
   }
   // else add new node and return true.
   temp = p1;
   temp->prev = this->tail;
   this->tail->next = temp;
   this->tail = temp;
   return true;
}

// Remove specified node from the list.
void DoubleLinkedList::remove(int x) {
   node *temp = this->head;
   while(temp) {
       // If mnode is found
       if(temp->data == x) {
           // If node is internal node.          
           if(temp->prev && temp->next) {
               temp->prev->next = temp->next;
           }
           // If node is one the terminal node, head or tail.
           else if(temp->prev){
               this->tail = temp->prev;
               this->tail->next = NULL;
           }
           else if(temp->next) {
               this->head = this->head->next;
               this->head->prev = NULL;
           }
           // If node is the single presnt in the list.
           else {
               this->head = NULL;
               this->tail = NULL;
           }
           // Delete node.
           delete temp;
           break;
       }
       temp = temp->next;
   }
}

void DoubleLinkedList::print() {
   node *temp = this->head;
   while(temp) {
       cout << temp->data << " ";
       temp = temp->next;
   }
   cout << '\n';
}

int main(void) {
   DoubleLinkedList d1;
   d1.insert(5);
   d1.insert(2);
   d1.insert(3);
   d1.insert(4);
   d1.insert(5);
   cout << "Original list: ";
   d1.print();
   DoubleLinkedList d2 = d1;
   cout << "Copy of original list using Assignment: ";
   d2.print();
   d2.remove(2);
   cout <<"Removed 2 form the copy list: ";
   d2.print();
   d2.insert(49);
   cout <<"Inserted 49 in the copy list: ";
   d2.print();
   cout << "Original list: ";
   d1.print();
   DoubleLinkedList d3(d1);
   cout << "Copy of original list using Copy constructor: ";
   d3.print();
   return 0;
}

Screenshot of output:-


Related Solutions

Define the following class: class XYPoint { public: // Contructors including copy and default constructor //...
Define the following class: class XYPoint { public: // Contructors including copy and default constructor // Destructors ? If none, explain why double getX(); double getY(); // Overload Compare operators <, >, ==, >=, <=, points are compare using their distance to the origin: i.e. SQR(X^2+Y^2) private: double x, y; };
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const container& c); container& operator=(const container& c) // MEMBER FUNCTIONS bool lookup(const int& target); void remove(const int& target); void add(const int& target); private: struct node{ int key; node *next; }; node* head; node* tail; }; #endif For a linked list based implementation of a container class shown above, implement the constructor, copy constructor, destructor and copy assignment functions.
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of the class. I have added the Javadoc comments but please feel free to format them if they are done incorrectly. public class Nickel implements Comparable { private int year; /** * The monetary value of a nickel in cents. */ public final int CENTS = 5; /** * Initializes this nickel to have the specified issue year. * * @param year * * @pre....
In the following class public class Single { private float unique; ... } Create constructor, setter...
In the following class public class Single { private float unique; ... } Create constructor, setter and getter methods, and toString method. Write a program that request a time interval in seconds and display it in hours, minutes, second format. NEED THESE ASAP
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter...
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter and getter methods, and toString method.
In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score...
Implement a class Student, including the following attributes and methods: Two public attributes name(String) and score (int). A constructor expects a name as a parameter. A method getLevel to get the level(char) of the student. score level table: A: score >= 90 B: score >= 80 and < 90 C: score >= 60 and < 80 D: score < 60 Example:          Student student = new Student("Zack"); student.score = 10; student.getLevel(); // should be 'D'. student.score = 60; student.getLevel(); //...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT