2016-11-04 12 views
1

私は値渡しのノードを削除しようとしていますが、関数によって返されましたが、参照で渡されます。私はこのコードを試しましたが、コンパイラは:* head = * head-> nextで文句を言う。参照で渡されたノードを削除するには

#include<stdio.h> 


typedef struct nody student; 


struct nody{ 

    char name[20]; 

    double gpa; 

    student *next; 

}; 

student* deletefirstlist(student **head, student *nn); 

int main(){ 

    student *head, *node, *w; 

    node = (student*) malloc(sizeof(student)); 
    gets(node->name); 
    node->gpa=3.6; 
    node->next=NULL; 

    head = node; 

    node = (student*) malloc(sizeof(student)); 
    gets(node->name); 
    node->gpa=3.7; 
    node->next=head; 

    head = node; 

    w = head; 
    while(w!=NULL){ 
     printf("%s %lf\n\n", w->name, w->gpa); 
     w=w->next; 
    } 
    node = deletefirstnode(&head, node); 

    while(w!=NULL){ 
     printf("%s %lf\n\n", w->name, w->gpa); 
     w=w->next; 
    } 
    return 0; 
} 

student* deletefirstlist(student **head, student *nn){ 


    nn = *head; 
    *head = *head->next; // the problem is here 
    nn->next=NULL; 

    return nn; 
} 

おかげで、私はあなたが頭やリンクされたノードとのリストを作成していることを理解して、あなたが最初に削除しようと引用されたコードから百万

+0

'*頭部> next' - >' (* head) - > next' – BLUEPIXY

+0

私は* head = nn-> nextをやろうとしました。 – TheGame

+0

それは働いた。それは正しい? – TheGame

答えて

0

(:ここ

コードですヘッド)ノード。それがそうであるならば、 あなたのコードは次のように修正する必要があります。

#include<stdio.h> 


    typedef struct nody student; 


    struct nody{ 

     char name[20]; 

     double gpa; 

     student *next; 

    }; 

    student *deletefirstnode(student *head, student *nn); 

    int main(){ 

    student *head, *node, *w; 

    node = (student*) malloc(sizeof(student)); 
    gets(node->name); 
    node->gpa=3.6; 
    node->next=NULL; 

    head = node; 

    node = (student*) malloc(sizeof(student)); 
    gets(node->name); 
    node->gpa=3.7; 
    head->next=node; 
    node->next=NULL; 

    // head->next = node (The first node is the head node) 

    w = head; 
    while(w!=NULL){ 
    printf("%s %lf\n\n", w->name, w->gpa); 
    w=w->next; 
    } 
    node = deletefirstnode(head, node); 
    free(head); //to free up the memory of the old first node 
    w=node; //reset w to point to the new First node of the list 
    while(w!=NULL){ 
    printf("%s %lf\n\n", w->name, w->gpa); 
    w=w->next; 
    } 
    return 0; 
    } 

    student *deletefirstnode(student *head, student *nn){ 
     nn = head->next; 
     return nn; 
    } 

は、これらの助けを願っています。

+0

ありがとう、それは助けになる!ちょうど自由(頭)について。ステートメント。私たちが頭を失ったかどうかわかるように、リンクされたリストは失われているので、私たちはそれをしなくてはなりません。 私が間違っている場合は私を修正してください。 – TheGame

0

全体的に補正サンプル

#include <stdio.h> 
#include <stdlib.h> //need this 

typedef struct nody student; 

struct nody{ 
    char name[20]; 
    double gpa; 
    student *next; 
}; 

student* deletefirstlist(student **head, student *nn); 

int main(){ 
    student *head, *node, *w; 

    node = (student*) malloc(sizeof(student));//casting is not necessary in C. 
    *node->name = 0; 
    //gets has already been abolished. 
    while(1 != scanf("%19[^\n]%*c", node->name)){ 
     printf("input name\n"); 
     scanf("%*[^\n]");scanf("%*c"); 
    } 
    node->gpa=3.6; 
    node->next=NULL; 

    head = node; 

    node = (student*) malloc(sizeof(student)); 
    *node->name = 0; 
    while(1 != scanf("%19[^\n]%*c", node->name)){ 
     printf("input name\n"); 
     scanf("%*[^\n]");scanf("%*c"); 
    } 
    node->gpa=3.7; 
    node->next=head; 

    head = node; 

    w = head; 
    while(w != NULL){ 
     printf("%s %lf\n\n", w->name, w->gpa); 
     w = w->next; 
    } 
    node = deletefirstlist(&head, NULL);//The argument node is not necessary substantially. also function name is typo. 

    free(node); 

    w = head;//you forgot this 
    while(w != NULL){ 
     printf("%s %lf\n\n", w->name, w->gpa); 
     w = w->next; 
    } 
    //free rest 
    return 0; 
} 

student* deletefirstlist(student **head, student *nn){ 
    nn = *head; 
    if(*head){//NOT NULL 
     *head = (*head)->next; // *head->next meant *(head->next) 
     nn->next=NULL; 
    } 

    return nn; 
} 
+0

他の文字を入力せずにEnterキーを押すと%19 [^ \ n]%* c "'が機能しません –

+0

はい、もちろんです。あなたはそれをチェックすることができます。または、名前を入力する必要のある名前を入力します。 :D – BLUEPIXY

0

は限りコンパイラの訴えが考慮されるように、これはそれを

*head = (*head)->next; 

を修正します。しかし、あなたがのためにメモリを解放していないようなコードを持つ他の問題があります

node = deletefirstnode(&head, node); 
free(node); //missing 
w=head; //w still points to the deleted node, point it to the new head 
while(w!=NULL){ 

BLUEPIXYのコードが処理しますこれらすべての問題。あなたは私が名前の文字列を使用することをお勧めかもしれませんが、同じのためのC++を使用している場合も

getline(cin,name); 

はあなたが

student* deletefirstlist(student*& head, student *nn); 

があり習慣

としてあなたの関数を使用することができるよう

、あなたは簡単に入力を取ることができますインダイレクションを使用する必要があります。あなたは、単に行うことができます。

student* deletefirstlist (student* &head, student *nn) { 
nn = head; 
if (head) { 
    head = head->next; // *head->next meant *(head->next) 
    nn->next=NULL; 
} 

return nn; 
} 

として呼び出す:あなたはあまりにもFN宣言を変更する必要があります

node = deletefirstlist(head, node); 

student* deletefirstlist(student *&head, student *nn); 
関連する問題