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; };
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...
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; }...
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
Research "Const Correctness" and answer the following questions: Given: class SimpleClass { private: int _x; public:...
Research "Const Correctness" and answer the following questions: Given: class SimpleClass { private: int _x; public: SimpleClass(int x) : _x(x){} int getX() const { return _x; } void setX(int newX) { _x = newX; } void displayDataWithCustomMessage(const string &customMessage) { cout<<"Data: "<<_x<<endl; cout<<"Custom Message: "<<customMessage<<endl; } }; What is the usefulness of the "const" keyword in the definition of the "getX" member function? What is the usefulness of the "const" keyword in the definition of the "displayDataWithCustomMessage" member function? Why...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically...
package hw; public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums = new double[nums1.length]; for(int i=0;i<nums1.length;i++) nums[i] = nums1[i]; numElements = nums1.length; } void printArray(){ // cost, times System.out.printf("printArray(%d,%d): ",numElements,nums.length); for(int i=0; i<numElements;i++) System.out.print(nums[i]+" "); System.out.println(); }...
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT