c linked list

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

showing results for - "c linked list"
Braylon
09 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}
Davide
01 Mar 2020
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}
Alina
31 Feb 2018
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}
Alban
10 May 2016
1// Node of the list
2typedef struct node {
3    int val;
4    struct node * next;
5} node_t;
6
Michelle
19 Jul 2017
1#include <stdio.h>
2#include <stdlib.h>
3
4// Represents a node
5typedef struct node
6{
7    int number;
8    struct node *next;
9}
10node;
11
12int main(void)
13{
14    // List of size 0. We initialize the value to NULL explicitly, so there's
15    // no garbage value for our list variable
16    node *list = NULL;
17
18    // Allocate memory for a node, n
19    node *n = malloc(sizeof(node));
20    if (n == NULL)
21    {
22        return 1;
23    }
24
25    // Set the value and pointer in our node
26    n->number = 1;
27    n->next = NULL;
28
29    // Add node n by pointing list to it, since we only have one node so far
30    list = n;
31
32    // Allocate memory for another node, and we can reuse our variable n to
33    // point to it, since list points to the first node already
34    n = malloc(sizeof(node));
35    if (n == NULL)
36    {
37        free(list);
38        return 1;
39    }
40
41    // Set the values in our new node
42    n->number = 2;
43    n->next = NULL;
44
45    // Update the pointer in our first node to point to the second node
46    list->next = n;
47
48    // Allocate memory for a third node
49    n = malloc(sizeof(node));
50    if (n == NULL)
51    {
52        // Free both of our other nodes
53        free(list->next);
54        free(list);
55        return 1;
56    }
57    n->number = 3;
58    n->next = NULL;
59
60    // Follow the next pointer of the list to the second node, and update
61    // the next pointer there to point to n
62    list->next->next = n;
63
64    // Print list using a loop, by using a temporary variable, tmp, to point
65    // to list, the first node. Then, every time we go over the loop, we use
66    // tmp = tmp->next to update our temporary pointer to the next node. We
67    // keep going as long as tmp points to somewhere, stopping when we get to
68    // the last node and tmp->next is null.
69    for (node *tmp = list; tmp != NULL; tmp = tmp->next)
70    {
71        printf("%i\n", tmp->number);
72    }
73
74    // Free list, by using a while loop and a temporary variable to point
75    // to the next node before freeing the current one
76    while (list != NULL)
77    {
78        // We point to the next node first
79        node *tmp = list->next;
80        // Then, we can free the first node
81        free(list);
82        // Now we can set the list to point to the next node
83        list = tmp;
84        // If list is null, when there are no nodes left, our while loop will stop
85    }
86}
87
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 ccreating a node in linked list in clinked list in example cllinked lists cprogram on linked list in chow to learn linked list in c easilyhow to create a node in linked list clinked list in example c examplenode clist 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 define a linked list in chow to make linked lists in clist in c 24lists in c 27linked list standard libary cc stl linked liststruct linked list def in 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 cwhat 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 clinklist cc program linked list operationslinked list simple program in chow to linked list in cc programming listlist chain in cprogram which uses linked list in cimplementation of linkeed listshow to read a llist in clinked 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 nodec list forlinked 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 clinked 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 clist en 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 listwhat is linkedlist in ccreate 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 clink list c implementlinked list functionslearn linked lists in ccreation of singly linked list in csingly linked list in cc list libraryc struct for linked listwrite a code for creation of linked listlinked list implementation cliunked list in c syntaxlinked list in c codeliked list in clist struct clinked list declaration in chow to create a linked list in clist structure in ccreate 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 implementationc 25 listc list of listeasy way to learn linked list 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 cc language listlinked list in c data structurecreate a linked list in c step by stephow to make a list c 7ecreating linked lists in cc a list of listsc listaexample for linked list in clinked list functions in cc linked list classsyntax 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 link list libc singly linked listc node tlist clinked list tutorial in clinked list in c program codesingly linked list example in cc linkedlistc linked list all functionsnode c programminglist c librarycreate a linked listc program to create and display singly linked listc programming listsusing lists in clinked 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 tutoc c2 a3 listcreate a linked list in c algorithm for singlylinklist in cis 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 3bcreate singly linked list in cinitalising linked lists in cimplementing linkedlist in ccreate a linked list in cwhat is list in cc linked list with a linked list structurewhat are linked lists in c and how to use themc 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 cc list of listsnode 2a in cmake linked listhow to implement a linked list in clist c programminglista in clist funciont c list chow to use list in struct in cdynamic lists in ccreate node ccreating a linked list in clinked list program in clist syntax in cusing linked listshow 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 clinked list c 2b 2b 27c 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 clinkedlist clistnode documentation clists in cc what are linked lists used forc list structc list 3eimplement linked list in clists 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 ccreate linked list function in clinked list creation in chow to allocate a node in cc 23 listc object listlist c 24how 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 ccreation of a linked list in cnpode in cc linked lists tutorialcreate a singly linked list in clist function clinked list cuse linked list in linked list cis there list in 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 implementationstructures 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 clista 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 5chow to define list in clinked 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 cc list singly 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 liststruct first linked list tutowhat is the use of linked list program in ceasy linked list in cc linked list create nodec listslinear linke dlist in cpointer and linked kist in cstack adt using linked list in clist c 5blinked 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 cc 24 listlinked list display in cc listelinked 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 cdeclare a new struct node in clinked list using node struct and list structcreate linked list ccreating a linked list in c with explanationlinkend list chow to make linkes lise clinkedi list strct clistas cc list structurelinkedlists in cc node structc language listscreate 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 functionslinked lists codellinked list in clinked list structures in clinked list using struct in clinked list in cximplementation of linked list in c programc linked list librarywhat the different list and linkedlist in c c2 a3linedlist cstructure linked list in clinked list algorithm in chow to use list in cc listlist of list in cwhat does the linkedlist 2a list refer to clinked list of linked lists cinitialising linked lists in c implementation of linked list in cmake linked list in c appendc nodeslinked lists in clinked list using chow to node in c programmingwhy do we use linked list in ccan you create a linked list in clinked list in c implementationc code linked listslinked list code in cc creating a linked listlinkedlist with csingly linked list application example in ccreate a list in clinked structures in cnode in cwhat does 2a do in linked list in clinked lists functions chow to creat a globally linked list in ccreating a linked listnodes 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 clist of c programming librariesstruct list node chow to make linked list in clist c 7bwhy 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 listhow to do list in csyntax 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 listwhats c listhow to define a list in chow to code a linked list in chow to do a 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 listdoes c have listslist in clinked 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 algorithmc code linked listcreat 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 pointersc listunderstanding 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 clinekd list ccreate simple linked list in c 22embedded c linked list 22c library listc singly linked list examplefull linked list program in csingly linked listprogram for linked list operations in clinked 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 a list 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 cwhat is link list in 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 ccreateing a linked list in c linked list in cc list wihin listhow to use list in c programminghow to make a list in cc list of list of intc linked list operationshow to create a linked list for any data typelinked list c libraryinitialize linked list c functionimplement list in cc list exampleis 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 chow to make a linked list cc list implementationlinked list c inthow to access linked list in cbasic singly linked list operations in clifo linked list clinkedlist all fucntion in clinked list 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 cc linked list