creating and displaying linked list in c

Solutions on MaxInterview for creating and displaying linked list in c by the best coders in the world

showing results for - "creating and displaying linked list in c"
Fiona
09 Sep 2020
1#include <stdio.h>
2#include <stdlib.h>
3
4struct node 
5{
6    int num;                        //Data of the node
7    struct node *nextptr;           //Address of the next node
8}*stnode;
9
10void createNodeList(int n); // function to create the list
11void displayList();         // function to display the list
12
13int main()
14{
15    int n;
16		printf("\n\n Linked List : To create and display Singly Linked List :\n");
17		printf("-------------------------------------------------------------\n");
18		
19    printf(" Input the number of nodes : ");
20    scanf("%d", &n);
21    createNodeList(n);
22    printf("\n Data entered in the list : \n");
23    displayList();
24    return 0;
25}
26void createNodeList(int n)
27{
28    struct node *fnNode, *tmp;
29    int num, i;
30    stnode = (struct node *)malloc(sizeof(struct node));
31
32    if(stnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
33    {
34        printf(" Memory can not be allocated.");
35    }
36    else
37    {
38// reads data for the node through keyboard
39
40        printf(" Input data for node 1 : ");
41        scanf("%d", &num);
42        stnode->num = num;      
43        stnode->nextptr = NULL; // links the address field to NULL
44        tmp = stnode;
45// Creating n nodes and adding to linked list
46        for(i=2; i<=n; i++)
47        {
48            fnNode = (struct node *)malloc(sizeof(struct node));
49            if(fnNode == NULL)
50            {
51                printf(" Memory can not be allocated.");
52                break;
53            }
54            else
55            {
56                printf(" Input data for node %d : ", i);
57                scanf(" %d", &num);
58 
59                fnNode->num = num;      // links the num field of fnNode with num
60                fnNode->nextptr = NULL; // links the address field of fnNode with NULL
61 
62                tmp->nextptr = fnNode; // links previous node i.e. tmp to the fnNode
63                tmp = tmp->nextptr; 
64            }
65        }
66    }
67}
68void displayList()
69{
70    struct node *tmp;
71    if(stnode == NULL)
72    {
73        printf(" List is empty.");
74    }
75    else
76    {
77        tmp = stnode;
78        while(tmp != NULL)
79        {
80            printf(" Data = %d\n", tmp->num);       // prints the data of current node
81            tmp = tmp->nextptr;                     // advances the position of current node
82        }
83    }
84} 
85
86
queries leading to this page
create a linked list node in cwrite a program 28wap 29 to create the linked list of n nodes creating a singly linked list in cmaking a linked list in cwap to display the by listc create and use linked listcode to create linked list in chow to create a list in c programmingcreating and initializing linked list cdisplay a linked list in clinked list display program in chow to create node in cmake and display a linked list in cbuild a linked list in chow to create linked listwrite a pogram 28wap 29 to create the linked list of n nodes c language contribution on creating nodeuse of a linked list in ccreate linked list in ccreate node in clinked list program user input in ccreation of singly linked list in clinked list creation in chow to create linked list in ccreating a linked list in cc program to creating a single linked list with n elements function to create a linked list in cdisplay elements in linked list ccreate a linked listcreate a linked list in c step by stepcreation of a linked list in cwrite a c program for singly linked list perform operations create slllinked list creationhow to create lihnked list in ccreating singly linked list in chow to display a linked list chow to make a linked list in ccreate linked list cc program to display linked listlinked list tutorial in ccode foe creating a linked list and diaplaydisplay linked list in cwrite a program for singly linked listcode for displaying nodes of linked list make linked list in chow to create and use a linked list in ccreating linkedlist in cmaking linked list in cmake a list in clinked list display in ccreate a linked list in cfunction to create and display linked listsingly linked list program display in ccreating linked list in ccreating linked lists in ctake input and dispay linked listcreate a linked list of n nodes in ccreate singly linked list in ccreate linked list c programcreate a singly linked list in cc creating a linked listprogram in c for singly linked listcan you create a linked list in cinput into linked list ccreating a linked listwrite a code for creation of linked listhow to display the single linked list in ccreate and insert and display to perform singly linked list in chow to create a linked list chow to create a linked list in c with n nodeshow to display list in cprogram to create and display a singly linked list in chow to create a linked list in cwrite a program in c to create and display singly linked listc program to create a linked listread name in linked list in chow to display data of link listsingly linked list display in cwrite a c program to create and display singly linked listhow to build a linked list ccreate a linked list with all operations in ccreation of linked list in cc code for creation of linked listcreating and displaying linked list in c