Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>The issue is with the way how double pointers are used.</em></p> <p>Here is the <strong>full working code</strong>: I have made some changes on the way how double pointers were used. You can see the changes in pop function and copyList function.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct node { int data; struct node* next; }; static int length(struct node** head); static void push(struct node** head, int data); static int pop(struct node** head); static void appendNode(struct node** head, int data); static struct node *copyList(struct node** head); static void printList(struct node** head); /************************************************************ length - return length of a list ************************************************************/ int length(struct node** head) { int count = 0; struct node* current = NULL; current = *head; while (current != NULL) { current = current-&gt;next; ++count; } return count; } /************************************************************ push - add new node at beginning of list ************************************************************/ void push(struct node** head, int data) { struct node* new_ptr = NULL; new_ptr = (struct node*)malloc(sizeof(struct node)); new_ptr-&gt;data = data; new_ptr-&gt;next = *head; *head = new_ptr; } /************************************************************ pop - delete node at beginning of non-empty list and return its data ************************************************************/ int pop(struct node** head) { int val = 0; struct node* temp = NULL; if (*head != NULL) { val = (*head)-&gt;data; temp = (*head)-&gt;next; free(*head); *head = temp; } return(val); } /************************************************************ appendNode - add new node at end of list ************************************************************/ void appendNode(struct node** head, int data) { struct node* current = NULL; struct node* previous = NULL; struct node* new_ptr = NULL; current = *head; previous = current; while (current != NULL) { previous = current; current = current-&gt;next; } new_ptr = (struct node*)malloc(sizeof(struct node)); new_ptr-&gt;data = data; new_ptr-&gt;next = NULL; previous = new_ptr; } /************************************************************ copyList - return new copy of list ************************************************************/ struct node* copyList(struct node** head) { struct node* copy = NULL; struct node* current = NULL; struct node* new_ptr = NULL; /* Copy current head to copy */ current = *head; while (current != NULL) { appendNode(&amp;copy, current-&gt;data); current = current-&gt;next; } return copy; } /************************************************************ printList - print linked list as "List: &lt; 2, 5, 6 &gt;" (example) ************************************************************/ void printList(struct node** head) { struct node* current = NULL; printf("List: &lt; "); current = *head; if (current == NULL) printf("none "); while (current != NULL) { printf("%d", current-&gt;data); current = current-&gt;next; if (current != NULL) printf(", "); } printf(" &gt;\n"); } void main() { int i; // index used for loops struct node *list_a; // a new list struct node *list_a_copy; // copy of list list_a = NULL; // initialize empty list list_a_copy = NULL; // initialize empy list // test push for (i = 0; i &lt; 4; ++i) push(&amp;list_a, i); // test length printf("Length of list = %d\n", length(&amp;list_a)); // test print head list printf("head:\n"); printList(&amp;list_a); // test append node for (i = 4; i &lt; 8; ++i) appendNode(&amp;list_a, i); // test print head list printf("head(append):\n"); printList(&amp;list_a); // make a copy of list list_a_copy = copyList(&amp;list_a); // test pop head list for (i = 0; i &lt; 4; ++i) printf("%d popped\n", pop(&amp;list_a)); // test print copy list printf("head copy:\n"); printList(&amp;list_a_copy); // test pop copy list for (i = 0; i &lt; 4; ++i) printf("%d popped\n", pop(&amp;list_a_copy)); } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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