2016-07-13 24 views
0

私のプログラムの作り方を理解できません。プログラムは、ユーザに番号を入力して印刷するように要求する。リンクされたリストを使用して値を保存します。しかし、それを働かせることができない、助けを必要とする 。私はあなたがパラメータを渡しているが、それらはまた、出力パラメータであり、あなたのinsert(head, current);機能で参照を引数として渡す関数への参照を渡す方法

#ifndef STRUCTURE_H_ 
#define STRUCTURE_H_ 

struct intNode 
{ 
    int value; 
    struct intNode *next; 
}; 

#endif 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include "structure.h" 
#define commandLength 6 
#define initSize 100 

void insert(struct intNode *head, struct intNode *current); 
void print(struct intNode *head); 

int main() 
{ 
    char command[commandLength]; 
    struct intNode *head = NULL; 
    struct intNode *current = NULL; 

    printf("Enter a command from the following list of commands:\n\n" 
      "insert - to add a number to the list;\n" 
      "print - to print out the list;\n" 
      "exit - to terminate the program;\n\n" 
      "Enter your command:\n"); 

    while (strcmp(command, "exit") != 0) 
    { 
     scanf("%s",command); 

     if(strcmp(command,"insert") == 0) 
     {insert(head, current);} 
     else if(strcmp(command, "print") == 0) 
     {print(head);} 
     else if(strcmp(command, "exit") == 0) 
     {printf("\nThe program has been terminated.");} 
     else 
     {printf("Error: unknown request '%s'\n", command);} 
    }//end of while 
}//end of main 

void insert(struct intNode *head, struct intNode *current) 
{ 
    struct intNode *node; 
    node = (struct intNode *) malloc(sizeof(struct intNode)); 
    printf("enter a number:"); 
    scanf("%d",&node->value); 
    if(head == NULL) 
    { 
     head = node; 
    } 
    else 
    { 
     if(head->next == NULL) 
     { 
      head->next = node; 
      current = node; 
     }else{ 
      printf("%d ",current->value); 
      current->next = node; 
      current = node; 
     } 
    } 
}//end of insert 

void print(struct intNode *head) 
{ 
    struct intNode *temp; 
    temp = head; 

    while(temp != NULL) 
    { 
     printf("%d ",temp->value); 
     temp = temp->next; 
    } 
}//end of print 

答えて

0

私の構造を定義するためにstructure.hファイルを使用していました。だからあなたはそれらのアドレスを渡して、その値を変更する必要があります。これはmain()に反映されます。だから、

insert(&head, &current) 

への呼び出しを変更し、私はあなたのコードの2つのバージョンを作成しました

void insert(struct intNode **head, struct intNode **current) 
{ 
    struct intNode *node; 
    node = (struct intNode *) malloc(sizeof(struct intNode)); 
    printf("enter a number:"); 
    scanf("%d",&node->value); 
    //--v note * here and other places 
    if(*head == NULL) 
    { 
     *head = node; 
    } 
    else 
    { 
     if((*head)->next == NULL) 
     { 
      (*head)->next = node; 
      *current = node; 
     }else{ 
      printf("%d ",(*current)->value); 
      (*current)->next = node; 
      (*current) = node; 
     } 
    } 
}//end of insert 
+0

ありがとうございました!!!! –

0

としてあなたの機能を更新します。 1つはバグの[ほとんどの]とクリーンアップされたと作業バージョン[無償のスタイルのクリーンアップをご赦免ください]と注釈。

headを挿入時に設定した場合は、mainに設定されていません。insertのみです。私はリストヘッダーのアプローチとして "ダミー"ノードを使用してこれを解決しました。

#ifndef STRUCTURE_H_ 
#define STRUCTURE_H_ 

struct intNode { 
    int value; 
    struct intNode *next; 
}; 

#endif 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include "structure.h" 
#define commandLength 6 
#define initSize 100 

void insert(struct intNode *head, struct intNode *current); 
void print(struct intNode *head); 

int 
main() 
{ 
    char command[commandLength]; 
    struct intNode *head = NULL; 
    struct intNode *current = NULL; 

    printf("Enter a command from the following list of commands:\n\n" "insert - to add a number to the list;\n" "print - to print out the list;\n" "exit - to terminate the program;\n\n" "Enter your command:\n"); 

    // NOTE/BUG: on the first time through the loop, command is invalid, so do 
    // this: 
#if 1 
    command[0] = 0; 
#endif 
    while (strcmp(command, "exit") != 0) { 
     scanf("%s", command); 

     // NOTE/BUG: passing current serves no purpose 
     if (strcmp(command, "insert") == 0) { 
      insert(head, current); 
     } 
     else if (strcmp(command, "print") == 0) { 
      print(head); 
     } 
     else if (strcmp(command, "exit") == 0) { 
      printf("\nThe program has been terminated."); 
     } 
     else { 
      printf("Error: unknown request '%s'\n", command); 
     } 
    }         // end of while 
}          // end of main 

void 
insert(struct intNode *head, struct intNode *current) 
{ 
    struct intNode *node; 

    node = (struct intNode *) malloc(sizeof(struct intNode)); 
    printf("enter a number:"); 
    scanf("%d", &node->value); 

    // NOTE/BUG: when head is set here, it only modifies it within _this_ 
    // function and does _not_ update it in main 
    if (head == NULL) { 
     head = node; 
    } 

    // NOTE: this is closer to the insertion you want to do 
    else { 
     if (head->next == NULL) { 
      head->next = node; 
      current = node; 
     } 
     else { 
      printf("%d ", current->value); 
      current->next = node; 
      current = node; 
     } 
    } 
}          // end of insert 

void 
print(struct intNode *head) 
{ 
    struct intNode *temp; 

    temp = head; 

    while (temp != NULL) { 
     printf("%d ", temp->value); 
     temp = temp->next; 
    } 
}          // end of print 

ここでクリーンアップと作業バージョンがあります:


ここで注釈付きバージョンです。 headlistに変更し、その新しい役割をより詳しく説明しました。

#ifndef STRUCTURE_H_ 
#define STRUCTURE_H_ 

struct intNode { 
    int value; 
    struct intNode *next; 
}; 

#endif 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
//#include "structure.h" 

#define commandLength 6 
#define initSize 100 

void insert(struct intNode *list); 
void print(struct intNode *list); 

int 
main() 
{ 
    char command[commandLength]; 
    struct intNode list; 

    list.next = NULL; 

    while (1) { 
     printf("Enter a command from the following list of commands:\n\n" "insert - to add a number to the list;\n" "print - to print out the list;\n" "exit - to terminate the program;\n\n" "Enter your command:\n"); 

     scanf("%s", command); 

     if (strcmp(command, "exit") == 0) { 
      printf("\nThe program has been terminated.\n"); 
      break; 
     } 

     if (strcmp(command, "insert") == 0) { 
      insert(&list); 
      continue; 
     } 

     if (strcmp(command, "print") == 0) { 
      print(&list); 
      continue; 
     } 

     printf("Error: unknown request '%s'\n", command); 
    } 
} 

void 
insert(struct intNode *list) 
{ 
    struct intNode *current; 
    struct intNode *prev; 
    struct intNode *node; 

    node = malloc(sizeof(struct intNode)); 
    node->next = NULL; 

    printf("enter a number:"); 
    scanf("%d", &node->value); 

    // find the end of the list 
    prev = NULL; 
    for (current = list->next; current != NULL; current = current->next) 
     prev = current; 

    if (prev != NULL) 
     prev->next = node; 
    else 
     list->next = node; 
} 

void 
print(struct intNode *list) 
{ 
    struct intNode *temp; 

    for (temp = list->next; temp != NULL; temp = temp->next) 
     printf("%d ", temp->value); 
    printf("\n"); 
} 
関連する問題