TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Deletion in Circular Doubly Linked List at Beginning

Deletion in Circular Doubly Linked List at Beginning with Introduction, Asymptotic Analysis, Array, Pointer, Structure, Singly Linked List, Doubly Linked List, Circular Linked List, Binary Search, Linear Search, Sorting, Bucket Sort, Comb Sort, Shell Sort, Heap Sort, Merge Sort, Selection Sort, Counting Sort, Stack, Qene, Circular Quene, Graph, Tree, B Tree, B+ Tree, Avl Tree etc.

<< Back to DELETION

Deletion in Circular doubly linked list at beginning

There can be two scenario of deleting the first node in a circular doubly linked list.

The node which is to be deleted can be the only node present in the linked list. In this case, the condition head → next == head will become true, therefore the list needs to be completely deleted.

It can be simply done by assigning head pointer of the list to null and free the head pointer.

head = NULL; 
	free(head);

in the second scenario, the list contains more than one element in the list, therefore the condition head → next == head will become false. Now, reach the last node of the list and make a few pointer adjustments there. Run a while loop for this purpose

	temp = head; 
		while(temp -> next != head)
		{
			temp = temp -> next;
		}

Now, temp will point to the last node of the list. The first node of the list i.e. pointed by head pointer, will need to be deleted. Therefore the last node must contain the address of the node that is pointed by the next pointer of the existing head node. Use the following statement for this purpose.

temp -> next = head -> next;

The new head node i.e. next of existing head node must also point to the last node of the list through its previous pointer. Use the following statement for this purpose.

head -> next -> prev = temp;

Now, free the head pointer and the make its next pointer, the new head node of the list.

free(head);
		head = temp -> next;

in this way, a node is deleted at the beginning from a circular doubly linked list.

Algorithm

  • Step 1: IF HEAD = NULL
  • Write UNDERFLOW
    Go to Step 8
    [END OF IF]

  • Step 2: SET TEMP = HEAD
  • Step 3: Repeat Step 4 while TEMP -> NEXT != HEAD
  • Step 4: SET TEMP = TEMP -> NEXT
  • [END OF LOOP]

  • Step 5: SET TEMP -> NEXT = HEAD -> NEXT
  • Step 6: SET HEAD -> NEXT -> PREV = TEMP
  • Step 7: FREE HEAD
  • Step 8: SET HEAD = TEMP -> NEXT

Deletion in Circular doubly linked list at beginning

C Function

#include<stdio.h>
#include<stdlib.h>
void create(int);
void deletion_beginning();
struct node
{
	int data;
	struct node *next;
	struct node *prev;
};
struct node *head;
void main ()
{
	int choice,item,choice1;
	do 
	{
		printf("1.Append List\n2.Delete Node from beginning\n3.Exit\n4.Enter your choice?");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:
			printf("\nEnter the item\n");
			scanf("%d",&item);
			create(item);
			break; 
			case 2:
			deletion_beginning();
			break; 
			case 3:
			exit(0);
			break;	
			default:
			printf("\nPlease Enter valid choice\n");
		}
				
	}while(choice != 3);
}
void create(int item)
{
   struct node *ptr = (struct node *) malloc(sizeof(struct node));
   struct node *temp; 
	if(ptr == NULL)
   {
       printf("\nOVERFLOW\n");
   }
   else
   {
        ptr->data=item;
       if(head == NULL)
       {
           head = ptr;
      	   ptr -> next = head; 
     	   ptr -> prev = head; 
       }
       else
       {
          temp = head;
          while(temp->next !=head)
          {
              temp = temp->next;
          }
          temp->next = ptr;
          ptr ->prev=temp;
          head -> prev = ptr;
		   ptr -> next = head;
        }
   }
     printf("\nNode Inserted\n");
}
void deletion_beginning()
{
    struct node *temp;
    if(head == NULL)
	{
		printf("\n UNDERFLOW\n");
	}
	else if(head->next == head)
	{
		head = NULL; 
		free(head);
		printf("\nNode Deleted\n");
	}
	else
	{
		temp = head; 
		while(temp -> next != head)
		{
			temp = temp -> next;
		}
		temp -> next = head -> next;
		head -> next -> prev = temp;
		free(head);
		head = temp -> next;
		printf("\nNode Deleted\n");
	}

}

Output

1.Append List
2.Delete Node from beginning
3.Exit
4.Enter your choice?1

Enter the item
12

Node Inserted
1.Append List
2.Delete Node from beginning
3.Exit
4.Enter your choice?2

Node Deleted

Next TopicDoubly Linked List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf