TheDeveloperBlog.com

Home | Contact Us

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

Insertion in Circular Singly Linked List at End

Insertion 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 INSERTION

Insertion into circular singly linked list at the end

There are two scenario in which a node can be inserted in circular singly linked list at beginning. Either the node will be inserted in an empty list or the node is to be inserted in an already filled list.

  • Firstly, allocate the memory space for the new node by using the malloc method of C language.
  • struct node *ptr = (struct node *)malloc(sizeof(struct node));
    

    In the first scenario, the condition head == NULL will be true. Since, the list in which, we are inserting the node is a circular singly linked list, therefore the only node of the list (which is just inserted into the list) will point to itself only. We also need to make the head pointer point to this node. This will be done by using the following statements.

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

    In the second scenario, the condition head == NULL will become false which means that the list contains at least one node. In this case, we need to traverse the list in order to reach the last node of the list. This will be done by using the following statement.

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

    At the end of the loop, the pointer temp would point to the last node of the list. Since, the new node which is being inserted into the list will be the new last node of the list. Therefore the existing last node i.e. temp must point to the new node ptr. This is done by using the following statement.

    temp -> next = ptr; 
    

    The new last node of the list i.e. ptr will point to the head node of the list.

    ptr -> next = head;
    

    In this way, a new node will be inserted in a circular singly linked list at the beginning.

    Algorithm

    • Step 1: IF PTR = NULL
    •   Write OVERFLOW
         Go to Step 1
        [END OF IF]

    • Step 2: SET NEW_NODE = PTR
    • Step 3: SET PTR = PTR -> NEXT
    • Step 4: SET NEW_NODE -> DATA = VAL
    • Step 5: SET NEW_NODE -> NEXT = HEAD
    • Step 6: SET TEMP = HEAD
    • Step 7: Repeat Step 8 while TEMP -> NEXT != HEAD
    • Step 8: SET TEMP = TEMP -> NEXT
    • [END OF LOOP]

    • Step 9: SET TEMP -> NEXT = NEW_NODE
    • Step 10: EXIT

    Insertion into circular singly linked list at the end

    C Function

    void lastinsert(struct node*ptr, struct node *temp, int item)
    {
    	ptr = (struct node *)malloc(sizeof(struct node));
    	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;
    			}
    			temp -> next = ptr; 
    			ptr -> next = head;
    		}
    	}
    
    }
    

    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