TheDeveloperBlog.com

Home | Contact Us

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

Insertion in Singly Linked List at End

Insertion in 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 in singly linked list at the end

In order to insert a node at the last, there are two following scenarios which need to be mentioned.

  1. The node is being added to an empty list
  2. The node is being added to the end of the linked list

in the first case,

  • The condition (head == NULL) gets satisfied. Hence, we just need to allocate the space for the node by using malloc statement in C. Data and the link part of the node are set up by using the following statements.
ptr->data = item;
				ptr -> next = NULL;
  • Since, ptr is the only node that will be inserted in the list hence, we need to make this node pointed by the head pointer of the list. This will be done by using the following Statements.
Head = ptr   

In the second case,

  • The condition Head = NULL would fail, since Head is not null. Now, we need to declare a temporary pointer temp in order to traverse through the list. temp is made to point the first node of the list.
Temp = head 
  • Then, traverse through the entire linked list using the statements:
while (temp→ next != NULL)
			temp = temp → next;
  • At the end of the loop, the temp will be pointing to the last node of the list. Now, allocate the space for the new node, and assign the item to its data part. Since, the new node is going to be the last node of the list hence, the next part of this node needs to be pointing to the null. We need to make the next part of the temp node (which is currently the last node of the list) point to the new node (ptr) .
temp = head;
		while (temp -> next != NULL)
		{
			temp = temp -> next;
		}
		temp->next = ptr;
		ptr->next = NULL;

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 = NULL
  • Step 6: SET PTR = HEAD
  • Step 7: Repeat Step 8 while PTR - > NEXT != NULL
  • Step 8: SET PTR = PTR - > NEXT
    [END OF LOOP]
  • Step 9: SET PTR - > NEXT = NEW_NODE
  • Step 10: EXIT

inserting node at the last into a non empty list

C Function

#include<stdio.h>
#include<stdlib.h>
void lastinsert(int);
struct node
{
	int data;
	struct node *next;
};
struct node *head;
void main ()
{
	int choice,item;
	do 
	{
		printf("\nEnter the item which you want to insert?\n");
		scanf("%d",&item);
		lastinsert(item);
		printf("\nPress 0 to insert more ?\n");
		scanf("%d",&choice);
	}while(choice == 0);
}
void lastinsert(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)
			{
				ptr -> next = NULL;
				head = ptr;
				printf("\nNode inserted");
			}
			else
			{
				temp = head;
				while (temp -> next != NULL)
				{
					temp = temp -> next;
				}
				temp->next = ptr;
				ptr->next = NULL;
				printf("\nNode inserted");
			
			}
		}
	}

Output

Enter the item which you want to insert?
12

Node inserted
Press 0 to insert more ?
0

Enter the item which you want to insert?
23

Node inserted
Press 0 to insert more ?
2

Next Topic#




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