Data Structure > C Comments Feed" href="https://www.c4learn.com/create-singly-linked-list-data.html/feed"/>

Create Singly Linked List > Data Structure > C

April 22, 2025 3 Comments » Hits : 1,138





Program to Create Singly Linked List .

void creat()
{
char ch;
 do
 {
 struct node *new_node,*current;
 new_node=(struct node *)malloc(sizeof(struct node));
 printf("\nEnter the data : ");
 scanf("%d",&new_node->data);
 new_node->next=NULL;
  if(start==NULL)
  {
  start=new_node;
  current=new_node;
  }
  else
  {
  current->next=new_node;
  current=new_node;
  }
 printf("\nDo you want to creat another : ");
 ch=getche();
 }while(ch!='n');
}

Step 1 : Include This Header File

  • For Allocating Dynamic Memory - Alloc & Malloc Functions are used.
  • These Functions are included in alloc.h
#include<alloc.h>

Step 2 : Define Node Structure

  • Declare start as Global Node Variable
struct node
{
  int data;
  struct node *next;
}*start=NULL;

Step 3 :Create Node

  • Allocate memory Dynamically.
  • For More Info : Malloc
new_node=(struct node *)malloc(sizeof(struct node));

Step 5 : Fill Information

  • Fill Data into Node
  • Make its Next Field as NULL.
printf("nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;


Step 6 : Creating Very First Node

  • If you are Creating First Node that means you don’t have starting node i.e start = NULL.
  • Then Assign that created node as start
  • Also Assign Current node as new_node
  • First node has 3 names as new_node,curr,start
if(start==NULL)
    {
    start=new_node;
    curr=new_node;
    }

Diagram :

Step 7 : Creating Second or nth node

  • Here start!=NULL
  • i.e You have First node
  • Then make link between curr and new_node
else
    {
    current->next=new_node;
    current=new_node;
    }

Incoming search terms:

  • Anonymous

    amazing… thanks alot

  • raj

    EXCELLENT VERY HELPFUL.REALLY GREAT

  • priti

    superb!!!!