Oct 4, 2011

C program to create a linked list and display the elements in the list.

Data structures using C,
C program to create a linked list and display the elements in the list. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. Read more about C Programming Language .


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                                  Happy Coding
***********************************************************/

#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>

void main()
{
 struct node
 {
  int num;
  struct node *ptr;
 };

 typedef struct node NODE;

 NODE *head, *first, *temp=0;
 int count = 0;
 int choice = 1;
 first = 0;

 while(choice)
 {
  head =(NODE*) malloc(sizeof(NODE));
  printf("Enter the data item\n");
  scanf("%d", &head-> num);

  if(first != 0)
  {
   temp->ptr = head;
   temp = head;
  }
  else
  {
   first = temp = head;
  }
  fflush(stdin);
  printf("Do you want to continue(Type 0 or 1)?\n");
  scanf("%d", &choice);

 }         /* End of while */

 temp->ptr = 0;
 temp = first;  /* reset temp to the beginning*/
 printf("\nstatus of the linked list is\n");

 while(temp!=0)
 {
  printf("%d=>", temp->num);
  count++;
  temp = temp -> ptr;
 }
 printf("NULL\n");
 printf("No. of nodes in the list = %d\n", count);
}  /* End of main*/
Read more Similar C Programs
Data Structures

Learn C Programming
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!