using c++ implement the following methods
Question
In doubly circular linked list using c++ implement the following methods
the doubly circular linked list has Two private data members:
1. myFirst: a pointer to the first node if the list is not empty, otherwise to null.
2. mySize: an integer representing the number of nodes in the list.
Methods to be implemented are the following
deleteBeforeData to delete the node before the one that contains a certain data from the list.
deleteAfterData to delete the node after the one that contains a certain data from the list.
cutAtpos to cut and return the node at a certain valid position.
Summary
Here in this question, we have to write a c++ program for the doubly circular linked list. Which has Two private data members.
Explanation
A doubly linked list is a linked list that points to both nodes, two nodes. One node has it before and the node next to it. Each node in the doubly linked list is linked to two nodes.
Pointer to → Data → Pointer to
Previous node Next node
Code
#include<iostream> using namespace std; //node structure for doubly linked list struct Node { int data; struct Node *before, *last; }; //class implementation for doubly linkedlist class DoublyLinkedList{ Node *myFirst; int mySize; public: DoublyLinkedList() { myFirst=NULL; mySize=0; } //method to insert a new value void insertData(int val) { struct Node *newNode=new Node(); newNode->data=val; struct Node *temp; if(myFirst==NULL) { newNode->before=newNode->last=NULL; myFirst=newNode; } else { temp=myFirst; while(temp->last!=NULL) temp=temp->last; newNode->before=temp; newNode->last=NULL; temp->last=newNode; } mySize++; } //method to print the list void print() { struct Node *temp=myFirst; while(temp!=NULL) { cout<<"--"<<temp->data; temp=temp->last; } } //method to delete the element before void deleteBeforeData(int val) { struct Node *temp,*prev; temp=myFirst; prev=NULL; while(temp->last->data!=val) { prev=temp; temp=temp->last; } prev->last=temp->last; temp->last->before=prev; delete temp; } //method to delete the element after the data entered void deleteAfterData(int val) { struct Node *temp=myFirst; while(temp->data!=val) temp=temp->last; struct Node *del=temp->last; struct Node *next=temp->last->last; temp->last=temp->last->last; next->before=temp;; delete del; } //method to delete element at a given posiiton void cutAtpos(int pos) { struct Node *temp=myFirst; for(int i=1; i<pos-1; i++) temp=temp->last; struct Node *del=temp->last; struct Node *next=temp->last->last; temp->last=temp->last->last; next->before=temp;; delete del; } }; int main() { DoublyLinkedList obj; obj.insertData(1); obj.insertData(2); obj.insertData(3); obj.insertData(4); obj.insertData(5); obj.insertData(6); obj.insertData(7); obj.insertData(8); obj.insertData(9); cout<<"\nLinkedlist is: "; obj.print(); obj.deleteBeforeData(6); cout<<"\nLinkedlist after deleting data before 6 is: "; obj.print(); obj.deleteAfterData(3); cout<<"\nLinkedlist after deleting data after 3 is: "; obj.print(); obj.cutAtpos(5); cout<<"\nLinkedlist after deletion at position 5 is: "; obj.print(); return 0; }
Output
Also read, For these situations, just design the h files you need to implement the classes you’d need.