Site icon T4Tutorials.com

Insert at the end in Doubly Link List C++ – Easy Code

Let us see the C++ source code of Insert at the end in Doubly Link List.

Doubly Link List – insert at the end

struct DoublyNode* head = NULL; 
void insert(int newdata) { 
   struct DoublyNode* newdoublyNode = (struct DoublyNode*) malloc(sizeof(struct DoublyNode)); 
   newdoublyNode->data  = newdata; 
   newdoublyNode->NextNode  = NULL;  
   newdoublyNode->PreviousNode = head;  
   if(head !=  NULL) 
   {head->NextNode = newdoublyNode ;
   }           
   head = newdoublyNode; 
}

Complete source code

//Insert at the end in Doubly Link List C++ - by t4tutorials.com
#include <iostream>
#include <cstdlib>
using namespace std;
struct DoublyNode { 
   int data; 
   struct DoublyNode *PreviousNode; 
   struct DoublyNode *NextNode; 
}; 
struct DoublyNode* head = NULL; 
void insert(int newdata) { 
   struct DoublyNode* newdoublyNode = (struct DoublyNode*) malloc(sizeof(struct DoublyNode)); 
   newdoublyNode->data  = newdata; 
   newdoublyNode->NextNode = NULL;  
   newdoublyNode->PreviousNode = head;  
   if(head !=  NULL) 
   {head->NextNode = newdoublyNode ;
   }           
   head = newdoublyNode; 
}   
void display() { 
   struct DoublyNode* ptr;
   ptr = head;  
   while(ptr != NULL) { 
      cout<< ptr->data <<" "; 
      ptr = ptr->PreviousNode; 
  } 
} 
int main() { 
   insert(4);
   insert(5);   
   insert(6);
   cout<<"The doubly linked list is: ";
   display(); 
   return 0; 
}

Output

The doubly linked list is: 6 5 4

Doubly Link List – delete at the end in Doubly Link List

void del() { 
   struct DoublyNode* temp; 
   if(head==NULL)
   {cout<<"empty";
   }
   else
   { temp  = head; 
   if(head->NextNode==NULL && head->PreviousNode == NULL)
   {
   	free(temp);
   	free(head);
   }
   }
   head = head->PreviousNode;
    head->NextNode = NULL;
   free(temp);
   }

Complete source code

//Insert and delete at the end in Doubly Link List (right most node among other nodes) C++ - by t4tutorials.com
#include <iostream>
#include <cstdlib>
using namespace std;
struct DoublyNode { 
   int data; 
   struct DoublyNode *PreviousNode; 
   struct DoublyNode *NextNode; 
}; 
struct DoublyNode* head = NULL; 
void insert(int newdata) { 
   struct DoublyNode* newdoublyNode = (struct DoublyNode*) malloc(sizeof(struct DoublyNode)); 
   newdoublyNode->data  = newdata; 
   newdoublyNode->NextNode = NULL;  
   newdoublyNode->PreviousNode = head;  
   if(head !=  NULL) 
    {head->NextNode = newdoublyNode ;
   }           
   head = newdoublyNode; 
} 
void del() { 
   struct DoublyNode* temp; 
   if(head==NULL)
   {cout<<"empty";
   }
   else
   { temp  = head; 
   if(head->NextNode==NULL && head->PreviousNode == NULL)
   {
   	free(temp);
   	free(head);
   }
   }
   head = head->PreviousNode;
   head->NextNode = NULL;
   free(temp);
   }  
void display() { 
   struct DoublyNode* ptr;
   ptr = head;  
   while(ptr != NULL) { 
      cout<< ptr->data <<" "; 
      ptr = ptr->PreviousNode; 
  } 
} 
int main() { 
   insert(4);
   insert(5);   
   insert(6);
   del();
   cout<<"The doubly linked list is: ";
   display(); 
   return 0; 
}

 

 

Exit mobile version