build a linked list in c

Solutions on MaxInterview for build a linked list in c by the best coders in the world

showing results for - "build a linked list in c"
Franco
28 Apr 2017
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
Jakob
23 Nov 2019
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}
Christian
13 Sep 2019
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}
Giulia
22 Feb 2018
1node_t *create(int nodes)
2{
3	// code that builds the list
4}
queries leading to this page
how to implement a linked list cuses of linked lists in chow to instantiate a linked list in clinked list example code in chow to create a linked list chow to create a linked list in c with n nodesdefine a linked list clinked lists c examplesmake a list in cexemple linked list in ccode for displaying nodes of linked list creating a node in linked list in chow to create a node in linked list in clinked list in example cllinked lists cprogram on linked list in ccode foe creating a linked list and diaplayhow to learn linked list in c easilyhow to create a node in linked list clinked list in example c examplelist 2a list cstruct with a linked list csingly linked list program in data structure using clinked list create node in cc code for creation of linked liststructure node in clinkedlist in clinked list node chow to make linked list clist cwhat is singly linked list in chow to make linked lists in clist in c 24linked list standard libary chow to create a list on csingle link list nodewhat is a node in cc stractures and listscreate linked list in c loopc linked list notationc linked list with structsingly linked list code in cc listswhat is a list in cimplement a linked list in chow to create node in linked list in clinked list programslink list c inplementationhow to make a linked listhow to build a linked list cc program for a linked listbasic operations in singly linked list in cprogram in linked list in c with all operationslinked list types in csingly linked list program in cc program linked list operationslinked list simple program in chow to linked list in clist chain in cprogram which uses linked list in cimplementation of linkeed listslinked list using array in clinked list on cmake and display a linked list in clinked lists explained in chow to implement linked list in cc programmming list nodelinked list implementation in clinked list class cprograms on linkedlist in ccreate a linked list of 5 elements in clinked lists clinked list source cc linked list example codewrite a c program to create and display singly linked listlinked list in data structure clinked list node in cwrite a program 28wap 29 to create the linked list of n nodes linked list operations in clinked list add in ca linked list in csingly linked list in c programcreate a simple linked list in chow to implement a linked list in c languagesingly linked lists in chow to creat linked list cmaking a linked list in csingly linked list program in c with explanationlinkediliist in chow to use lists in cc program to linked listinput into linked list cwhat is linkedlist in cnode c create a linked list node in cc linked listhow to create a linked list in c using functionhow to create linked listcreate linked listuser defined linked list in ccreate a node clinked list functionslearn linked lists in ccreation of singly linked list in csingly linked list in cc struct for linked listwrite a code for creation of linked listlinked list implementation cliunked list in c syntaxhow to display the single linked list in clinked list in c codeliked list in clist struct clinked list declaration in chow to create a linked list in ccreate linked list c programcreate list of pointers of linked list cc program code for linked listlinked list in c program with all operationslinked list tutorial ccreate a linked list of n nodes in chow to make a list chow to program a dubbel linked listlink list c implementationeasy way to learn linked list in csingly linked list display in csingly linked list program display in csingly linked list c with structs examplelinked list create node cdata structure linked list codewhy pointer node variable is required in dynamic linked listlinked list in c examplelist pointermake linked list in clinked list in c data structurecreate a linked list in c step by stephow to make a list c 7ecreating linked lists in cexample for linked list in clinked list functions in csyntax in node to create a node in the singly linked listlists csimple linked list program in cwrite a program in c to create and display singly linked listc linked list tutoriallinked list iun clinked list in cc nodec singly linked listc node tlist clinked list tutorial in clinked list in c program codesingly linked list example in cc linkedlistnode c programmingcreate a linked listc program to create and display singly linked listc programming listswrite a program for singly linked listlinked lists c programmingbasic operations linked list clinked list in c 5b 5bc lists witch functionshow to create and use a linked list in cc programming in the linked liststruct head linked list tutocreate a linked list in c algorithm for singlyis struct is linked listlist method in clinked list in c with all the operationscreate a singly linked list in c cheegc program to create a linked listexporting linked list in chow to parse a linked list in chow to create linked list cdefine list in cimplementation of link liststruct list 2a next 3blinkedlist c create singly linked list in cwrite a c program for singly linked list perform operations create sllimplementing linkedlist in ccreate a linked list in cwhat is list in cc linked list with a linked list structurehow to create lihnked list in cc program singly linked listcreate a node in linked list in cdisplaying a linked list in clinking linked lists in chow to use a list in clink list implementation in cnode 2a in cc language contribution on creating nodedisplaying linked list in cmake linked listhow to implement a linked list in clist c programminglist funciont chow to use list in struct in cdynamic lists in ccreate node ccreating a linked list in clinked list program in cusing linked listshow to display list in chow to create a list in cimplement a linked list using c singly linked list ccode to create linked list in ccreation of linked list in cc how to make a linked listcan you create a list in cfunction to create a linked list in csingly linked list implementation in chow to form a linked list using structcreate list in cprogram in c for singly linked listlistnode documentation clists in cc what are linked lists used forc list structimplement linked list in ccreating and initializing linked list ccreate and insert and display to perform singly linked list in clinked list all operations in clinkedlist implementatino cc listhow to use a linked list in cc program linked list addresswhere is linked list created in ccreating a node in linked list chow to create a list in c programmingcreate linked list function in clinked list creation in chow to allocate a node in cc 23 listhow to create a node in cc lists tutorialhow linked list is implemented in clinkedlist creation c 23 5dbasic operations in linked list in cwhat a chained list cstoring empty linked list in cwap to display the by listc program to display linked listhow to display data of link listcreation of a linked list in cc linked lists tutorialcreate a singly linked list in clist function clinked list cuse linked list in linked list clinked list in c programminglinked list datastructure in clink list titorial cc linked listystruct node clink list of struct cusing structure data type for linked list implementationfunction to create and display linked liststructures and linked lists in cc linked list and exampleslist in c programminglinked list implement in chow to create list in link list in cprogram to create a linked list in clinkted list in clinked list of linked lists in cc program linked list implementationlinked list in c using structurelinked list syntax in cc programming linked listsingly linked list simple program in ccreating a linked list struct clinked list c exampleshow to create singly linked list in clinked list of nodesc lang list nextrc language basic linked listlinked lists and how to use them clinked list creationlinked list with embedded cuse of a linked list in csingly linked list program using clinked list in c using pointersstruct c linked listhow to create a linked list with numbers in it c programlinked list operations implementation clinked list in c 5clinked list codelinked list c codedisplay linked list in clinkedlist function clinked ilist in caccessing linked list with 5b 5d in cuse linked lists cworking with linked list in clinked list applications in csingly linked list easy example in clinked lists implementation in clinked list programadd linked list cc c linked listmake a list cdoubly linked list program in cprogram to create and display a singly linked list in cdefine a list in clinked list used in cgeeksforgeeks linked list chow to do a linked list in cc linked listshow to create a linked listlinked list display program in cstruct first linked list tutowhat is the use of linked list program in ceasy linked list in cc linked list create nodelinear linke dlist in cpointer and linked kist in cstack adt using linked list in clinked list c 2b 2b using structslinked list c languagec linked list implementationlinked list in ccreate node in clinkedlist creation c 23 5clinked list struct clinked list node c 23 24c linked list examplemaking linked list in csingly linked list in c example programgo through linked list cc why use a linked listhow to make a lsit in clinked list display in cdisplay elements in linked list clinked lsit in cnode list in cstore data from limked list in ccode for linked listlink list programlinked list c programcreate a node list cdoubly linked list in clsit chow is a list node created in chow to create new node in linked list cwrite a pogram 28wap 29 to create the linked list of n nodes declare a new struct node in cdisplay a linked list in ccreate linked list ccreating a linked list in c with explanationlinkend list chow to make linkes lise cc node structlinkedlists in ccreate linked list in ccreating a singly linked list in cc program linked listlink list clinked list using node and list struct chow to add a node to a linked list in cdisplay function in c to linked listwhat are linked lists used for in cinitliaze linkedlist c functionlinked list example chow to define node in ccreating linked list in clinked list in c with functionshow to create node in clinked lists codellinked list in clinked list structures in clinked list using struct in cimplementation of linked list in c programc linked list librarywhat the different list and linkedlist in c c2 a3structure linked list in clinked list algorithm in cwhat does the linkedlist 2a list refer to clinked list of linked lists cimplementation of linked list in cmake linked list in c appendc nodeslinked lists in clinked list using cwhy do we use linked list in ccan you create a linked list in clinked list in c implementationlinked list code in ctake input and dispay linked listc creating a linked listlinkedlist with csingly linked list application example in ccreate a list in cc program to creating a single linked list with n elements linked structures in cnode in cwhat does 2a do in linked list in ccreate and display linked list in chow to creat a globally linked list in ccreating a linked listnodes in cread name in linked list in cc program how to create a link listlinked lists c viduallinked list c defc linked list explainedare there linked lists in chow to initialize a linked listlinked list using node struct and list struct in chow give list of data to a ink list in cwrite a c program to implement singly linked listlisted list chow to make a list in c 23 23linked list in c example codec create and use linked listlink list in cc create linked listexample of singly linked list in cwrite a program to implement singly linked list in cstruct list node chow to make linked list in cwhy are linked lists used in call operations on linked list in clinked list basic operations in cc progra how to do linked listdisplay linked list clinked list and structure incc program to implement a singly linked listsyntax of linked list in cbasic linked list program in cimplementing linked list in cimplementation of singly linked list using array in cc program to implement all the list operations using linked listsc program linked list with structdynamic linked list in c codecreating a list in cc linked listhow to define a list in chow to code a linked list in cstruct linked list 2anext 3bsimple program that uses linked list in cwhat are the ways to implement linked list in chow to make a linked list of structures chow to collect linked list pounter address build a linked list in c linked list ccreate a list cc start new linked listlinked list example in chow to make a list in c codelinked list i n ccreate new node in linked list in cimplementation linked list in c linked list program in c with algorithmcreat link listlinkedlist explaination in chow to create head node in linked listlinked list in c programhow do u make a linked list in clist up from list in clinear linked list in clinked list c programminghow linked list workd in cwhat is linked list in cpointer node 3enext cimplementung link list in clinked list implementationnode struct cimplement linkedlist in ccreate a linked list cc linked list programlist h in c to create linked listlinked list implementation in c codehow to create a linked list with struct in c languagecreate a linked list with all operations in clinked list in c using pointers youtcreate list no node cexplain the code of linked list in clinked list with pointersunderstanding linked list tutorial in c for beginnerslist in cc code for linked listsingly linked list operations in clinked list programs in cwrite a program to create a linked list in ccreat a generic list using a linked list in clinked list in c librarycreate linked list next prev ccode for linked list in chow to display a linked list clinekd list ccreate simple linked list in cc singly linked list examplefull linked list program in csingly linked listlinked list i clinked list of a linked list in cwhat is linked lists in cc how to make a listhow to implent linked lis in cc linked list how to usesimple linked list in ccreate a doubly linked list in clist in c headhow to create a node in linked listlinked list in c using functionc linkedlist methodsft push back linked list c 42elements in a list cstore data linked list cwhat are linked lists in clinked list singly for ca simple program of linked list in clinked lists of linked lists chow to create linked list in ccreate a linked list node using structures in cstruct node in clinked listr cc pionter listc general purpose linked listlinked list codeslinked list syntax ceverything about linked list in chow to make a linked list in c linked list in ccreateing a linked list in chow to make a list in chow to create a linked list for any data typelinked list c libraryinitialize linked list c functionimplement list in cis linkedlist is singly linked listcreating linkedlist in cdeclaring a linked list 3alinked list in c contentshow to build linked list cinked list in clinked lists c implementationwhat is a linked list in clinked list c intc list implementationhow to access linked list in cbasic singly linked list operations in clinkedlist all fucntion in chow to create a linked list in c 3d 3dlinked list clinked list program user input in cnode cinc how to implement linked list in cc create a listcreating singly linked list in clinked list c implementationlinked list creation program in chow linked list works in cbuild a linked list in c