Insertion of node at end in singly linked list

0 like 0 dislike
1.3k views

edited
insertion of node at end in singly linked list

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited

Insertion of node at end

Algorithm to insert node at the end

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

Example

Program to insert a node at the end

#include <stdio.h>

#include<conio.h>

#include<stdlib.h>

void end(int);

struct node

{

int data;

struct node *next;

};

void end(int item)

{

struct node *ptr = (struct node*)malloc(sizeof(struct node));

struct node *temp;

if(ptr == NULL)

{

printf("\nOVERFLOW");

}

else

{

ptr->data = item;

{

ptr -> next = NULL;

printf("\nNode inserted");

}

else

{

while (temp -> next != NULL)

{

temp = temp -> next;

}

temp->next = ptr;

ptr->next = NULL;

printf("\ngiven node is inserted");

}      }  }

void display()

{

struct node *ptr;

if(ptr == NULL)

{

printf("Nothing to print");

}

else

{

printf("\ninsertion of node at end\n");

while (ptr!=NULL)

{

printf("\n%d",ptr->data);

ptr = ptr -> next;

}    }  }

void main ()

{

int item;

printf("\nEnter the item which you want to insert : \n");

scanf("%d",&item);

end(item);

display();

}

Output

For more Manipal University Jaipur B.Tech CSE-III Sem Data Structure Lab Experiments Click here

0 like 0 dislike
by (592 points)

27/04/2020

```Algorithm to insert node at the end of a Singly Linked List
Begin:
alloc (newNode)
If (newNode == NULL) then
write ('Unable to allocate memory')
End if
Else then
newNode.data ← data
newNode.next ← NULL
While (temp.next != NULL) do
temp ← temp.next
End while
temp.next ← newNode
End else
End```

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.