2016-05-26 17 views
0

このプログラムは入力をスキャンし、Cのリンクリストの概念を使用して出力します。プログラムは入力をスキャンし、 ' - 'を検出すると停止します。問題は、入力を逆に印刷することです。私はすべてを逆にする別の機能を実装したくありません。私はエラーがどこにあるのかわかりません。リンクされたリストはC言語で逆に印刷されます

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

typedef struct student { 
    char name[64]; 
    unsigned long long student_number; 
    struct student *next; 
} student; 

int main() 
{ 
    student *curr_stdptr; 
    student *hd_stdptr = NULL; 
    int i,x,y; 
    char input[70]; 
    char in_name[60]; 


    for(i=0;i<6;i++) { 
        curr_stdptr = (student *)malloc(sizeof(student)); 
        curr_stdptr->next = hd_stdptr; 
        hd_stdptr = curr_stdptr; 

        fgets(input,60,stdin); 
        for(x=0;input[x]!='-';x++) 
         hd_stdptr->name[x] = input[x]; 
         hd_stdptr->name[x] = '\0'; 

        } 


    while(curr_stdptr!=NULL) { 
    printf("%s\n",curr_stdptr->name); 
    curr_stdptr=curr_stdptr->next; 
    } 

} 
+0

問題の内容がわからないため、エラーの原因がわかりません。 – Lee

+0

@ OPの部分には言葉の貧弱な選択肢があります。問題は明確に述べられています:「問題は、入力を逆に印刷することです」。その目的は、同じ入力順で印刷することです。 – WhozCraig

+1

あなたのコードを他の人に読ませてもらうようにしてください。できるだけ少なくしてください。 – davmac

答えて

1
curr_stdptr = (student *)malloc(sizeof(student)); 
       curr_stdptr->next = hd_stdptr; 
       hd_stdptr = curr_stdptr; 

あなたはstudentあなたは常にorder.Youが新しいstudentsを挿入することができ、逆を取得することが新しいhead.So最後studentがhead.Soなっ作り始めにそれを挿入し、新たな読み毎回あなたは注文を維持することができます。

 //initialize hd_stdptr and curr_stdptr as NULL before loop 
     new_stdptr = (student *)malloc(sizeof(student)); 
     new_stdptr->next=NULL; 
     if(hd_stdptr!=NULL)//not the first node 
     { 
      curr_stdptr->next = new_stdptr; 
     } 
     else 
     { //first node 
      hd_stdptr = new_stdptr; 
     } 
     curr_stdptr = new_Stdptr; 

EDIT:@WhozCraigコメント後編集。


前向き連鎖ポインタのポインタ方法

入力順にリンクされたリストを構築するための代替的な構築方法は、ポインタへのポインタで前方連鎖を使用することです。最初に、ポインタへのポインタはheadポインタのアドレスを保持する(これは最初はNULLである)。アイテムが入力されると、アイテムは常に次のノードを受け取るポインタのアドレスを保持します。終了したら、それはそれによって、リストを終了、最後のヌルで終了し

あなたのコード内でいくつかの潜在的な問題を修正を含む完全な例は以下の通りです:

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

typedef struct student 
{ 
    char name[70]; 
    unsigned long long student_number; 
    struct student *next; 
} student; 

int main() 
{ 
    student *hd_stdptr = NULL, *p; 
    student **pp = &hd_stdptr; 

    int i,x; 
    char input[70]; 


    for(i=0; i<6 && fgets(input,sizeof(input),stdin) != NULL; ++i) 
    { 
     student *p = malloc(sizeof *p); 
     if (p == NULL) 
     { 
      perror("Failed to allocate new student: "); 
      exit(EXIT_FAILURE); 
     } 

     for (x=0; input[x] && input[x] != '-' && input[x] != '\n'; ++x) 
      p->name[x] = input[x]; 
     p->name[x] = 0; 

     /* save where new node belong */ 
     *pp = p; 

     /* and advance our ptr-to-ptr to address location for next node */ 
     pp = &p->next; 
    } 
    *pp = NULL; 


    for (p=hd_stdptr; p; p = p->next) 
     printf("%s\n",p->name); 


    /* free the list */   
    while (hd_stdptr) 
    { 
     p = hd_stdptr; 
     hd_stdptr = p->next; 
     free(p); 
    } 
} 
+0

ポインターツーポインターに注意するだけで、リンクされたリンクリスト。また、両方のブロックの2番目のステートメントをそれぞれの外部に引き出し、以下にまとめることができます。 – WhozCraig

+0

私はあなたの2番目のポイントを持っています。最初に説明してください。 –

+0

私が説明した内容を反映するように回答が更新されました。それが役に立てば幸い。 – WhozCraig

-1
int main() 
{ 
    student *curr_stdptr; 
    student *hd_stdptr = NULL; 
    student *tail_stdptr = NULL; 
    int i,x,y; 
    char input[70]; 
    char in_name[60]; 


    for(i=0;i<6;i++) { 
        curr_stdptr = (student *)malloc(sizeof(student)); 
        if(i == 0) 
        { 
         tail_stdptr = hd_stdptr = curr_stdptr; 
        } 
        else 
        { 
         tail_stdptr->next = curr_stdptr; 
         tail_stdptr = curr_stdptr; 
        } 

        fgets(input,60,stdin); 
        for(x=0;input[x]!='-';x++) 
         tail_stdptr->name[x] = input[x]; 
         tail_stdptr->name[x] = '\0'; 
        } 

    curr_stdptr = hd_stdptr; 
    while(curr_stdptr!=NULL) { 
    printf("%s\n",curr_stdptr->name); 
    curr_stdptr=curr_stdptr->next; 
    } 

} 

あなたはこのコードを試すことができ、あなたはlinked listの末尾を指す別のポインタtail_stdptrを追加できます。 追加された要素ごとに、tail_stdptrに従ってリンクリストの末尾に追加することができます。

関連する問題