Question

In: Computer Science

(I need an answer in c++ please / assignment question) In a double linked chain, each...

(I need an answer in c++ please / assignment question)

In a double linked chain, each node can point to the previous node as well as the next node.:

a. Define a class to represent a node in a doubly linked chain (UML)

b. Define a class to represent the doubly linked chain (UML), with operations such as:

1. finding its length.

2. add a new Node to the end of the chain.

3. check whether a given Node is included in the Chain (search a node)

4. another operation of your choice.

c. illustrate and list the steps to add to a node to the beginning of the doubly linked chain.

d. illustrate and list the steps necessary to remove the first noe from the doubly linked chain.

Solutions

Expert Solution

Screenshot

Program

dll.h

//Header file for I/O
#include<iostream>
using namespace std;
//Create a class Node
class Node {
public:
   int data;
   Node *prev;
   Node *next;
};
//Create a class for doubly linked list
class DoublyLL {
//Instance variable
private:
   Node *head;
public:
   //Constructor
   DoublyLL();
   //Insert at the end
   void insert(int newdata);
   //Print list in forward order
   void print();
   //Length of the list
   int findLength();
   //Search an element
   void searchNode(int data);
   //Print list in reverse order
   void printReverse();
   //Add element at the beginning
   void insertBeg(int val);
   //Delete first node
   void deleteFirst();
};
//Constructor
DoublyLL::DoublyLL() {
   head = nullptr;
}
//Insert at the end
void DoublyLL::insert(int newdata) {
   //Create ne node
   Node *newnode = new Node;
   newnode->data = newdata;
   newnode->prev = nullptr;
   newnode->next = nullptr;
   //list is empty
   if (head == nullptr)
       head = newnode;
   //Otherwise
   else {
       Node* ptr = head;
       while (ptr->next != nullptr) {
           ptr = ptr->next;
       }
       ptr->next = newnode;
       newnode->prev = ptr;
   }
}
//Print list in forward order
void DoublyLL::print() {
   Node* ptr = head;
   while (ptr != nullptr) {
       cout << ptr->data << " ";
       ptr = ptr->next;
   }
   cout << endl;
}
//Length of the list
int DoublyLL::findLength() {
   int cnt = 0;
   Node* ptr = head;
   while (ptr != nullptr) {
       cnt++;
       ptr = ptr->next;
   }
   return cnt;
}
//Search an element
void DoublyLL::searchNode(int data) {
   Node* ptr = head;
   int cnt = 0;
   while (ptr != nullptr) {
       cnt++;
       if (ptr->data == data) {
           cout << "Data found at the position " << cnt << endl;
           return;
       }
       ptr = ptr->next;
   }
   cout << "Data not found in the lsit" << endl;
}
//Print list in reverse order
void DoublyLL::printReverse() {
   Node* ptr = head;
   while (ptr->next != nullptr) {
       ptr = ptr->next;
   }
   while (ptr != NULL) {
       cout << ptr->data << " ";
       ptr = ptr->prev;
   }
   cout << endl;
}
//Add element at the beginning
void DoublyLL::insertBeg(int val) {
   Node* temp = head;
   //Create new node
   Node *newnode = new Node;
   newnode->data = val;
   newnode->prev = nullptr;
   newnode->next = temp;
   //Add into head
   head = newnode;
}
//Delete first node
void DoublyLL::deleteFirst() {
   //Head empty check
   if (head == nullptr) {
       cout << "List is empty!!" << endl;
   }
   //Head next empty check
   else if (head->next == nullptr) {
       head = nullptr;
   }
   //Otherwise
   else {
       head = head->next;
   }
}

main.cpp

#include "dll.h"
int main()
{
   //Create a doubly linked list
   DoublyLL dll;
   //Insert values into list
   dll.insert(10);
   dll.insert(2);
   dll.insert(5);
   //Display list
   cout << "Print list: ";
   dll.print();
   //Length of the list
   cout <<"Length of the list: "<<dll.findLength() << endl;
   //Reverse the list
   cout << "Reverse list: ";
   dll.printReverse();
   //Insert at the beginning
   dll.insertBeg(3);
   //Test
   cout << "Print after insert first: ";
   dll.print();
   //Delete
   cout << "Print after Deletion: ";
   dll.deleteFirst();
   dll.print();
}

------------------------------------------------------

Output

Print list: 10 2 5
Length of the list: 3
Reverse list: 5 2 10
Print after insert first: 3 10 2 5
Print after Deletion: 10 2 5


Related Solutions

c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
Please I need answer for This question I have assignment for this subjects . Please be...
Please I need answer for This question I have assignment for this subjects . Please be brief and to the point . Write than 3 pages (maximum),but It is enough to give the name of the authors Only legible answers will be considered.Thanks in advance/Ha !-What are Structural discrimination in the housing market in Sweden?.Give an overview of the theories of discrimination used in economics. What are the main differences between these theories?
Please I need answer for This question I have assignment for this subjects . Please be...
Please I need answer for This question I have assignment for this subjects . Please be brief and to the point . Write than 3 pages (maximum),but It is enough to give the name of the authors Only legible answers will be considered.Thanks in advance/Ha 1-Discrimination in the Swedish housing market ?Give an overview of the theories of discrimination used in economics. What are the main differences between these theories?
Please I need answer for This question I have assignment for this subjects . Please be...
Please I need answer for This question I have assignment for this subjects . Please be brief and to the point . ,but It is enough to give the name of the authors Only legible answers will be considered.Thanks in advance/Ha 1- Give an overview of the Becker theory of Discrimination. a-Discrimination definition. b-Explicit Discrimination . c-Hypothesis formulation.
Please I need today answer for This question and it is very important and I need...
Please I need today answer for This question and it is very important and I need solution for this issue with all the details , and help me with all the details.Please write your answer to me by typing, not by handwriting, so that I can read and understand your answer clearly.BR/Hassan Question : Unemployment and economic policy . Please be brief and to the point. . Your task is to explain how employment and/or unemployment is determined according to...
Please I need today answer for This question and it is very important and I need...
Please I need today answer for This question and it is very important and I need solution for this issue with all the details , and help me with all the details.Please write your answer to me by typing, not by handwriting, so that I can read and understand your answer clearly.BR/Hassan Externalities : a) Define a negative production externality. b) Why do markets not always manage to solve the problem of externalities on their own? c) Under which conditions...
Please I need today answer for This question and it is very important and I need...
Please I need today answer for This question and it is very important and I need solution for this issue with all the details , and help me with all the details.Please write your answer to me by typing, not by handwriting, so that I can read and understand your answer clearly.BR/Hassan Q) Two firms produce a good which leads to pollution : Assume that the marginal cost for cleaning up in one firm is given by ??1 = 40...
Please I need today answer for This question and it is very important and I need...
Please I need today answer for This question and it is very important and I need solution for this issue with all the details , and help me with all the details.Please write your answer to me by typning  not by hand writing, so that I can read and understand your answer clearly.thanks in advance/Ha Question This question will ask you to use the Heckscher-Ohlin model to analyze the effect of trade liberalization between two countries. Assume that there are two...
Please I need answer for This question and it is very important and I need solution...
Please I need answer for This question and it is very important and I need solution for this issue with all the details just nu , and help me with all the details, so that I can read and understand your answer clearly.thanks in advance/Ha 2. Answer whether the following examples of Foreign Direct Investment (FDI) describe horizontal or vertical FDI. Explain your answers. a) Volvo builds a car factory in Austin, Texas, which is similar to Volvo’s factory in...
Please I need today answer for This question and it is very important and I need...
Please I need today answer for This question and it is very important and I need solution for this issue with all the details , and help me with all the details.Please write your answer to me by typning  not by hand writing, so that I can read and understand your answer clearly.thanks in advance/Ha 1. Explain the following concepts in 50 words maximum for each concept. . a) Vertical foreign direct investment b) Internal economies of scale c) Comparative advantage...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT