Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked List Delete Node
    primarykey
    data
    text
    <p>So I'm trying to implement a function that deletes nodes from a linked list.</p> <p>Here's my main:</p> <pre><code>int main(void) { NODE* first = generateNodes(5); NODE* jank = getNode(first, 2); deleteNode(first,2); printf("Length of Node List: %d\n",getNodeListLength(first)); printf("First Node: %d\n",first -&gt; pos); printf("Jank Node: %d\n",jank -&gt; pos); return 0; } </code></pre> <p>This is my output:</p> <pre><code>Length of Node List: 2 First Node: 0 Jank Node: 2 </code></pre> <p>The output should be (because in <code>main()</code> I removed <code>jank</code>, and decreased the size of the linked list by one):</p> <pre><code>Length of Node List: 4 First Node: 0 Jank Node: NULL </code></pre> <p>Here's my entire source code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* NODE STRUCTURE */ typedef struct node{ char* thing; int pos; /* Index of node */ struct node* next; /* Pointer to next node */ } NODE; /* Generates a single node */ NODE* generateNode(); /* Generates linked nodes and returns the first node */ NODE* generateNodes(int num); /* Gets a node at a certain index */ NODE* getNode(NODE* start, int index); /* Returns the length of a list of nodes */ size_t getNodeListLength(NODE* start); /* Removes a node at a certain index */ NODE* deleteNode(NODE* start, int index); int main(void) { NODE* first = generateNodes(5); NODE* jank = getNode(first, 2); deleteNode(first,2); printf("Length of Node List: %d\n",getNodeListLength(first)); printf("First Node: %d\n",first -&gt; pos); printf("Other Node: %d\n",jank -&gt; pos); return 0; } NODE* generateNode() { return (NODE*) malloc(sizeof(NODE)); } NODE* generateNodes(int num) { NODE* one = generateNode(); NODE* cpy = one; int i; for(i = 0; i &lt; num - 1; i++) { NODE* next = generateNode(); cpy -&gt; next = next; cpy -&gt; pos = i; cpy = next; } cpy -&gt; pos = i; cpy -&gt; next = NULL; return one; } NODE* getNode(NODE* start, int index) { int i; for(i = 0; i &lt; index; i++) { start = start -&gt; next; } return start; } size_t getNodeListLength(NODE* start) { size_t i; while(start -&gt; next != NULL) { start = start -&gt; next; i++; } return i - 1; } NODE* deleteNode(NODE* start, int index) { if(index &gt; 0) { NODE* f = getNode(start,index - 1); NODE* l = getNode(start,index + 1); NODE* d = getNode(start,index); f -&gt; next = l; free(d); return start; } if(index == 0) { NODE* up = start -&gt; next; free(start); return up; } if(index + 1 == getNodeListLength(start)) { NODE* r = getNode(start,index); NODE* c = getNode(start,index - 1); c -&gt; next = NULL; free(r); return start; } } </code></pre> <p>Where did I go wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload