You need to edit the same code so as to make it a circular doubly linked list. Implement the necessary functionality in all the given functions. Note: A circular doubly linked list is a doubly linked list whose last next pointer points to the head node #include using namespace std; class Node{ public: int data; Node *next; Node *prev; }; class List{ public: Node *head; List(){ head = NULL; } void display(){ Node *temp = head; while(temp!=NULL) { cout<data<next; } } void insert(int data){ Node *nn = new Node; nn->next = NULL; nn->prev = NULL; nn->data = data; if(head==NULL){ head = nn; cout<<"Node inserted successfully at head"<next!=NULL) temp = temp->next; temp->next=nn; nn->prev = temp; cout<<"Node inserted successfully at end"<data==data){ cout<<"Data found"<next; } return temp; } void insertInBetween(Node *node, int data){ Node *nn = new Node; nn->data = data; Node *temp = node->next; node->next = nn; nn->next = temp; nn->prev = node; temp->prev = nn; cout<data<<" inserted successfully after: "<data<data = data; nn->next = head; nn->prev = NULL; head->prev = nn; head = nn; } void deleteNode(int data){ Node *temp = search(data); if(temp==NULL) cout<<"Node not found"<next; head->prev = NULL; } else { Node *before = temp->prev; // while(before->next != temp) // before = before->next; before->next = temp->next; temp->next->prev = before; } delete temp; } } }; int main() { List list; list.insert(10); list.insert(20); list.insert(30); list.display(); Node *found = list.search(20); if(found==NULL) cout<<"Node not found"<data<
You need to edit the same code so as to make it a circular doubly linked list. Implement the necessary functionality in all the given functions.
Note: A circular doubly linked list is a doubly linked list whose last next pointer points to the head node
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node *prev;
};
class List{
public:
Node *head;
List(){
head = NULL;
}
void display(){
Node *temp = head;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
void insert(int data){
Node *nn = new Node;
nn->next = NULL;
nn->prev = NULL;
nn->data = data;
if(head==NULL){
head = nn;
cout<<"Node inserted successfully at head"<<endl;
}
else{
Node *temp = head;
while(temp->next!=NULL)
temp = temp->next;
temp->next=nn;
nn->prev = temp;
cout<<"Node inserted successfully at end"<<endl;
}
}
Node* search(int data){
Node *temp = head;
while(temp!=NULL)
{
if(temp->data==data){
cout<<"Data found"<<endl;
break;
}
temp = temp->next;
}
return temp;
}
void insertInBetween(Node *node, int data){
Node *nn = new Node;
nn->data = data;
Node *temp = node->next;
node->next = nn;
nn->next = temp;
nn->prev = node;
temp->prev = nn;
cout<<nn->data<<" inserted successfully after: "<<node->data<<endl;
}
void insertAtHead(int data){
Node *nn = new Node;
nn->data = data;
nn->next = head;
nn->prev = NULL;
head->prev = nn;
head = nn;
}
void deleteNode(int data){
Node *temp = search(data);
if(temp==NULL)
cout<<"Node not found"<<endl;
else{
if(head==temp){
head = head->next;
head->prev = NULL;
}
else
{
Node *before = temp->prev;
// while(before->next != temp)
// before = before->next;
before->next = temp->next;
temp->next->prev = before;
}
delete temp;
}
}
};
int main() {
List list;
list.insert(10);
list.insert(20);
list.insert(30);
list.display();
Node *found = list.search(20);
if(found==NULL)
cout<<"Node not found"<<endl;
else
cout<<"Found node: "<<found->data<<endl;
list.insertInBetween(found, 25);
list.display();
list.insertAtHead(5);
list.display();
list.deleteNode(10);
list.display();
return 0;
}
Step by step
Solved in 3 steps with 2 images