Текущий узел не сбрасывается в головной узел, на который он ссылается - PullRequest
0 голосов
/ 22 октября 2018

Я создал 2 связанных списка.Узел и Кодекс.Кодекс - это проверка орфографии.

Eye I
eye I
chequer checker
Pea P
Sea C
plane plainly
lee skip
four for
revue review
Miss Mistakes
Steaks skip
knot not
sea see
quays keys
whirred word
weight wait
Two To
two to
Weather Whether
write right
oar or
aweigh away
threw through
Your You're
shore sure
two to
no know
Its It's
vary very
weigh way
tolled told
sew so
bless blessed
freeze frees
yew you
lodes loads
thyme time
right write
stiles styles
righting writing
aides aids
rime rhyme
frays phrase
come composed
posed skip
trussed trusted
too to
bee be
joule jewel
cheque check
sum some

Другой связанный список представляет собой текстовый файл слов.Похоже на это.


Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.

Теперь, когда вы найдете Eye в файле данных, вы замените его на I. Как вы можете видеть в файле кодекса, они находятся рядом с каждымДругой.Мой код работает при вставке текста в связанные списки.Но когда я передаю заголовок обоих связанных списков своей функции LIST_CORRECT, головной узел кодекса застревает в узле со словом sum (со второго по последнее слово в кодексе).Я попытался установить указатель на обе главы связанных списков и сделать эти токи, но это все еще не работает.Любые предложения о том, что делать?

Вот код.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

// A complete working C program to demonstrate all insertion methods
// on Linked List
// A linked list node
struct Node;
void push(struct Node **head_ref, char *new_data);
void insertAfter(struct Node *prev_node, char *new_data);
void append(struct Node **head_ref, char *new_data);
void printList(struct Node *node);
//void LIST_CORRECT(struct Codex **head_refC,struct Node **head_ref);
//void pushC(struct Codex **head_ref, char *new_data);
//void insertAfterC(struct Codex *prev_node, char *new_data);
//void appendC(struct Codex **head_ref, char *new_data);
//void printListC(struct Codex *node);
int LINECOUNT(FILE *(*stream), char *filename);

struct Node {
    char *data;
    struct Node *next;
};
struct Codex {
    char *word1;
    char *word2;
    struct Codex *next;
};
/* Given a reference (pointer to pointer) to the head of a list and
 an int, inserts a new node on the front of the list. */
void push(struct Node **head_ref, char *new_data) {
    /* 1. allocate node */
    struct Node* new_node = (struct Node *)malloc(sizeof(struct Node));

    /* 2. put in the data  */
    new_node->data  = new_data;
    //printf("push data:%s ", new_data);

    /* 3. Make next of new node as head */
    new_node->next = (*head_ref);

    /* 4. move the head to point to the new node */
    (*head_ref) = new_node;
}
void pushC(struct Codex **head_ref, char *new_word,char *new_word2) {
    /* 1. allocate node */
    struct Codex* new_node = (struct Codex *)malloc(sizeof(struct Codex));
    struct Codex* new_node2 = (struct Codex *)malloc(sizeof(struct Codex));

    /* 2. put in the data  */
    new_node->word1  = new_word;
    //printf("push data:%s ", new_data);

    /* 3. Make next of new node as head */
    new_node->next = (*head_ref);

    /* 4. move the head to point to the new node */
    (*head_ref) = new_node;
    //------------------------//
    /* 5. put in the data  */
    new_node2->word2  = new_word;
    //printf("push data:%s ", new_data);

    /* 6. Make next of new node as head */
    new_node2->next = (*head_ref);

    /* 7. move the head to point to the new node */
    (*head_ref) = new_node2;
}
/* Given a reference (pointer to pointer) to the head
 of a list and an int, appends a new node at the end  */

// This function prints contents of linked list starting from head
void printListC(struct Codex *node) {
    while (node != NULL) {
        printf("%s ", node->word1);
        node = node->next;
    }
}
void LIST_INSERT_POEM(struct Node **head_ref, char *new_data) {
    /* 1. allocate node */
    struct Node* new_node = (struct Node *)malloc(sizeof(struct Node));
    struct Node *last = *head_ref;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->data  = new_data;
    //printf("push data:%s ", new_data);

    /* 3. This new node is going to be the last node, so make next of
     it as NULL*/
    new_node->next = NULL;

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }

    /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;

    /* 6. Change the next of last node */
    last->next = new_node;
    return;
}
void LIST_INSERT_CODEX(struct Codex **head_ref, char *new_data) {
    /* 1. allocate node */
    struct Codex* new_node = (struct Codex *)malloc(sizeof(struct Codex));
    //if(x==1){
    struct Codex *last = *head_ref;  /* used in step 5*/

    /* 2. put in the data  */
    new_node->word1  = new_data;
    //printf("push data:%s ", new_data);

    /* 3. This new node is going to be the last node, so make next of
     it as NULL*/
    new_node->next = NULL;

    /* 4. If the Linked List is empty, then make the new node as head */
    if (*head_ref == NULL) {
        *head_ref = new_node;
        return;
    }

    /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;

    /* 6. Change the next of last node */
    last->next = new_node;
    return;
    }


// This function prints contents of linked list starting from head
void printList(struct Node *node) {
    while (node != NULL) {
        printf("%s ", node->data);
        node = node->next;
    }

}

int LINECOUNT(FILE *(*stream), char *filename) {
    int size = 0;
    size_t chrCount;
    char *text;

    if ((*stream = fopen(filename, "r")) == NULL) {
        printf("LC Could not open hw8 data file.\n");
        exit(0);
    }
    while (1) {
        text = NULL;
        getline(&text, &chrCount, *stream);
        free(text); /*free text*/
        if (feof(*stream))
            break;
        size++;
    }
    rewind(*stream);
    return size;
}
void LIST_CORRECT(struct Codex **head_refC,struct Node **head_ref,int lc,int x){
    int t;int e;printf("\nlc for codex:%d\n",lc);
    //struct Codex **currentC; struct Node **CurrentH;
    struct Codex **currentC = (struct Codex **)malloc(sizeof(struct Codex*));
    struct Node** currentH = (struct Node **)malloc(sizeof(struct Node*));
    currentC=head_refC;
    currentH=head_ref;
    printf("\ncurrentC:%s\n",(*currentC)->word1);
    printf("currentH:%s\n",(*currentH)->data);
    t = strcmp( ((*currentC)->word1) , ((*currentH)->data) );
    printf("t:%d\n",t);
    if(t==0){printf("(t==0)");((*currentH)->data)=(((*currentC)->next)->word1);}
    if(t!=0){for(int i = 0;i<lc-1;i++){printf("\ti:%d\tcurrentC:%s\t",i,(*currentC)->word1);(*currentC)=(*currentC)->next->next;printf("currentC:%s\n",(*currentC)->word1);}}
}

int main(void) {
    char *fn = "hw8data.txt";
    char *fn2 = "hw8codex.txt";

    int lineCount;
    int lineCount2;

    FILE *stream;
    FILE *stream2;
    lineCount = LINECOUNT(&stream, fn);
    lineCount2= LINECOUNT(&stream2,fn2);
    //int lineArr[lineCount];
    //int lineArr[];//lineArr[0] = 4 would say the first line has 4 words. using this data for strtok

    //lineArr = wordCount(&stream, fn, lineCount);

    //-------------------------------------
    char ch;
    int wordcount = 0;
    int charcount = 0;
    int wordcount2 = 0;
    int charcount2 = 0;
    stream = fopen("./hw8data.txt", "r");

    int x = 0;
    int lineArr[lineCount];
    for (int i = 0; i < lineCount; i++) {
        lineArr[i] = 0;
    }
    if (stream) {
        while ((ch = getc(stream)) != EOF) {
            if (ch != ' ' && ch != '\n') {
                charcount++;
            }
            if (ch == ' ' || ch == '\n') {
                wordcount++;
                lineArr[x] = lineArr[x] + 1;
            }
            if (ch == '\n') {
                x++;
            }
        }
        printf("\nwc:%d\n",wordcount);
        //if (charcount > 0) { wordcount++; charcount++; }
    } else {
        printf("Failed to open the file1\n");
    }
    //----------------------------------------------//
    stream2 = fopen("./hw8codex.txt", "r");

    int y = 0;
    int lineArr2[lineCount2];
    for (int i = 0; i < lineCount2; i++) {
        lineArr2[i] = 0;
    }
    if (stream) {
        while ((ch = getc(stream)) != EOF) {
            if (ch != ' ' && ch != '\n') {
                charcount2++;
            }
            if (ch == ' ' || ch == '\n') {
                wordcount2++;
                lineArr2[y] = lineArr2[y] + 1;
            }
            if (ch == '\n') {
                y++;
            }
        }
        //if (charcount > 0) { wordcount++; charcount++; }
    } else {
        printf("Failed to open the file2\n");
    }
    //----------------------------------------------//
    /* Start with the empty list */
    struct Node *head = NULL;
    struct Codex *headC = NULL;
    struct Node **headP = &head;
    struct Codex **headCP = &headC;
    printf("\n%p\t",&head);    printf("%p\n",headP);
    printf("\n%p\t",&headC);    printf("%p\n",headCP);


    rewind(stream);
    rewind(stream2);

    //char *sArr = malloc(42 * sizeof(char));
    //printf("List-----------------------------\n\n");
    for(int i =0; i<wordcount;i++){
    char *sArr = malloc(42 * sizeof(char));
    fscanf(stream, "%s ", sArr);
    //printf("%s ", sArr);
    LIST_INSERT_POEM(&head, sArr);
        i++;
    }

    //printf("\n\nCodex-----------------------------\n\n");
    char *sArr2 = malloc(42 * sizeof(char));
    for(int i =0; i<lineCount2+lineCount2;i++){
        char *sArr = malloc(42 * sizeof(char));
        char *sArr2 = malloc(42 * sizeof(char));
        fscanf(stream2, "%s %s", sArr,sArr2);
        //printf("%s %s ", sArr,sArr2);
        LIST_INSERT_CODEX(&headC, sArr);
        LIST_INSERT_CODEX(&headC, sArr2);

        i++;
    }
    //printf("\n\n-----------------------------\n\n");

    printListC(headC);
    printf("\n\n");
    printf("head->data:%s\theadC->word1:%s",(head)->data,(headC)->word1);

    for(int i = 0;i<wordcount;i++){
        printf("\nLIST_CORRECT CALL #%d\n",i+1);
        //LIST_CORRECT(&headC,&head,lineCount2,i);
        LIST_CORRECT(*(&headCP),*(&headP),lineCount2,i);
        printf("head->data:%s\theadC->word1:%s",(head)->data,(headC)->word1);
    }
    printList(head);

    return 0;
}
...