Note that there are some explanatory texts on larger screens.

plurals
  1. POI can't create doubly linked list properly in C
    primarykey
    data
    text
    <p>My list head always points to tail. What's the problem?</p> <p>My <code>linked_list.h</code>:</p> <pre><code>#ifndef LINKED_LIST #define LINKED_LIST struct node { char *data; struct node *nextElement; struct node *prevElement; }; void createList(struct node **head, struct node **tail); void fill_list (char *word, struct node **head, struct node **tail); #endif </code></pre> <p><code>main.c</code>:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "linked_list.h" #include &lt;string.h&gt; int main() { FILE *dataFile; char *word = (char *) calloc ( 255, sizeof(char) ); /* Create empty list */ struct node *head, *tail; createList (&amp;head, &amp;tail); /*------------------------*/ /* Data file open*/ dataFile = fopen("data.txt" ,"r"); if( dataFile == NULL ) { perror("Error while opening the file.\n"); exit(EXIT_FAILURE); } /* Data reading */ while (( fscanf(dataFile, "%s", word) ) != EOF ) { int i = 0; int wordsCount = 0; for (i = 0; i &lt;= strlen(word); i++) { if ( (word[i] &gt;= 'a') &amp;&amp; (word[i] &lt;= 'z') ) wordsCount = wordsCount + 1; } if ( wordsCount == strlen(word) ) { fill_list ( word, &amp;head, &amp;tail ); } } fclose(dataFile); return 0; }; </code></pre> <p>and <code>linked_list.c</code>:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "linked_list.h" void createList(struct node **head, struct node **tail) { *head = NULL; *tail = NULL; } void fill_list ( char *word, struct node **head, struct node **tail ) { struct node *elem, *temp; if ( (*head) == NULL ) { // printf("HEAD = NULL\n"); elem = (struct node *) malloc ( sizeof (struct node) ); elem -&gt; data = word; elem -&gt; nextElement = NULL; elem -&gt; prevElement = NULL; (*head) = elem; *tail = elem; // printf("%s\n", (*head) -&gt; data ); } else { // printf("HEAD != NULL\n"); elem = (struct node *) malloc ( sizeof (struct node) ); elem -&gt; data = word; elem -&gt; nextElement = NULL; elem -&gt; prevElement = *tail; *tail = elem; // printf("%s\n", (*head) -&gt; data ); } } </code></pre> <p>My data file: qw erty b cc. Firstly, <code>head == NULL</code>, so <code>head -&gt; data = 'qw'</code> and it should be as head all the time, but it changes to erty, then b and cc after every loop step.</p> <p>What I'm doing wrong?</p>
    singulars
    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