Ahoj,
muze mi nekdo poradit, proc mi nefunguje funkce sorts a pritom funguje insback? Jde mi o to, ze v sorts kompletne preskocim while loop, zatimco v insback to funguje spravne. Uz do toho koukam snad hodinu a nevidim chybu.
Diky.
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// maximum length for a word
#define LENGTH 45
// default dictionary
#define DICTIONARY "words.txt"
typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}
Word;
Word *head = NULL;
/****************************************/
/*              PROTOTYPES              */
/****************************************/
Word* sorts(char *node);
Word* insback(char *node);
void create(char *node);
void display();
void freememory();
/****************************************/
/*              FUNCTIONS               */
/****************************************/
void create(char *node)
{
    Word *new_node = NULL;
    new_node=(Word*)malloc(sizeof(Word));
    if(head==NULL)
    {
        printf ("Creating list\n");
        new_node->next=NULL;
        head=new_node;
        printf ("List created\n");
    }
}
//Insert sorted after the word
Word* sorts(char *node)
{
    Word *temp1;
    temp1 = (Word*)malloc(sizeof(Word));
    temp1 = head;
    while(temp1->next!=NULL)
    {
        printf ("looping\n");
        Word *curr;
        curr = (Word*)malloc(sizeof(Word));
        strncpy(curr->word, node, LENGTH);
        if (strcmp(curr->word, temp1->word) >0 )
        {
        printf ("adding word\n");
        curr->next = NULL;
        temp1->next = curr;
        }
        
        temp1 = temp1->next;
    }
    return head;
}
//Insert from back
Word* insback(char *node)
{
    Word *temp1;
    temp1 = (Word*)malloc(sizeof(Word));
    temp1 = head;
    
    while(temp1->next!=NULL)
    {
        temp1 = temp1->next;
    }
    
    Word *curr;
    curr = (Word*)malloc(sizeof(Word));
    strncpy(curr->word, node, LENGTH);
    curr->next = NULL;
    temp1->next = curr;
    return head;
}
// output the list
void display()
{
    Word *Wordlist;
    Wordlist = head;
    while (Wordlist != NULL)
    {
        printf("%s->", Wordlist->word);
        Wordlist = Wordlist->next;
    }
    printf("End of the list!\n");
}
// free allocated memory
void freememory()
{
    Word *freenode = NULL;
    Word *curr = NULL;
    freenode = head;
    while(freenode != NULL)
    {
        curr = freenode;
        freenode = freenode->next;
        free(freenode->word);
        free(curr);
    }
}
/****************************************/
/*               M A I N                */
/****************************************/
int main()
{
    system("clear");
    char data[LENGTH];
    int count = 0;
        
    FILE* file = fopen(DICTIONARY, "r");
    printf ("Opening file\n");
    
    // check successfull opening of the file
    if(file == NULL) {
        perror("Error opening file");
        return -1;
    }
   
    create(NULL);
    
    //read data (words) from file
    while(fscanf (file, "%s", data) == 1)
    {
        count++;
        insback(data);
    }
    
    //insfront("Start");
    printf ("%d words read\n", count);
    display();
    freememory();
    fclose(file);
    return 0;
}