C Program to Create a Linked List and Display it

This C Program create a linked list & display the elements in the list. Linked list is an ordered set of data elements, each containing a link to its successor. This program is to create a linked list and display all the elements present in the created list.

Here is source code of the C program to create a linked list & display the elements in the list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to create a linked list and display the elements in the list
  3.  */
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <stdlib.h>
  7.  
  8. void main()
  9. {
  10.     struct node
  11.     {
  12.         int num;
  13.         struct node *ptr;
  14.     };
  15.     typedef struct node NODE;
  16.  
  17.     NODE *head, *first, *temp = 0;
  18.     int count = 0;
  19.     int choice = 1;
  20.     first = 0;
  21.  
  22.     while (choice)
  23.     {
  24.         head  = (NODE *)malloc(sizeof(NODE));
  25.         printf("Enter the data item\n");
  26.         scanf("%d", &head-> num);
  27.         if (first != 0)
  28.         {
  29.             temp->ptr = head;
  30.             temp = head;
  31.         }
  32.         else
  33.         {
  34.             first = temp = head;
  35.         }
  36.         fflush(stdin);
  37.         printf("Do you want to continue(Type 0 or 1)?\n");
  38.         scanf("%d", &choice);
  39.  
  40.     }
  41.     temp->ptr = 0;
  42.     /*  reset temp to the beginning */
  43.     temp = first;
  44.     printf("\n status of the linked list is\n");
  45.     while (temp != 0)
  46.     {
  47.         printf("%d=>", temp->num);
  48.         count++;
  49.         temp = temp -> ptr;
  50.     }
  51.     printf("NULL\n");
  52.     printf("No. of nodes in the list = %d\n", count);
  53. }

$ cc pgm98.c
$ a.out
Enter the data item
5
Do you want to continue(Type 0 or 1)?
0
 
status of the linked list is
5=>NULL
No. of nodes in the list = 1
 
$ a.out
Enter the data item
5
Do you want to continue(Type 0 or 1)?
1
Enter the data item
9
Do you want to continue(Type 0 or 1)?
1
Enter the data item
3
Do you want to continue(Type 0 or 1)?
0
 
status of the linked list is
5=>9=>3=>NULL
No. of nodes in the list = 3

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Linked List, go to C Programming Examples on Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.