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.
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
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...
White the class Trace with a copy constructor and an assignment operator, printing a short message...
White the class Trace with a copy constructor and an assignment operator, printing a short message in each. Use this class to demonstrate the difference between initialization Trace t("abc"); Trace u = t; and assignment. Trace t("abc"); Trace u("xyz"); u = t; the fact that all constructed objects are automatically destroyed. the fact that the copy constructor is invoked if an object is passed by value to a function. the fact that the copy constructor is not invoked when a...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT