Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, I think your problem is you point all your nodes to the same buffer. You need to copy that buffer every time.</p> <pre><code>myNode-&gt;data = buf; </code></pre> <p>Just points data to the address of the buffer. Then later, when you go update that buffer, the address stays the same but the contents change. So everything is essentially pointing to the same string. You need to copy the contents of the buffer into a new array and point data at that.</p> <p>Also this line seems questionable when you are looping adding nodes:</p> <pre><code>head = head-&gt;next; </code></pre> <p>Insert should be taking care of that.</p> <p>Here is your code rejiggered to work:</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 head) { nodePTR myNode; myNode = (node *)malloc(sizeof(node)); myNode-&gt;data = malloc(sizeof(char) * 10); strcpy(myNode-&gt;data,buf); myNode-&gt;next = head-&gt;next; myNode-&gt;previous = head; head-&gt;next = myNode; } 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); numElements=3; /* Build the list */ for(i = 0; i &lt; numElements; i++) { printf("Please enter the data:"); buf[0] = 60 + i; buf[1] = 0; insert(buf, head); } printlist(head, numElements); return 0; } </code></pre> <p>I got rid of the input prompts so I could make it work on codepad, but the idea is the same. Also I think I accidentally got rid of keeping track of the tail. This will insert stuff right after the head So it will go start:</p> <p>HEAD - TAIL</p> <p>HEAD - 1st - TAIL</p> <p>HEAD - 2nd - 1st - TAIL</p> <p>...</p> <p>where 2nd is the 2nd element you inserted.</p> <p>As for your </p> <pre><code>char * tmpBuf = buf; </code></pre> <p>The problem is that tmpBuf is on the stack and it <em>still</em> points to the same address as buf. You needed to allocate a new buffer with malloc (or build a new house per my metaphor in the comments).</p> <p>This line</p> <pre><code>myNode = (node *)malloc(sizeof(node)); </code></pre> <p>Creates enough room for the things you defined in your node struct. Which in your case are <strong><em>pointers</em></strong> to an array of characters, a next node and a previous node. So all you have room for is the address of an array of chars. You don't actually have room for an array of chars in your node.</p> <pre><code>myNode-&gt;data = malloc(sizeof(char) * 10); </code></pre> <p>This malloc actually makes room for an array of 10 chars and saves the address of the array in your nodes data field. Without this 2nd malloc you never make room for actual arrays. When you are first learning data structures, I might suggest using ints as your data type. There are less pitfalls to distract you from the workings of the actual data strucure you are learning about.</p>
 

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