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