-2

重複の可能性:
リンクリストに個別の単語を保存する

1つのリンクリストから単語を読み取るコードを書き込もうとしています。

次に、固有の単語を特定し、その単語を別のリンクリストに保存します。

残念ながら、コードは機能していません。この問題を解決するにはヘルプが必要です。

これは私がこれまでに得たものです、私はそれが役立つことを願ってコメントを追加しました。

struct list {

    char string[50];
    struct list *next;
};

list *header;
list *next = NULL;


struct distinct {

    char string[50];
    struct distinct *dnext; 
};

distinct *track;
distinct *dnext;


void checkdistinct() {

        int dwcheck = 0;  //as boolean to check whether distinct word is found
        list *ori;        //first struct
        ori = header->next;
        distinct *copy;   //second struct
        distinct *check = NULL;

        track = (distinct*)malloc(sizeof(distinct));
        track->dnext=NULL;
        copy = track;

        if(copy == track) {   // direct copy first time
            strcpy(copy->string, ori->string);
        }
        else {}

        while(ori->next!=NULL) { // while original struct did not end
            check = track;      

            while(check->dnext != NULL) { //check end when second list ends
                    if(strcmp(ori->string, check->string)!=0) {
                        check = check->dnext;
                    }
                    else if(strcmp(ori->string, check->string)==0) {
                        dwcheck = 1;
                        ori = ori->next; // original list will move one node next
                        check = check->dnext; // check pointer continues
                    }
            }

            copy->dnext = (distinct*)malloc(sizeof(distinct)); // new node for new word
            copy = copy->dnext;

            if(dwcheck != 1) { // when boolean = false, original will move one node next, next word will be copied
                ori = ori->next;   // as the node is moved one node (above) when boolean = true
            }
            else if(dwcheck == 1) {
                strcpy(copy->string, ori->string);
            }

            dwcheck = 0; // reset
            copy->dnext=NULL; // set not copied node as NULL each time
            check = NULL; // reset
        }

    }

私の最初のリスト:こんにちは私の名前はスーパーマンです

私の2番目のリスト:こんにちは私の名前名<-残り

悪いコーディングで申し訳ありませんが、まだ新人です。ありがとう!

4

1 に答える 1

0

最初に元のリストをソートする方が良いと思います。次に、これを行うことができます:

prev = 0; current = first string;

while (current) {
  if (strcmp(prev, current)!=0) {
     add current to distinct list;
  } 
  prev = current;
  current = current->next;
}

また、この部分は常に真実であるため、私には意味がありません。

    copy = track;

    if(copy == track) {   // direct copy first time
        strcpy(copy->string, ori->string);
    }
于 2012-06-27T13:22:58.667 に答える