{"id":27418,"date":"2018-02-02T21:18:00","date_gmt":"2018-02-02T15:48:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27418"},"modified":"2018-02-02T21:18:00","modified_gmt":"2018-02-02T15:48:00","slug":"generic-linked-list-c","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/generic-linked-list-c\/","title":{"rendered":"Generic Linked List in C"},"content":{"rendered":"<p>Unlike C++ and Java, C doesn\u2019t support generics. How to create a linked list in C that can be used for any data type? In C, we can use void pointer and function pointer to implement the same functionality.<span id=\"more-134691\"><\/span> The great thing about void pointer is it can be used to point to any data type. Also, size of all types of pointers is always is same, so we can always allocate a linked list node. Function pointer is needed process actual content stored at address pointed by void pointer.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20for%20generic%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20%2F%2F%20Any%20data%20type%20can%20be%20stored%20in%20this%20node%0A%20%20%20%20void%20%20*data%3B%0A%20%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20add%20a%20node%20at%20the%20beginning%20of%20Linked%20List.%0A%20%20%20This%20function%20expects%20a%20pointer%20to%20the%20data%20to%20be%20added%0A%20%20%20and%20size%20of%20the%20data%20type%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20void%20*new_data%2C%20size_t%20data_size)%0A%7B%0A%20%20%20%20%2F%2F%20Allocate%20memory%20for%20node%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20new_node-%3Edata%20%20%3D%20malloc(data_size)%3B%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F%2F%20Copy%20contents%20of%20new_data%20to%20newly%20allocated%20memory.%0A%20%20%20%20%2F%2F%20Assumption%3A%20char%20takes%201%20byte.%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3Cdata_size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20*(char%20*)(new_node-%3Edata%20%2B%20i)%20%3D%20*(char%20*)(new_data%20%2B%20i)%3B%0A%20%0A%20%20%20%20%2F%2F%20Change%20head%20pointer%20as%20new%20node%20is%20added%20at%20the%20beginning%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list.%20fpitr%20is%20used%0A%20%20%20to%20access%20the%20function%20to%20be%20used%20for%20printing%20current%20node%20data.%0A%20%20%20Note%20that%20different%20data%20types%20need%20different%20specifier%20in%20printf()%20*%2F%0Avoid%20printList(struct%20node%20*node%2C%20void%20(*fptr)(void%20*))%0A%7B%0A%20%20%20%20while%20(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20(*fptr)(node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Function%20to%20print%20an%20integer%0Avoid%20printInt(void%20*n)%0A%7B%0A%20%20%20printf(%22%20%25d%22%2C%20*(int%20*)n)%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20print%20a%20float%0Avoid%20printFloat(void%20*f)%0A%7B%0A%20%20%20printf(%22%20%25f%22%2C%20*(float%20*)f)%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20node%20*start%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20and%20print%20an%20int%20linked%20list%0A%20%20%20%20unsigned%20int_size%20%3D%20sizeof(int)%3B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B10%2C%2020%2C%2030%2C%2040%2C%2050%7D%2C%20i%3B%0A%20%20%20%20for%20(i%3D4%3B%20i%3E%3D0%3B%20i\u2013)%0A%20%20%20%20%20%20%20push(%26start%2C%20%26arr%5Bi%5D%2C%20int_size)%3B%0A%20%20%20%20printf(%22Created%20integer%20linked%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList(start%2C%20printInt)%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20and%20print%20a%20float%20linked%20list%0A%20%20%20%20unsigned%20float_size%20%3D%20sizeof(float)%3B%0A%20%20%20%20start%20%3D%20NULL%3B%0A%20%20%20%20float%20arr2%5B%5D%20%3D%20%7B10.1%2C%2020.2%2C%2030.3%2C%2040.4%2C%2050.5%7D%3B%0A%20%20%20%20for%20(i%3D4%3B%20i%3E%3D0%3B%20i\u2013)%0A%20%20%20%20%20%20%20push(%26start%2C%20%26arr2%5Bi%5D%2C%20float_size)%3B%0A%20%20%20%20printf(%22%5Cn%5CnCreated%20float%20linked%20list%20is%20%5Cn%22)%3B%0A%20%20%20%20printList(start%2C%20printFloat)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Created integer linked list is\r\n 10 20 30 40 50\r\n\r\nCreated float linked list is\r\n 10.100000 20.200001 30.299999 40.400002 50.500000<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Generic Linked List in C &#8211; Linked List &#8211; Unlike C++ and Java, C doesn\u2019t support generics. How to create a linked list in C that can be used.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,79476,79478],"tags":[79636,73368,81539,81537,81541,80334,81536,79583,81538,73337,81540,79502,79631,80333,81542],"class_list":["post-27418","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-linked-list","category-singly-linked-list","tag-c-linked-list-library","tag-doubly-linked-list-program-in-c","tag-generic-linked-list-c","tag-generic-linked-list-java","tag-insertion-in-linked-list-in-c","tag-linked-list-implementation-in-c","tag-linked-list-in-c-example","tag-linked-list-in-c-program","tag-linked-list-in-c-program-with-explanation","tag-linked-list-program-in-c","tag-linked-list-program-in-c-for-insertion-and-deletion","tag-linked-list-program-in-c-with-explanation","tag-linked-list-program-in-c-pdf","tag-linked-list-program-in-java","tag-singly-linked-list-program-in-data-structure-using-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27418"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27418\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}