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

1 Answer

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

Program to show insertion of node at beginning

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. 

image

  • A node contains two fields i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory.
  • The last node of the list contains pointer to the null.

The simplest kind of linked list is a singly liked list (SLL) which has one link per node. It has two parts, one part contains data and other contains address of next node. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.The structure of a node in a SLL is given as in C: 

struct node

{

    int data;

    struct node *next;

};

struct node *head, *ptr;   

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

Algorithm to insert a node at beginning

Step 1: IF PTR = NULL 

Write OVERFLOW 

Go to Step 7
[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 HEAD = NEW_NODE

Step 7: EXIT

Example

 image

Program

#include <stdio.h>

#include<conio.h>

#include<stdlib.h>  

void front(int);  

struct node  

{  

    int data;  

    struct node *next;  

};  

struct node *head;  

void display()  

{  

    struct node *ptr;  

    ptr = head;   

    if(ptr == NULL)  

    {  

        printf("Nothing to print");  

    }  

    else  

    {  

        printf("\nprinting values . . . . .\n");   

        while (ptr!=NULL)  

        {  

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

            ptr = ptr -> next;  

        }  

    }  

void front(int item)  

    {  

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

        if(ptr == NULL)  

        {  

            printf("\nOVERFLOW !!!\n");  

        }  

        else  

        {  

            ptr->data = item;  

            ptr->next = head;  

            head = ptr;  

            printf("\nGiven node is inserted\n");  

        }            

    } 

    void main ()  

{  

    int item;  

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

        scanf("%d",&item);  

        front(item); 

        display();  

Output

image


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


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

 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

...