Site icon T4Tutorials.com

Doubly Link List C++ Easy Code

Let us see the C++ Code to insert at the start(beginning) in Doubly Link List

Doubly Link List – insert at the start(beginning)

insert at start

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

Complete source code

// Doubly Link  List - insert at the start(beginning)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->PreviousNode = NULL; 
   newdoublyNode->NextNode = head;     
   if(head !=  NULL) 
   {head->PreviousNode = newdoublyNode ;
   }           
   head = newdoublyNode; 
}   
void display() { 
   struct DoublyNode* ptr;
   ptr = head;  
   while(ptr != NULL) { 
      cout<< ptr->data <<" "; 
      ptr = ptr->NextNode; 
  } 
} 
int main() { 
   insert(7);
   insert(8);
   cout<<"The doubly linked list is: ";
   display(); 
   return 0; 
}

Output

The doubly linked list is: 8 7

Doubly Link List – delete a node at the start(beginning)- (left most node)

This will delete the node on the left side of the link list. left side means at the beginning of the 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->NextNode;
   head->PreviousNode = NULL;
   free(temp);
   }
  

Complete source code

// Doubly Link List - insert at the start(beginning) of the double link list 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->PreviousNode = NULL; 
   newdoublyNode->NextNode = head;     
   if(head !=  NULL) 
   {head->PreviousNode = 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->NextNode;
   head->PreviousNode = NULL;
   free(temp);
   }
  
  
void display() { 
   struct DoublyNode*  ptr;
   ptr = head;  
   while(ptr != NULL) { 
      cout<< ptr->data <<" "; 
      ptr = ptr->NextNode; 
  } 
} 
int main() { 
   insert(7);

   cout<<"The doubly linked list is: ";
 del();
   display(); 
   
   return 0; 
}

Output

The doubly linked list is: 7

Another code sample for delete at the beginning

void node_deletion() 
{
	if (head==NULL) 
//Supposing that no nodes have been inserted
		{
			cout<<"No node to delete"<<endl;
		}
		else
		{	
			DoublyNode* temp=head;
			cout<<head->data<<" is deleted from the list\n";
			head=head->NextNode; 
			if (head!=NULL)
			{
				head->PreviousNode=NULL;
			}
			free(temp);
		}
	
		}

 

Exit mobile version