FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
1.3k views
in Manipal University Jaipur B.Tech(CSE-III Sem) Data Structure Lab by Goeduhub's Expert (7.6k points)
edited by
insertion of node at end in singly linked list

2 Answers

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

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

 image

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;  

};  

struct node *head;  

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;  

            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("\ngiven node is inserted");              

            }      }  }

    void display()  

{  

    struct node *ptr;  

    ptr = head;   

    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

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:
    createSinglyLinkedList (head)
    alloc (newNode)
    If (newNode == NULL) then
        write ('Unable to allocate memory')
    End if
    Else then
        read (data)
        newNode.datadata
        newNode.nextNULL
        temphead
        While (temp.next != NULL) do
            temptemp.next
        End while
        temp.nextnewNode
    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.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...