Linked list made in C language. The code allows you to add and remove a node structure at the beginning, end or any other given position by index.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node {
  4. int data;
  5. struct node *next;
  6. } node_t;
  7. void print_list(node_t *);
  8. void add_at_beginning(node_t **, int);
  9. void add_at_end(node_t *, int);
  10. void add_node_at_index(node_t **, int, int);
  11. int remove_last_node(node_t *);
  12. int remove_first_node(node_t **);
  13. int remove_node_by_index(node_t **, int);
  14. int main(void)
  15. {
  16. int data;
  17. node_t *head = NULL;
  18. head = (node_t *)malloc(sizeof(node_t));
  19. printf("Linked List with C.\n");
  20. head->data = 1;
  21. head->next = NULL;
  22. print_list(head);
  23. printf("\n-Made by CodeNoSchool ! Check it on YouTube, Twitter, GitHub!\n");
  24. return 0;
  25. }
  26. void print_list(node_t *head)
  27. {
  28. node_t *current = head;
  29. while (current != NULL)
  30. {
  31. printf("%d\n", current->data);
  32. current = current->next;
  33. }
  34. }
  35. void add_at_beginning(node_t **head, int data)
  36. {
  37. node_t *new_node = NULL;
  38. new_node = (node_t *)malloc(sizeof(node_t));
  39. new_node->data = data;
  40. new_node->next = *head;
  41. *head = new_node;
  42. }
  43. void add_at_end(node_t *head, int data)
  44. {
  45. node_t *current = head;
  46. node_t *new_node = NULL;
  47. new_node = (node_t *)malloc(sizeof(node_t));
  48. new_node->data = data;
  49. new_node->next = NULL;
  50. while (current->next != NULL)
  51. {
  52. current = current->next;
  53. }
  54. current->next = new_node;
  55. }
  56. int remove_last_node(node_t *head)
  57. {
  58. node_t *current = head;
  59. int data;
  60. while (current->next->next != NULL)
  61. {
  62. current = current->next;
  63. }
  64. data = current->next->data;
  65. free(current->next);
  66. current->next = NULL;
  67. return data;
  68. }
  69. int remove_first_node(node_t **head)
  70. {
  71. int data;
  72. data = (*head)->data;
  73. *head = (*head)->next;
  74. free(*head);
  75. return data;
  76. }
  77. int remove_node_by_index(node_t **head, int p)
  78. {
  79. if (p == 0)
  80. return remove_first_node(head);
  81. int data;
  82. int i;
  83. node_t *current = *head;
  84. for(i = 0; i < p - 1; i++)
  85. {
  86. current = current->next;
  87. }
  88. data = current->next->data;
  89. free(current->next);
  90. current->next = NULL;
  91. return data;
  92. }
  93. void add_node_at_index(node_t **head, int p, int data)
  94. {
  95. if (p == 0)
  96. {
  97. add_at_beginning(head, data);
  98. } else
  99. {
  100. node_t *current = *head;
  101. if (current->next == NULL)
  102. {
  103. add_at_end(*head, data);
  104. } else
  105. {
  106. int i;
  107. for(i = 0; i < p - 1; i++)
  108. {
  109. current = current->next;
  110. }
  111. node_t *new_node = NULL;
  112. new_node = (node_t *)malloc(sizeof(node_t));
  113. new_node->data = data;
  114. new_node->next = current->next;
  115. current->next = new_node;
  116. }
  117. }
  118. }