linked list program in c

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

showing results for - "linked list program in c"
Faisal
28 Jan 2020
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}
Wyatt
05 Feb 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}
Juan Martín
17 Aug 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}
Janice
12 Sep 2017
1// Node of the list
2typedef struct node {
3    int val;
4    struct node * next;
5} node_t;
6
Iker
04 Oct 2016
1node_t *create(int nodes)
2{
3	// code that builds the list
4}
queries leading to this page
creating a node in linked list in cllinked list in ccreate a linked list node in cc linked list example codecreating a singly linked list in ceverything about linked list in cmaking a linked list in cdeclaring a linked list 3acreate linked listc linked list operationsimplement list in csingly linked list example in chow to code a linked list in clinked list of nodeslinked list in c using structurec create and use linked listcode to create linked list in cstruct with a linked list chow to make linkes lise clinked lists and how to use them cc object listc 25 listmake and display a linked list in clinking linked lists in cc program for a linked listc list librarydefine a linked list clinked list codelinked list of linked lists cliked list in clinked list in c with functionssingly linked list program in c with explanationc language basic linked listlinked lists c programmingexplain the code of linked list in clinked list datastructure in chow to define a list in ccreate node in cc stl linked listlinked list c deflinked list standard libary clinked list creation in cbasic singly linked list operations in ccreation of singly linked list in chow to add a node to a linked list in ccreating a linked list in clist in c 24linked list in c using pointersc start new linked listlist ccreate a linked list in c step by stepcreate a node list chow to do a list in clinked list creationc language listcreating singly linked list in cc linked listcreate linked list chow to define a linked list in cc pionter listlinked list operations implementation cc linked lists tutoriallinked list example code in clinked lists of linked lists clinked list simple program in clists in c 27linked list in c program codec list of listlinked list structures in cfull linked list program in cc list forstruct head linked list tutocreate a linked list of 5 elements in clinked list example cmake linked list in clist up from list in cprogram in linked list in c with all operationslinedlist ccreate list no node ccreate a singly linked list in c cheeglink list c implementhow to create a linked list with struct in c languagelinked list display in chow to create a node in linked listcreate a linked list in cc why use a linked listlinked list source cc linked list librarywhat a chained list clinked list in c examplehow is a list node created in cstruct list node clinked list programs in clist c 24c stractures and listslist structure in clinked list program in chow to use list in struct in chow to do list in cc library listlinklist csingly linked list simple program in csimple program that uses linked list in cwhat are the ways to implement linked list in clinked lists implementation in clinked list using node struct and list struct in clinked list operations in clinked list creation program in cc program linked list implementationsingly linked list code in ccreate a linked list clinked list program in c with algorithmcreating a linked listinitliaze linkedlist c functionwrite a code for creation of linked listlinked list in c data structurehow to access linked list in cc program singly linked listhow to implement a linked list clinked list class chow to make linked lists in clinkedlists in chow to create a linked list in c with n nodeslinkedlist implementatino clinked list in c program with all operationslinked list algorithm in chow to create a node in clinked list in c libraryc list of listsinitialize linked list c functionhow to create a linked list with numbers in it c programc create a listwrite a c program to create and display singly linked liststruct first linked list tutolinkedlist creation c 23 5dlinked list create node cinc how to implement linked list in clist syntax in cc code for creation of linked listwhat does the linkedlist 2a list refer to cimplementation of link listlist pointerc list wihin listc lists witch functionsstructure node in cdefine list in cc program linked list with structimplementing linkedlist in ccreating a node in linked list cc listswhat are linked lists in cc linked list programc linked list examplelinked list in c using pointers youtexporting linked list in clist c 7blist in c headis there list in chow to make a list in clinked list clinklist in chow to make a linked list of structures cc node tc linked list create nodecreate list in clink list implementation in cc singly linked listhow to implement a linked list in clinked list c 2b 2b using structsc node structlinked lists cwhat is a node in clist in a list clinked list c libraryhow to use list in c programmingc what are linked lists used forlinked list in c 5b 5busing structure data type for linked list implementationc linkedlist methodsc nodesprogram on linked list in clinked list clisted list cwhat is linked list in cc linked listsc code linked listc language listshow to use lists in chow to make a list c 7elink list of struct cc how to make a listlinked list using cbasic operations in singly linked list in cc lists tutorialc code linked listswhat the different list and linkedlist in c c2 a3c linked liststack adt using linked list in chow to make a linked list in clinked list iun clist 2a list ccreateing a linked list in cstruct node in clinked lists c vidualdisplay linked list in cis struct is linked listhow to parse a linked list in chow to make linked list clinkedlist explaination in chow to initialize a linked listlinked list using node and list struct cdeclare a new struct node in chow to create and use a linked list in cmaking linked list in cmake a list in cwhat is list in cc linked list and examplesc nodelinked list c intlinked list in c programmingcreate new node in linked list in chow to define node in ccreating linked list in clistnode documentation chow to form a linked list using structlinked list c codemake linked list in c appendlinked lists functions clist cwhat does 2a do in linked list in cdisplaying a linked list in cc linked list explainedft push back linked list c 42c linked list how to usenode struct ccreate a doubly linked list in c 22embedded c linked list 22write a program to create a linked list in clearn linked lists in cc list examplehow to create list in link list in csingly linked listc linked list classnode clinked list syntax in cc linked list all functionslinkedi list strct clinked list used in cnpode in cwhat is a linked list in cllinked lists clifo linked list clinkedlist creation c 23 5csingly linked list c with structs exampleimplementation of singly linked list using array in clinked list functions in clinked list struct ca simple program of linked list in cc linked list tutorialbasic operations linked list chow to create a list on ccreate list of pointers of linked list cc struct for linked listhow to create singly linked list in ccode for linked listc programmming list nodelinked list in cxeasy way to learn linked list in clink list titorial ccreate a linked list with all operations in clinked ilist in clinked list implementationlinked list implementation in cc linked list with a linked list structurec how to make a linked listhow to create head node in linked listlists in clinked list in clinear linked list in csingly linked list easy example in ccreate a list clinked list of a linked list in clinked list in c example codeprogram to create a linked list in cusing linked listsc a list of listslist of list in clinked list i clinked list in c with all the operationsstruct c linked listsingly linked list program in cnode clinked list programsc program to implement all the list operations using linked listslinear linke dlist in csingly linked lists in cimplementung link list in cc programming linked listimplementing linked list in ccreating a linked list in c with explanationlinked list in data structure clinkedlist chow to build linked list clist c programminglist chain in clinked lsit in clinked lists in cbasic operations in linked list in clinked list basic operations in cbuild a linked list in chow to make a linked listsyntax of linked list in cc program linked list operationsimplementation of linked list in c programlinked list declaration in clinked listr clinked list syntax cdynamic linked list in c codehow to implement a linked list in c languageuse linked lists cliunked list in c syntaxsingly linked list operations in clinked list functionsadd linked list cgo through linked list cc list 3eimplementation of linkeed listshow to make a list ccreate node cc linked list implementationlinked list implement in chow to use a list in chow to read a llist in cc program how to create a link listlist in csingly linked list in chow to create linked list in cc list structlinked list c languagelinked list in c 5clinked list in c programc linked list with structlink list clinkedlist function chow to creat a globally linked list in cimplementation linked list in c create a linked listprograms on linkedlist in cuses of linked lists in cwhat is a list in clinkedlist in ccreate simple linked list in clinked lists c examplescreate a linked list in c algorithm for singlyc list implementationstore data linked list cstructures and linked lists in cc linked list notationc code for linked listc singly linked list examplelinked list tutorial in cwhere is linked list created in csingly linked list program in data structure using clists clinked list example in clinked list i n cwhat is link list in cc lang list nextrwhat are linked lists used for in ccreating linkedlist in csingly linked list program using cwhat is linkedlist in cc c linked listhow to create linked list chow linked list is implemented in cwhy do we use linked list in chow to implement linked list in clinked list c programminglinked list in c implementationall operations on linked list in ccreate a list in clists in clinked list in example c examplelinked list implementation in c codedoubly linked list program in clinked list tutorial clistas clinked list using node struct and list structc listc listac progra how to do linked listcreating linked lists in clist in clinked list in example clink list c inplementationlinked list code in chow to do a linked list in cgeeksforgeeks linked list cstoring empty linked list in care there linked lists in cdoubly linked list in cdoes c have listscreate linked list in c loopcreate linked list next prev cimplement linkedlist in ccreating a list in cdynamic lists in ccan you create a linked list in chow to allocate a node in clinked list c programcreat link listc program linked listc programming in the linked listhow to use list in cprogram for linked list operations in cnode 2a in cpointer and linked kist in clinked list node cnode in cc programming listsc c2 a3 list linked list in cexample of singly linked list in clinked list codeslinkend list cdisplay function in c to linked listdefine a list in chow to create a linked list in clist function chow to create a list in clinked list c 2b 2b 27creat a generic list using a linked list in cc listc programming listbasic linked list program in cwhat are linked lists in c and how to use themhow to program a dubbel linked listlinkted list in ccode for linked list in chow to make a list in c 23 23how to make a linked list ccreation of linked list in cwhat is singly linked list in cmake linked listhow to make linked list in cworking with linked list in chow to create a linked list for any data typelinkedlist all fucntion in ccreate a node clist c 5blinked list using struct in cimplementation of linked list in cwhat is the use of linked list program in cc list how to node in c programminghow linked list workd in cc 24 listlinked list with pointerslinkedlist with clinked list programc program to implement a singly linked listhow to create linked listwrite a c program to implement singly linked listinked list in cwhats c liststruct list 2a next 3bstruct node cuse of a linked list in cc 23 listlinked list node in cc create linked listc linkedlistsingly linked list in c programhow linked list works in clinked list of linked lists in clinked list applications in ccreate linked list in clist in c programminglist h in c to create linked listhow give list of data to a ink list in clinked list with embedded clinked list and structure incc listehow to define list in clista clinked lists explained in ca linked list in celements in a list ccreate a simple linked list in cc program code for linked listfunction to create a linked list in cc program to create and display singly linked listexample for linked list in cc list structurelinked list create node in clista in ccreate linked list function in chow to make a list in c codecreate a node in linked list in c list cpointer node 3enext clinked list types in cwhy pointer node variable is required in dynamic linked listlinked list in c codecreation of a linked list in cstore data from limked list in cusing lists in chow to create a node in linked list chow to make a lsit in clinked list on cc program linked list addressc linked listyhow to create a linked list in c using functiondata structure linked list codestructure linked list in clinked list node c 23 24c general purpose linked listsingly linked list implementation in clist of c programming librarieslinked list implementation cc listexemple linked list in clinked list add in clist struct cwrite a program to implement singly linked list in clinked list c exampleshow to linked list in cis linkedlist is singly linked liststruct linked list def in chow to create new node in linked list cunderstanding linked list tutorial in c for beginnersuser defined linked list in csingly linked list clinked lists codelinked list c implementationlsit cnodes in ccreate a linked list of n nodes in ccreate singly linked list in clist method in cmake a list csimple linked list program in cc list of list of intlinked lists c implementationlist funciont cimplement a linked list using c how do u make a linked list in clist c libraryuse linked list in linked list csingly linked list application example in cwhat is linked lists in cnode list in ccreating a linked list struct chow to instantiate a linked list in cwhy are linked lists used in clinkediliist in ccreate a singly linked list in clinked list in c contentslist en cc creating a linked listhow to collect linked list pounter address c program to linked listlink list c implementationhow to create node in linked list in clink list in clink list programaccessing linked list with 5b 5d in cstruct linked list 2anext 3bhow to use a linked list in clinked list all operations in csingly linked list in c example programhow to implent linked lis in clinked structures in csingle link list nodehow to learn linked list in c easilynode c programmingsyntax in node to create a node in the singly linked listprogram which uses linked list in chow to create a linked list cimplement a linked list in ceasy linked list in clinked list using array in clinekd list cimplement linked list in cprogram to create and display a singly linked list in ccan you create a list in chow to creat linked list cwrite a program in c to create and display singly linked listc program to create a linked listcreate a linked list node using structures in csimple linked list in cc link list liblinked list in c using functiondisplay linked list chow to build a linked list clinked list in chow to create a linked list linked list clinked list program in c