Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting Nodes in a Linked List
    primarykey
    data
    text
    <p>After a lot of effort, I've managed to piece together a function that deletes some node from my linked list. But, out of sheer interest, I would like to find out how you can go about deleting the first node from the list, i.e. the head.</p> <p>My program asks for a letter to delete, so for example. Hello is stored in the list, the user inputs H for deletion, so that now the list is ello At the moment with my code, the program crashes obviously as if H is deleted, there is no head, and the program doesn't know where to go to look for the list.</p> <p>Below is my current implementation, any clues or hints on how to modify this code( I would like to keep it similar to how I have) to allow Head Node Deletion would be much appreciated!.</p> <p>EDIT: In response to below</p> <pre><code>FullList DeleteNode(FullList temp, char c) { FullList remember; FullList ptr; while (temp.head-&gt;c != c) { remember.head = temp.head; temp.head = temp.head-&gt;next; } ptr.head = temp.head-&gt;next; free(temp.head); remember.head-&gt;next = ptr.head; return temp; } int main(void) { FullList List; char c, s; List.head = NULL; while ((c=getchar()) != '.') { List = addToEnd(List, c); } scanf(" %c", &amp;s); List = DeleteNode(List, s); while (List.head != NULL) { printf("%c", List.head-&gt;c); List.head = List.head-&gt;next; } return 0; } </code></pre> <hr> <pre><code>typedef struct List { char c; struct List *next; }List; typedef struct { List *head; List *tail; }FullList; List *insertList(char c, List *t1) { List *t = (List*)calloc(1, sizeof(List)); t-&gt;c = c ; t-&gt;next = t1; return t; } FullList addToEnd(FullList c, char element) { if (c.head == NULL) { c.head = c.tail = insertList(element, NULL); }else { c.tail-&gt;next = insertList(element, NULL); c.tail = c.tail-&gt;next; } return c; } void DeleteNode(FullList temp, char c) { FullList remember; FullList ptr; while (temp.head-&gt;c != c) { remember.head = temp.head; temp.head = temp.head-&gt;next; } ptr.head = temp.head-&gt;next; free(temp.head); remember.head-&gt;next = ptr.head; } int main(void) { FullList List; char c, s; List.head = NULL; while ((c=getchar()) != '.') { List = addToEnd(List, c); } scanf(" %c", &amp;s); DeleteNode(List, s); while (List.head != NULL) { printf("%c", List.head-&gt;c); List.head = List.head-&gt;next; } return 0; } </code></pre>
    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.
    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