Note that there are some explanatory texts on larger screens.

plurals
  1. POPointers Linked Lists C
    primarykey
    data
    text
    <p>Did I do this right? I'm getting a strange error here... </p> <pre> linked.c: In function ‘push’: linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default] linked.c: In function ‘main’: linked.c:146:9: error: incompatible type for argument 1 of ‘push’ linked.c:45:6: note: expected ‘struct node **’ but argument is of type ‘struct node’ ~/swen250/CLinkedList$ gcc -o linked linked.c linked.c: In function ‘push’: linked.c:50:19: warning: assignment from incompatible pointer type [enabled by default] ~/swen250/CLinkedList$ gcc -o linked linked.c linked.c: In function ‘pop’: linked.c:63:19: error: request for member ‘data’ in something not a structure or union linked.c:64:20: error: request for member ‘next’ in something not a structure or union linked.c: In function ‘copyList’: linked.c:106:9: warning: passing argument 1 of ‘appendNode’ from incompatible pointer type [enabled by default] linked.c:75:6: note: expected ‘struct node **’ but argument is of type ‘struct node *’ </pre> <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(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.
 

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