Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with my Linked List code?
    primarykey
    data
    text
    <p>Question was solved :)<br /></p> <p>I was hoping you could help explain what I am doing wrong.</p> <p>Thanks in advance!</p> <p><strong>Code:</strong><br /></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; typedef struct node { char *data; struct node * previous; struct node * next; } node, *nodePTR; /* Insert into list */ void insert(char * buf, nodePTR tail) { nodePTR myNode; myNode = (node *)malloc(sizeof(node)); myNode-&gt;data = malloc(sizeof(char) * 10); strcpy(myNode-&gt;data,buf); myNode-&gt;next = NULL; myNode-&gt;previous = tail; tail-&gt;next = myNode; //tail = tail-&gt;next; } void printlist(nodePTR head, int numElements) { nodePTR tmpNode; tmpNode = head; printf("\n\n"); while(tmpNode!=NULL) { printf("Node data: %s\n", tmpNode-&gt;data); tmpNode = tmpNode-&gt;next; } } int main(void) { /* Variables */ int numElements; int i; char buf[10]; nodePTR head, tail; tail = (node *)malloc(sizeof(node)); head = (node *)malloc(sizeof(node)); tail-&gt;data = "EMPTY\0"; tail-&gt;next = NULL; tail-&gt;previous = NULL; head = tail; printf("Please enter the number of elements:\n"); scanf("%d", &amp;numElements); /* Build the list */ for(i = 0; i &lt; numElements; i++) { printf("Please enter the data:"); scanf("%s", buf); insert(buf, tail); tail = tail-&gt;next; } printlist(head, numElements); return 0; } </code></pre> <p><strong>Here is my output:</strong><br /> Please enter the number of elements:<br /> 3<br /> Please enter the data:n1<br /> Please enter the data:n2<br /> Please enter the data:n3<br /> <br /> Node data: EMPTY<br /> Node data: n3<br /></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