C Program to Create a Linked List and Display the Elements in the List

Using this article, You are going to know about “C Program to Create a Linked List and Display the Elements in the List“. Yes, Shall we start the article to learn about it? Come on! Let’s go.

C Program to Create a Linked List and Display the Elements in the List

C Program to Create a Linked List and Display the Elements in the List

Source Code

/*
 * C program to create a linked list and display the elements in the list
 */
#include <stdio.h>
#include <malloc.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 itemn");
        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);
 
    }
    temp->ptr = 0;
    /*  reset temp to the beginning */
    temp = first;
    printf("n status of the linked list isn");
    while (temp != 0)
    {
        printf("%d=>", temp->num);
        count++;
        temp = temp -> ptr;
    }
    printf("NULLn");
    printf("No. of nodes in the list = %dn", count);
}

Final Words

I hope this article helps you to learn “C Program to Create a Linked List and Display the Elements in the List“. If you face any issues please let me know via the comment section. Share this article with other C/C++ program developers via social networks. Thank You!

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment