TheDeveloperBlog.com

Home | Contact Us

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

Deletion in Circular Singly Linked List at Beginning

Deletion in Circular Singly 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 singly linked list at beginning

In order to delete a node in circular singly linked list, we need to make a few pointer adjustments.

There are three scenarios of deleting a node from circular singly linked list at beginning.

Scenario 1: (The list is Empty)

If the list is empty then the condition head == NULL will become true, in this case, we just need to print underflow on the screen and make exit.

if(head == NULL)
	{
		printf("\nUNDERFLOW");	
		return; 
	}

Scenario 2: (The list contains single node)

If the list contains single node then, the condition head → next == head will become true. In this case, we need to delete the entire list and make the head pointer free. This will be done by using the following statements.

 if(head->next == head)
	{
		head = NULL;
		free(head);
	}

Scenario 3: (The list contains more than one node)

If the list contains more than one node then, in that case, we need to traverse the list by using the pointer ptr to reach the last node of the list. This will be done by using the following statements.

ptr = head; 
			while(ptr -> next != head)
				ptr = ptr -> next; 

At the end of the loop, the pointer ptr point to the last node of the list. Since, the last node of the list points to the head node of the list. Therefore this will be changed as now, the last node of the list will point to the next of the head node.

ptr->next = head->next;

Now, free the head pointer by using the free() method in C language.

free(head);

Make the node pointed by the next of the last node, the new head of the list.

head = ptr->next;

In this way, the node will be deleted from the circular singly linked list from the beginning.

Algorithm

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

  • Step 2: SET PTR = HEAD
  • Step 3: Repeat Step 4 while PTR → NEXT != HEAD
  • Step 4: SET PTR = PTR → next
  • [END OF LOOP]

  • Step 5: SET PTR → NEXT = HEAD → NEXT
  • Step 6: FREE HEAD
  • Step 7: SET HEAD = PTR → NEXT
  • Step 8: EXIT

Deletion in circular singly linked list at beginning

C Function

#include<stdio.h>
#include<stdlib.h>
void create(int);
void beg_delete();
struct node
{
	int data;
	struct node *next;
};
struct node *head;
void main ()
{
	int choice,item;
	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:
			beg_delete();
			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");  
    }  
    else   
    {  
        ptr -> data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;  
        }  
        else   
        {     
            temp = head;  
            while(temp->next != head)  
                temp = temp->next;  
            ptr->next = head;   
            temp -> next = ptr;   
            head = ptr;  
        }   
	printf("\nNode Inserted\n");
    }  
              
} 
void beg_delete()
{
	struct node *ptr;
	if(head == NULL)
	{
		printf("\nUNDERFLOW\n");	
	}
	else if(head->next == head)
	{
		head = NULL;
		free(head);
		printf("\nNode Deleted\n"); 
	}
	
	else
	{	
		ptr = head; 
		while(ptr -> next != head)
			ptr = ptr -> next; 
		ptr->next = head->next;
		free(head);
		head = ptr->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