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 End

Deletion in Circular Singly Linked List at End 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 the end

There are three scenarios of deleting a node in circular singly linked list at the end.

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 element)

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 element)

If the list contains more than one element, then in order to delete the last element, we need to reach the last node. We also need to keep track of the second last node of the list. For this purpose, the two pointers ptr and preptr are defined. The following sequence of code is used for this purpose.

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

now, we need to make just one more pointer adjustment. We need to make the next pointer of preptr point to the next of ptr (i.e. head) and then make pointer ptr free.

preptr->next = ptr -> next;
		free(ptr);

Algorithm

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

  • Step 2: SET PTR = HEAD
  • Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
  • Step 4: SET PREPTR = PTR
  • Step 5: SET PTR = PTR -> NEXT
  • [END OF LOOP]

  • Step 6: SET PREPTR -> NEXT = HEAD
  • Step 7: FREE PTR
  • Step 8: EXIT

Deletion in Circular singly linked list at the end

C Function

#include<stdio.h>
#include<stdlib.h>
void create(int);
void last_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 end\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:
			last_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\n");  
    }  
    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 last_delete()
{
struct node *ptr, *preptr;	
	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)
		{
			preptr=ptr;
			ptr = ptr->next;
		}
		preptr->next = ptr -> next;
		free(ptr);
		printf("\nNode Deleted\n");
	}
}

Output

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

Enter the item
90

Node Inserted
1.Append List
2.Delete Node from end
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