In: Computer Science
(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.
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