Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct Node{
- public:
- int data;
- Node* next;
- Node* prev;
- };
- class DLL{
- Node* head;
- public:
- DLL(){
- head = NULL;
- }
- void insertAtTheHead(int dataToInsert){
- /* 1. allocate node */
- Node* new_node = new Node();
- /* 2. put in the data */
- new_node->data = dataToInsert;
- /* 3. Make next of new node as head and previous as NULL */
- new_node->next = head;
- new_node->prev = NULL;
- /* 4. change prev of head node to new node in case of existing head */
- if ((head) != NULL)
- head->prev = new_node;
- /* 5. move the head to point to the new node */
- head = new_node;
- }
- void insertAtTheEnd(int dataToInsert){
- /* 1. allocate node */
- Node* new_node = new Node();
- /* 2. put in the data */
- new_node->data = dataToInsert;
- /* 3. Make next and prev of new node as NULL */
- new_node->next = NULL;
- new_node->prev = NULL;
- /* 4a. If head is NULL, add this node as the head; */
- if(head==NULL){
- head = new_node;
- }
- else{ /* 4. Find out the last node and add new node after that */
- Node* t = head;
- while(t->next !=NULL)
- t=t->next;
- t->next = new_node;
- new_node->prev = t;
- }
- }
- void insertAfterValue(int valueToInsert, int afterValue){
- Node* nn = new Node();
- nn->data = valueToInsert;
- nn->next=nn->prev = NULL;
- Node* t = head;
- while(t->data != afterValue){
- t = t->next;
- if(t==NULL){
- cout<<afterValue<<" does not exist"<<endl;
- }
- }
- if(t->next == NULL){
- t->next = nn;
- nn->prev = t;
- }
- else{
- nn->next = t->next;
- t->next->prev = nn;
- nn->prev = t;
- t->next = nn;
- }
- }
- void insertBeforeValue(int valueToInsert, int beforeValue){
- Node* nn = new Node();
- nn->data = valueToInsert;
- nn->next=nn->prev = NULL;
- Node* t = head;
- while(t->data != beforeValue){
- t = t->next;
- if(t==NULL){
- cout<<beforeValue<<" does not exist"<<endl;
- }
- }
- if(t->prev == NULL){
- t->prev = nn;
- nn->next = t;
- head = nn;
- }
- else{
- nn->prev = t->prev;
- t->prev->next = nn;
- nn->next = t;
- t->prev = nn;
- }
- }
- void display(){
- Node* t = head;
- while(t!=NULL){
- cout<<t->data<<" ";
- t=t->next;
- }
- cout<<endl;
- }
- void backwardDisplay(){
- Node* t = head;
- while(t->next!=NULL)
- t = t->next;
- while(t!=NULL){
- cout<<t->data<<" ";
- t=t->prev;
- }
- cout<<endl;
- }
- void deleteAValue(int Value){
- if (head == NULL){
- cout<<" Linked List does not exist.."<<endl;
- return;
- }
- /*Finding the node to be deleted*/
- Node* del = head;
- if(head == NULL){
- cout<<"DLL does not exist..";
- return;
- }
- while(del->data !=Value){
- del = del->next;
- if(del==NULL){ /* Data Not Found*/
- cout<<" Data does not exist.."<<endl;
- return;
- }
- }
- if (del->next !=NULL)
- del->next->prev = del->prev;
- if(del->prev!=NULL)
- del->prev->next = del->next;
- if (head == del)
- head = del->next;
- free(del);
- }
- };
- int main(){
- DLL dobj;
- dobj.deleteAValue(100);
- dobj.insertAtTheEnd(10);
- dobj.insertAtTheHead(5);
- dobj.insertAtTheHead(2);
- dobj.insertAtTheEnd(20);
- dobj.display();
- dobj.deleteAValue(100);
- dobj.deleteAValue(2);
- dobj.display();
- dobj.deleteAValue(10);
- dobj.display();
- dobj.insertAfterValue(1000,5);
- dobj.display();
- dobj.insertAfterValue(2000,20);
- dobj.display();
- dobj.insertBeforeValue(7000,5);
- dobj.display();
- dobj.backwardDisplay();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement