how to make a linked list in c

Solutions on MaxInterview for how to make a linked list in c by the best coders in the world

showing results for - "how to make a linked list in c"
Victoria
10 Aug 2019
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
Matteo
24 Nov 2016
1typedef struct node{
2    int value; //this is the value the node stores
3    struct node *next; //this is the node the current node points to. this is how the nodes link
4}node;
5
6node *createNode(int val){
7    node *newNode = malloc(sizeof(node));
8    newNode->value = val;
9    newNode->next = NULL;
10    return newNode;
11}
Carl
05 Jun 2016
1// https://github.com/davidemesso/LinkedListC for the full
2// implementation of every basic function
3
4typedef struct Node 
5{
6  // Void pointer content of this node (to provide multityping).
7  void* data;
8
9  // Points to the next Node in the list.   
10  struct Node* next;  
11} Node;
12
13Node *llNewList(void* data, Node* next)
14{
15    Node* result = 
16        (Node*)malloc(sizeof(Node));
17
18    result->data          = data;
19    result->next          = next;
20    
21    return result;
22}
Debora
25 Oct 2017
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdbool.h>
5
6struct node {
7   int data;
8   int key;
9   struct node *next;
10};
11
12struct node *head = NULL;
13struct node *current = NULL;
14
15//display the list
16void printList() {
17   struct node *ptr = head;
18   printf("\n[ ");
19	
20   //start from the beginning
21   while(ptr != NULL) {
22      printf("(%d,%d) ",ptr->key,ptr->data);
23      ptr = ptr->next;
24   }
25	
26   printf(" ]");
27}
28
29//insert link at the first location
30void insertFirst(int key, int data) {
31   //create a link
32   struct node *link = (struct node*) malloc(sizeof(struct node));
33	
34   link->key = key;
35   link->data = data;
36	
37   //point it to old first node
38   link->next = head;
39	
40   //point first to new first node
41   head = link;
42}
43
44//delete first item
45struct node* deleteFirst() {
46
47   //save reference to first link
48   struct node *tempLink = head;
49	
50   //mark next to first link as first 
51   head = head->next;
52	
53   //return the deleted link
54   return tempLink;
55}
56
57//is list empty
58bool isEmpty() {
59   return head == NULL;
60}
61
62int length() {
63   int length = 0;
64   struct node *current;
65	
66   for(current = head; current != NULL; current = current->next) {
67      length++;
68   }
69	
70   return length;
71}
72
73//find a link with given key
74struct node* find(int key) {
75
76   //start from the first link
77   struct node* current = head;
78
79   //if list is empty
80   if(head == NULL) {
81      return NULL;
82   }
83
84   //navigate through list
85   while(current->key != key) {
86	
87      //if it is last node
88      if(current->next == NULL) {
89         return NULL;
90      } else {
91         //go to next link
92         current = current->next;
93      }
94   }      
95	
96   //if data found, return the current Link
97   return current;
98}
99
100//delete a link with given key
101struct node* delete(int key) {
102
103   //start from the first link
104   struct node* current = head;
105   struct node* previous = NULL;
106	
107   //if list is empty
108   if(head == NULL) {
109      return NULL;
110   }
111
112   //navigate through list
113   while(current->key != key) {
114
115      //if it is last node
116      if(current->next == NULL) {
117         return NULL;
118      } else {
119         //store reference to current link
120         previous = current;
121         //move to next link
122         current = current->next;
123      }
124   }
125
126   //found a match, update the link
127   if(current == head) {
128      //change first to point to next link
129      head = head->next;
130   } else {
131      //bypass the current link
132      previous->next = current->next;
133   }    
134	
135   return current;
136}
137
138void sort() {
139
140   int i, j, k, tempKey, tempData;
141   struct node *current;
142   struct node *next;
143	
144   int size = length();
145   k = size ;
146	
147   for ( i = 0 ; i < size - 1 ; i++, k-- ) {
148      current = head;
149      next = head->next;
150		
151      for ( j = 1 ; j < k ; j++ ) {   
152
153         if ( current->data > next->data ) {
154            tempData = current->data;
155            current->data = next->data;
156            next->data = tempData;
157
158            tempKey = current->key;
159            current->key = next->key;
160            next->key = tempKey;
161         }
162			
163         current = current->next;
164         next = next->next;
165      }
166   }   
167}
168
169void reverse(struct node** head_ref) {
170   struct node* prev   = NULL;
171   struct node* current = *head_ref;
172   struct node* next;
173	
174   while (current != NULL) {
175      next  = current->next;
176      current->next = prev;   
177      prev = current;
178      current = next;
179   }
180	
181   *head_ref = prev;
182}
183
184void main() {
185   insertFirst(1,10);
186   insertFirst(2,20);
187   insertFirst(3,30);
188   insertFirst(4,1);
189   insertFirst(5,40);
190   insertFirst(6,56); 
191
192   printf("Original List: "); 
193	
194   //print list
195   printList();
196
197   while(!isEmpty()) {            
198      struct node *temp = deleteFirst();
199      printf("\nDeleted value:");
200      printf("(%d,%d) ",temp->key,temp->data);
201   }  
202	
203   printf("\nList after deleting all items: ");
204   printList();
205   insertFirst(1,10);
206   insertFirst(2,20);
207   insertFirst(3,30);
208   insertFirst(4,1);
209   insertFirst(5,40);
210   insertFirst(6,56);
211   
212   printf("\nRestored List: ");
213   printList();
214   printf("\n");  
215
216   struct node *foundLink = find(4);
217	
218   if(foundLink != NULL) {
219      printf("Element found: ");
220      printf("(%d,%d) ",foundLink->key,foundLink->data);
221      printf("\n");  
222   } else {
223      printf("Element not found.");
224   }
225
226   delete(4);
227   printf("List after deleting an item: ");
228   printList();
229   printf("\n");
230   foundLink = find(4);
231	
232   if(foundLink != NULL) {
233      printf("Element found: ");
234      printf("(%d,%d) ",foundLink->key,foundLink->data);
235      printf("\n");
236   } else {
237      printf("Element not found.");
238   }
239	
240   printf("\n");
241   sort();
242	
243   printf("List after sorting the data: ");
244   printList();
245	
246   reverse(&head);
247   printf("\nList after reversing the data: ");
248   printList();
249}
Ariana
21 Jan 2020
1// Node of the list
2typedef struct node {
3    int val;
4    struct node * next;
5} node_t;
6
queries leading to this page
c listlinked list in c program with all operationslinking linked lists in clist in c programmingwrite a c program to implement singly linked listinput into linked list ccreating linked lists in chow to display the single linked list in cwhat does the linkedlist 2a list refer to cwhats c listcreate a doubly linked list in ceasy linked list in clists in c 27how to form a linked list using structhow to create list in link list in cpointer node 3enext clinked list of linked lists cstructure node in cc code for creation of linked listlinked list basic operations in cc listslinkted list in cbasic operations in linked list in cc stl linked listlinked lists in clinked list in c codenodes in clinked list in example c examplelinked list used in clinked list operations in ceverything about linked list in ccreate node in clinked list codeslists in ccreating linked list in cbasic linked list program in clist 2a list cc 23 listlinked list types in clist en chow to create lihnked list in chow to collect linked list pounter address code for linked listlinked list c librarylinked list in c programmingunderstanding linked list tutorial in c for beginnersexplain the code of linked list in clinked list programs in cc linked list all functionshow is a list node created in clist c programminglist function chow to display data of link listc program code for linked listhow to instantiate a linked list in chow to make a list in c codemake and display a linked list in ccreation of a linked list in ccreate a node cc list of list of intimplementung link list in clink list clinked list of nodeshow to create a linked listc linked list explainedhow to define a list in c list cc language listsingle link list nodec create and use linked listlinked list in c 5b 5bhow to use list in struct in cprogram for linked list operations in cc linked list create nodewhat are linked lists used for in chow to create and use a linked list in ccreate a simple linked list in cimplementation of singly linked list using array in cllinked lists clinked list in c contentselements in a list cwrite a c program for singly linked list perform operations create sllhow to make a linked list cc struct for linked listhow to make a linked liststruct with a linked list ctake input and dispay linked listbasic operations linked list cwrite a pogram 28wap 29 to create the linked list of n nodes linked listr csingly linked list application example in cc list structcreate a linked list cdisplay a linked list in cimplementation of linked list in clist chain in clinked list syntax in clinkedi list strct cdefine a linked list clinked list simple program in chow to create linked list in cnode csingly linked list example in chow to build a linked list cstruct head linked list tutohow to learn linked list in c easilyc program linked list operationsc singly linked listhow to linked list in clinked list tutorial cc programming linked listuse linked list in linked list cc linked listhow to initialize a linked listc code linked listimplementing linkedlist in clinked lists c examplesc code linked listslinked list using cimplementation of linkeed listsc program how to create a link listlist in chow to create a linked list in c with n nodescreating a linked listlinked list in c program codec pionter listhow to create a node in linked listlinkedlist function clinear linke dlist in cuser defined linked list in clinked list implement in cnode list in chow to create a linked list in c using functionexample of singly linked list in clinked list c 2b 2b 27struct node in cimplementing linked list in chow to make a list in c 23 23initialize linked list c functionwhat are linked lists in c and how to use themlinked list iun clinked list c examplescreate a linked list in cc object listhow to create new node in linked list cc linked listylinked list in cadd linked list cdoes c have listsprogram to create and display a singly linked list in chow to create a list on chow to implement a linked list in clsit ccreating linkedlist in chow to node in c programmingcreate simple linked list in cwhat is linked list in chow to create a linked list with numbers in it c programlinked list programsuse linked lists clink list titorial clinked list in c 5clinked list in cstack adt using linked list in ccreating and initializing linked list cprogram which uses linked list in cc program to create a linked listwhat is linked lists in clinked list in c examplelifo linked list clinkedlist implementatino clinked lists c viduallist h in c to create linked listimplement linked list in ca simple program of linked list in cc program singly linked listwrite a program to create a linked list in chow to do a list in ccreate singly linked list in cc program to display linked liststruct node clinked list using node struct and list struct in clinked list using array in c 22embedded c linked list 22c c linked listlinked list in cxc list forcreate linked list function in clinked list operations implementation chow to use list in cfunction to create and display linked listlinked list all operations in clinekd list ccreate linked list clink list in cmaking linked list in cwhat is linkedlist in cc singly linked list exampledefine a list in cc linked list example codehow to implement linked list in clistas chow to define list in csyntax of linked list in cc linked list with a linked list structurelinkedlist cwhat is singly linked list in chow to implent linked lis in clisted list cc link list liblinked list functionslinked list in c using pointers youtcreate a singly linked list in c cheeglinked list of a linked list in ccreate linked list in clinked list in c data structurelinked list declaration in chow to creat linked list csingly linked list program in data structure using clearn linked lists in clinedlist clinked list with pointerspointer and linked kist in chow to create linked list clinked list functions in csimple linked list program in cwap to display the by listwhat is list in ccan you create a linked list in clinked list syntax clinkedlist in cc node tlinked list using struct in chow to code a linked list in cmake a list cprogram on linked list in ccreation of singly linked list in ccreating a linked list in cc nodelinkedlists in cuse of a linked list in cprogram to create a linked list in cbuild a linked list in csingly linked list in ccreate a linked list node in cliunked list in c syntaxlinked list in c with all the operationsc create linked listlist in c 24c language listsdoubly linked list in cdeclare a new struct node in ccreate a node list clinked list i clink list implementation in clinked list c deflinklist clinked list using node and list struct clinked list example code in chow to create a linked list in chow to make linkes lise cstruct linked list 2anext 3bcreating a singly linked list in cc why use a linked listhow to use lists in chow to make linked list chow to make linked lists in cft push back linked list c 42create linked listc program for a linked listc listac c2 a3 listlinked lists of linked lists clinked list code in chow to make a list cdeclaring a linked list 3ahow do u make a linked list in cprograms on linkedlist in clinked list creationcreating a linked list struct cwhat is a list in chow to create a list in c programmingimplement a linked list using c displaying a linked list in cc linked list how to uselinked lists c implementationhow to access linked list in cstructures and linked lists in clinked list applications in cc linked lists tutorialwrite a program 28wap 29 to create the linked list of n nodes c listsingly linked list program using caccessing linked list with 5b 5d in cc what are linked lists used forhow to read a llist in cnpode in clist in a list cc programming in the linked listhow to create a linked list cstore data from limked list in cmake a list in chow to implement a linked list cc how to make a linked listc language contribution on creating nodehow linked list is implemented in cwrite a program for singly linked listlink list c implementlinked list creation in cc linked list tutorialhow to use list in c programmingdynamic lists in cliked list in chow to make a linked list in clist c librarylist clinked list implementation clists in chow to make a list c 7e linked list clinked lists explained in clinkedlist with csimple program that uses linked list in cc linked list notationcreate linked list c programc linked list examplelinked list in c using pointerslinked list c implementationall operations on linked list in chow to create a node in linked list cexemple linked list in clinked list in data structure csingly linked list in c programcreate a singly linked list in chow to define node in clinked list program in c with algorithmlinked list implementationc a list of listssingly linked list program in c with explanationcreate a linked list node using structures in clinkend list cgo through linked list ca linked list in cllinked list in ccan you create a list in chow to make linked list in clinked list example in clink list c inplementationhow to display a linked list ccreat a generic list using a linked list in clinked list standard libary cc lists tutoriallinked list tutorial in chow to creat a globally linked list in clinked list implementation in clinked list in c implementationdoubly linked list program in chow to do a linked list in cc linkedlistcreate list no node cc list linked list c programlinked lists functions cwrite a code for creation of linked listwhat is a linked list in ccreate a linked listc list librarydisplay elements in linked list cwhere is linked list created in cimplement list in clinked list creation program in chow to create a node in cstore data linked list clinked list c codebasic operations in singly linked list in cc library listlink list c implementationhow to create a linked list with struct in c languagelista in csingly linked listcreate a linked list in c step by stepis struct is linked listlinked list codec programming liststructure linked list in cc nodescreating a node in linked list in cc creating a linked listlinked list create node in clinked list of linked lists in clist method in cc list structurehow to parse a linked list in cnode struct cc list wihin listlist c 24linked list in c programprogram in linked list in c with all operationsc linked list operationsc program to implement all the list operations using linked listsdynamic linked list in c codec 24 listlista cstruct list 2a next 3bbasic singly linked list operations in clists cmake linked listhow to use a linked list in cdefine list in csyntax in node to create a node in the singly linked listlinklist in cc program to creating a single linked list with n elements c linked list with structc progra how to do linked listlinked lists cc linked list implementationimplement linkedlist in cc start new linked listc programmming list nodecode to create linked list in cc language basic linked listc list examplehow to create a linked list for any data typelinked ilist in cfull linked list program in ccode for linked list in care there linked lists in clinked list add in csingly linked list program in csingly linked list easy example in cc how to make a listlinkediliist in cinked list in clinkedlist all fucntion in cnode c programmingimplement a linked list in cc listecreate node cdata structure linked list codelistnode documentation csingly linked list operations in clinked list and structure incis there list in cstruct first linked list tutohow to create node in linked list in ccreate linked list next prev cwhat is link list in c linked list in clinked list c 2b 2b using structssingly linked list program display in clinked list algorithm in cprogram in c for singly linked listlinked list node in cis linkedlist is singly linked listlist syntax in clinked lists codelinked list program user input in cc list of listswhat are the ways to implement linked list in csimple linked list in cc program linked list with structhow to make a linked list of structures clinked structures in clinked list structures in cc create a listhow to create node in cwhy do we use linked list in clist cc list of listcode for displaying nodes of linked list list of c programming librariesc programming listsc program to create and display singly linked listcreate a linked list with all operations in clinked list datastructure in chow to implement a linked list in c languagelink list of struct ccreate a linked list of n nodes in chow to create singly linked list in clinked list create node csingly linked list simple program in chow to display list in chow linked list works in cread name in linked list in clist c 5bc list 3ec general purpose linked listhow to create head node in linked listc linked list and examplesc code for linked listc linked list classwhat a chained list clist of list in cwhat is the use of linked list program in chow to make a lsit in cc linked list libraryimplementation linked list in c singly linked list csingly linked list display in ccreate list of pointers of linked list clinked list in c librarylist up from list in chow to do list in clist funciont chow to create linked listc program to implement a singly linked listhow to create a list in cusing linked listshow to use a list in ccreate a linked list of 5 elements in clinked lists c programmingstruct list node cworking with linked list in clinked list on clinked lists implementation in cwhat are linked lists in chow to program a dubbel linked listlinked lists and how to use them cc program to linked listlinked list display program in cnode 2a in ccreating singly linked list in clist pointerlist in cdisplay linked list in cc stractures and listslinked list display in clinked list in c using structurelinkedlist creation c 23 5dmaking a linked list in cexporting linked list in csingly linked list c with structs examplewhy are linked lists used in cwhat does 2a do in linked list in clinked lsit in clinked list programc listlinked list struct chow linked list workd in cexample for linked list in ccreate a list in ccreat link listsingly linked list implementation in csingly linked list code in cmake linked list in clink list programc linked listslist struct cuses of linked lists in ccreate a list cc 25 listlinked list source ccreation of linked list in cimplementation of linked list in c programc program linked list addresswhy pointer node variable is required in dynamic linked listlinked list with embedded ccreateing a linked list in cc lang list nextrhow give list of data to a ink list in chow to define a linked list in clinked list in c with functionslinked list c programminghow to add a node to a linked list in clist structure in clinked list in c using functioncreate new node in linked list in cwhat the different list and linkedlist in c c2 a3c lists witch functionsmake linked list in c appendinc how to implement linked list in cnode csingly linked lists in cusing lists in cdisplay function in c to linked liststruct linked list def in ccreating a node in linked list clinked list c languagelinkedlist explaination in csingly linked list in c example programcreate a node in linked list in cwhat is a node in chow to allocate a node in clinked list using node struct and list structcreate and insert and display to perform singly linked list in ccreate linked list in c loopstruct c linked listhow to make a list in cusing structure data type for linked list implementationhow to build linked list cgeeksforgeeks linked list cfunction to create a linked list in clinked list program in cc linked listc list implementationc program linked listlinear linked list in cimplementation of link listcreating a list in clinked list in c example codecreating a linked list in c with explanationc linkedlist methodswrite a c program to create and display singly linked listcreate a linked list in c algorithm for singlylinked list class clinked list node c 23 24c node structlinked list in example clist in c headlinked list cwrite a program to implement singly linked list in clinked list c intlinkedlist creation c 23 5clinked list node cc linked list programdisplay linked list clinked list cnode in ccode foe creating a linked list and diaplayeasy way to learn linked list in ccreate list in clinked list i n clinked list example clinked list implementation in c codec program linked list implementationstoring empty linked list in cwrite a program in c to create and display singly linked listinitliaze linkedlist c functionlist c 7bhow to make a linked list in c