studyhubC visualizer

C visualizer

Pick a data structure, how it’s built, and an operation, then step through its C one line at a time. The arrow in the code is the line that just ran; whatever it changed is lit in the drawing.

insertAt — Walk to the link at position pos and splice the node in.

step 1 / 29

Before the call: the structure as main built it.

List · Linked list

LA200A100NULL2A300A200elemlink3A400A3005NULLA400

Function call · in main

  1. 1insertAt(&L, 4, 2); (runs next)

Function definition · insertAt

  1. 1void insertAt(LIST *A, int newData, int pos) {
  2. 2 int length = getLength(*A);
  3. 3 if (pos <= 0 || pos > length) {
  4. 4 printf("Invalid position!\n");
  5. 5 return;
  6. 6 }
  7. 7 int i;
  8. 8 LIST *ptr;
  9. 9 for (i = 1, ptr = A; *ptr != NULL && i < pos; ptr = &(*ptr)->link, i++) {}
  10. 10 LIST newNode = (LIST)malloc(sizeof(struct node));
  11. 11 if (newNode != NULL) {
  12. 12 newNode->elem = newData;
  13. 13 newNode->link = *ptr;
  14. 14 *ptr = newNode;
  15. 15 }
  16. 16}

Function definition · getLength

  1. 1int getLength(LIST A) {
  2. 2 int length = 0;
  3. 3 for (; A != NULL; A = A->link) {
  4. 4 length++;
  5. 5 }
  6. 6 return length;
  7. 7}
Show the whole program
  1. 1#include <stdio.h>
  2. 2#include <stdlib.h>
  3. 3
  4. 4typedef struct node {
  5. 5 int elem;
  6. 6 struct node* link;
  7. 7} *LIST;
  8. 8
  9. 9int getLength(LIST A) {
  10. 10 int length = 0;
  11. 11 for (; A != NULL; A = A->link) {
  12. 12 length++;
  13. 13 }
  14. 14 return length;
  15. 15}
  16. 16
  17. 17void insertFirst(LIST *A, int newData) {
  18. 18 LIST newNode = (LIST)malloc(sizeof(struct node));
  19. 19 if (newNode != NULL) {
  20. 20 newNode->elem = newData;
  21. 21 newNode->link = *A;
  22. 22 *A = newNode;
  23. 23 }
  24. 24}
  25. 25
  26. 26void insertLast(LIST *A, int newData) {
  27. 27 LIST *ptr;
  28. 28 for (ptr = A; *ptr != NULL; ptr = &(*ptr)->link) {}
  29. 29 *ptr = (LIST)malloc(sizeof(struct node));
  30. 30 if (*ptr != NULL) {
  31. 31 (*ptr)->elem = newData;
  32. 32 (*ptr)->link = NULL;
  33. 33 }
  34. 34}
  35. 35
  36. 36void insertAt(LIST *A, int newData, int pos) {
  37. 37 int length = getLength(*A);
  38. 38 if (pos <= 0 || pos > length) {
  39. 39 printf("Invalid position!\n");
  40. 40 return;
  41. 41 }
  42. 42 int i;
  43. 43 LIST *ptr;
  44. 44 for (i = 1, ptr = A; *ptr != NULL && i < pos; ptr = &(*ptr)->link, i++) {}
  45. 45 LIST newNode = (LIST)malloc(sizeof(struct node));
  46. 46 if (newNode != NULL) {
  47. 47 newNode->elem = newData;
  48. 48 newNode->link = *ptr;
  49. 49 *ptr = newNode;
  50. 50 }
  51. 51}
  52. 52
  53. 53void insertSorted(LIST *A, int newData) {
  54. 54 LIST *trav;
  55. 55 for (trav = A; *trav != NULL && newData > (*trav)->elem; trav = &(*trav)->link) {}
  56. 56 LIST newNode = (LIST)malloc(sizeof(struct node));
  57. 57 if (newNode != NULL) {
  58. 58 newNode->link = *trav;
  59. 59 newNode->elem = newData;
  60. 60 *trav = newNode;
  61. 61 }
  62. 62}
  63. 63
  64. 64void deleteFirst(LIST *A) {
  65. 65 if (*A != NULL) {
  66. 66 LIST temp = *A;
  67. 67 *A = temp->link;
  68. 68 free(temp);
  69. 69 } else {
  70. 70 printf("Nothing to be deleted.\n");
  71. 71 }
  72. 72}
  73. 73
  74. 74void deleteLast(LIST *A) {
  75. 75 if (*A == NULL) {
  76. 76 printf("Nothing to be deleted.\n");
  77. 77 return;
  78. 78 }
  79. 79 LIST *trav;
  80. 80 for (trav = A; (*trav)->link != NULL; trav = &(*trav)->link) {}
  81. 81 LIST temp = *trav;
  82. 82 *trav = temp->link;
  83. 83 free(temp);
  84. 84}
  85. 85
  86. 86void deleteAt(LIST *A, int pos) {
  87. 87 int length = getLength(*A);
  88. 88 if (pos < 1 || pos > length) {
  89. 89 printf("Invalid position!\n");
  90. 90 } else {
  91. 91 LIST *trav;
  92. 92 int count = 1;
  93. 93 for (trav = A; *trav != NULL && count < pos; trav = &(*trav)->link, count++) {}
  94. 94 LIST temp = *trav;
  95. 95 *trav = temp->link;
  96. 96 free(temp);
  97. 97 }
  98. 98}
  99. 99
  100. 100void deleteElem(LIST *A, int x) {
  101. 101 LIST *trav;
  102. 102 for (trav = A; *trav != NULL && (*trav)->elem != x; trav = &(*trav)->link) {}
  103. 103 if (*trav != NULL) {
  104. 104 LIST temp = *trav;
  105. 105 *trav = temp->link;
  106. 106 free(temp);
  107. 107 } else {
  108. 108 printf("element not found\n");
  109. 109 }
  110. 110}
  111. 111
  112. 112int main(void) {
  113. 113 LIST L = NULL;
  114. 114 insertLast(&L, 2);
  115. 115 insertLast(&L, 3);
  116. 116 insertLast(&L, 5); (just ran)
  117. 117 insertAt(&L, 4, 2);
  118. 118 return 0;
  119. 119}