C program to create singly linked list.

Posted by Mangesh on May 08, 2018

/* program to create a linked list */

Description :

Singly Link list
A singly link list's node is divided into twofield. The first field holds information about the node, and second field holds the reference of next node. When we link all node is some order then it becomes Singly Link list. In singly link list we have only one reference of next node, so in singly link list we can travels in only one way. The reference (link) points to the next node in the list, or to a null value( if its last node). For Singly Link list we need one Head reference to point to the First node of the list.

Program :

#include<stdio.h>
#include<conio.h>
typedef struct node
{
  int data;
  struct node *next;
} node;
void display(node *list)
{
  printf("\nElements in linked list : ");
  while(list != NULL)
  {
    printf("  %d",list->data);
    list = list->next;
  }
}
void main()
{
  node *head, *tail;
  char character ='y';
  head = (node *) malloc (sizeof(node));
  clrscr();
  tail = head;
  while(character == 'y'){
  printf("\nEnter the element value : ");
  scanf("%d",&tail->data);
  printf("Do you want to add more element (y/n) : ");
  scanf("%s",&character);
  if(character=='y')
  {
    tail->next =  (node *) malloc(sizeof(node));
    tail = tail->next;
  }
  else
    tail->next=NULL;
  }
  display(head);
getch();
}

Output :

c program linked list Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to create singly linked list.
2 C program to display elements of linked list in reverse order.
3 C program to count number of node in linked list.
4 C program to print a linked list in reverals order.
5 C program to search an element in linked list.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.