Simple plain linked lists implementation in C
Trees, Sort, Linked Lists are some of the important concepts in C programming. Here is a short example of simple plain linked list. You can compile it using GNU C compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <stdlib.h>; #include <stdio.h>; struct _linked_list { struct _linked_list *next; int x; }; typedef struct _linked_list linked_list; void d_node(int val, linked_list **s); void d_node(int val, linked_list **s1) { linked_list *temp; linked_list *s = *s1; while(s) { if(s->x == val){ temp->next = s->next; free(s); } temp = s; s = s->next; } } int main(int argc, char *argv[]) { int i = 10, j = 0; linked_list *temp = NULL; linked_list *head = NULL; while(j < i) { head = malloc(sizeof(struct _linked_list)); head->x = j; head->next = temp; temp = head; j++; } temp = head; while(head) { printf("%d \n", head->x); head = head->next; } head = temp; d_node(5, &head); head = temp; printf("Node Deleted \n"); while(head) { printf("%d \n", head->x); head = head->next; } } |