creating and displaying linked list in c

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

struct node 
{
    int num;                        //Data of the node
    struct node *nextptr;           //Address of the next node
}*stnode;

void createNodeList(int n); // function to create the list
void displayList();         // function to display the list

int main()
{
    int n;
		printf("\n\n Linked List : To create and display Singly Linked List :\n");
		printf("-------------------------------------------------------------\n");
		
    printf(" Input the number of nodes : ");
    scanf("%d", &n);
    createNodeList(n);
    printf("\n Data entered in the list : \n");
    displayList();
    return 0;
}
void createNodeList(int n)
{
    struct node *fnNode, *tmp;
    int num, i;
    stnode = (struct node *)malloc(sizeof(struct node));

    if(stnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
    {
        printf(" Memory can not be allocated.");
    }
    else
    {
// reads data for the node through keyboard

        printf(" Input data for node 1 : ");
        scanf("%d", &num);
        stnode->num = num;      
        stnode->nextptr = NULL; // links the address field to NULL
        tmp = stnode;
// Creating n nodes and adding to linked list
        for(i=2; i<=n; i++)
        {
            fnNode = (struct node *)malloc(sizeof(struct node));
            if(fnNode == NULL)
            {
                printf(" Memory can not be allocated.");
                break;
            }
            else
            {
                printf(" Input data for node %d : ", i);
                scanf(" %d", &num);
 
                fnNode->num = num;      // links the num field of fnNode with num
                fnNode->nextptr = NULL; // links the address field of fnNode with NULL
 
                tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode
                tmp = tmp->nextptr; 
            }
        }
    }
}
void displayList()
{
    struct node *tmp;
    if(stnode == NULL)
    {
        printf(" List is empty.");
    }
    else
    {
        tmp = stnode;
        while(tmp != NULL)
        {
            printf(" Data = %d\n", tmp->num);       // prints the data of current node
            tmp = tmp->nextptr;                     // advances the position of current node
        }
    }
} 


Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
creating linkedlist in C use of a linked list in c creating singly linked list in c how to create a linked list in c with n nodes display linked list in c Program To Create And Display A Singly Linked List in c how to build a linked list c create a linked list in c step by step make and display a linked list in c linked list tutorial in c making linked list in c c create and use linked list write a c program to create and display singly linked list code to create linked list in c creation of linked list in c create singly linked list in c creating linked lists in c c creating a linked list create a linked list with all operations in c create linked list c how to create a linked list c how to make a linked list in c write a program in c to create and display singly linked list making a linked list in c make linked list in c how to create linked list in c create a linked list of n nodes in c singly linked list display in c function to create a linked list in c c code for creation of linked list create a singly linked list in c display a linked list in c input into linked list c write a c program for singly linked list perform operations create sll write a program for singly linked list WAP to display the by list read name in linked list in c c language contribution on creating node singly linked list program display in c c program to display linked list C program to creating a single linked list with n elements. Code for displaying nodes of linked list. linked list program user input in c create node in c program in c for singly linked list make a list in c how to display the single linked list in c how to create a list in c programming creation of a linked list in c create a linked list c program to create a linked list creation of singly linked list in c function to create and display linked list how to create linked list linked list creation in c linked list display in c how to display list in c create linked list c program linked list creation display elements in linked list c linked list display program in c write a code for creation of linked list Write a pogram (WAP) to create the linked list of n nodes. creating a linked list in c creating a linked list how to display data of link list how to create a linked list in c how to create node in c create linked list in c Write a program (WAP) to create the linked list of n nodes. take input and dispay linked list how to create lihnked list in C creating linked list in c create a linked list node in c create a linked list in c code foe creating a linked list and diaplay
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source