2016-04-22 9 views
0

以下のプログラムは理論上、ユーザが追加、表示、または減算することができるリンクリストを作成する必要があります。現在、私の問題は、リンクされたリストに複数の値を印刷しようとしています。他のノードを追加するのではなく、ノードを変更するリンクされたリスト

主な問題は、私はかなりadd_node関数から来ていると思いますが、私はそれを変更することはできません。 (減算クラスオプションがありますが、関連性がないので機能を省略しました)

リンクリストに追加するには、これを変更する必要がありますか?

任意の助けを大幅にこれまでのところそれだけで次のように出力さ

#include <iostream> 
#include <cstring> 

struct ToDoList 
{ 
    std::string start_time; 
    std::string activity_name; 
    int time_for_activity; 
    ToDoList *next; 
}; 

void add_node(ToDoList * & head, std::string start, std::string activity,int time) 
{ 
    ToDoList* temp; 
    head = new ToDoList; 
    temp = new ToDoList; 
    head-> start_time = start; 
    head-> activity_name = activity; 
    head-> time_for_activity = time; 
    head-> next = head; 
    head = temp; 
    temp = temp->next; 
    head->next=NULL; 
}//in theory this should add another node to the list but it isn't working 

int main() 
{ 
    int ans, i = 0; 
    std::string start; 
    std::string activity; 
    int time; 
    ToDoList* head; 
    ToDoList* a; 
    std::cout << "Enter the start time in HH:MM am format: "; 
    std::getline(std::cin, start); 
    std::cout << "Enter the activity name: "; 
    std::getline(std::cin, activity); 
    std::cout << "Enter the time for the activity: "; 
    std::cin >> time; 
    //~ add_node(head); 
    add_node(head,start,activity,time); 

    std::cout << "\nWelcome to the Linked list menu! What would you like to do? \n0 Add a Node \n1 Display the list \n2 Delete a node \n3 Quit \n"; 
    std::cin >> ans; 
    while(ans != 3)//This should print all the values in the list 
    { 
     std::cin.ignore(); 
     if(ans == 0) 
     { 
      std::cout << "Enter the start time in HH:MM am format: "; 
      std::getline(std::cin, start); 
      std::cout << "Enter the activity name: "; 
      std::getline(std::cin, activity); 
      std::cout << "Enter the time for the activity: "; 
      std::cin >> time; 
      add_node(head,start,activity,time); 

     } 
     else if(ans == 1) 
     { 
      a = new ToDoList;//creates new pointer for while loop 
      a = head; 
      while(a != NULL)//loop used for printing 
      { 
       std::cout << i << " " << a->start_time << " " << a->activity_name << " " << a->time_for_activity << "\n"; 
       a = a -> next; 
       i++; 
      } 
      i = 0;//resets integer i 
     } 
     std::cout << "\nWelcome to the Linked list menu! What would you like to do? \n0 Add a Node \n1 Display the list \n2 Delete a node \n3 Quit \n"; 
     std::cin >> ans; 
    } 
    return 0; 
} 

をいただければ幸いです。

Enter the start time in HH:MM am format: 10:00 am 
Enter the activity name: Office Hours 
Enter the time for the activity: 30 

Welcome to the Linked list menu! What would you like to do? 
0 Add a Node 
1 Display the list 
2 Delete a node 
3 Quit 
0 
Enter the start time in HH:MM am format: 11:00 am 
Enter the activity name: Lunch 
Enter the time for the activity: 60 

Welcome to the Linked list menu! What would you like to do? 
0 Add a Node 
1 Display the list 
2 Delete a node 
3 Quit 
1 
0 0 
test 
+0

チュートリアル[here](http://pastebin.com/DXunz58Q) – sp2danny

答えて

1

新しいノードを追加するたびに新しいノードを作成する必要はありません。また、たとえそれを修正しても、新しいノードを追加するたびにNULLを頭の次に間違って割り当ててしまい、最初のものよりもリストのどのメンバーにもアクセスできなくなります。最後に、temp-> tempの次の割り当ては、最初のリスト要素以外のリスト要素にアクセスできなくなるもう一つの問題の原因です。

以下は、誤った文が削除されたコードのバージョンです。 hereが表示されているように、動作するようです。また

void add_node(ToDoList * & head, std::string start, std::string activity,int time) 
{ 
    ToDoList* temp; 
    temp = new ToDoList; 
    temp-> start_time = start; 
    temp-> activity_name = activity; 
    temp-> time_for_activity = time; 
    temp-> next = head; 
    head = temp; 
} 

、あなたは私がIdeone上で実験し、あなたのコードのバージョンで見ることができるようそれは主に、あなたの質問に関連していなかったものの、私はメインに、だけでなく、削除、NULLに頭を初期化ポインタへの不必要な動的割り当て。ループの一部が正常に終了できるように(つまり、決して終了しないように)、メモリリークを防ぐために割り当てを削除することが不可欠であるように、頭を初期化する必要があります。

0

あなたはadd_nodeの各呼び出しでheadを変更:

head = new ToDoList; 

あなたが可能それは空リストのためだけにしてください。

0

add_nodeを呼び出すたびにヘッドポインタを上書きしています。これは、毎回リストを失うことを意味します。あなたがそれをアレンジ方法は、あなたはmainに頭のポインタを割り当てるべきではありません、また間違っているし、代わりに

void add_node(ToDoList * & head, std::string start, std::string activity,int time) 
{ 
    ToDoList* temp = new ToDoDoList; 
    temp -> start_time = start; 
    temp -> activity_name = activity; 
    temp -> time_for_activity = time; 
    temp -> next = head; 
    head = temp; 
} 

は、この作業を行うには(あなたがリストの先頭に追加すると仮定して、終わりではない)しなければなりません代わりにNULLに初期化して、最初の呼び出しでリストの最後にNULLが設定されていることを確認します。私。

int main() 
{ 
    int ans, i = 0; 
    std::string start; 
    std::string activity; 
    int time; 
    ToDoList* head = NULL; 
    .... 

さらに、プログラムには多くのメモリリークがあります。まず、リストを削除することはありません。リストは二重リンクリストではないため、再帰的に行う必要があります。この機能を使用するとmainにあなたは即座にだけ割り当てられたメモリへの参照を失ったとしても、メモリリークである

a = new ToDoList;//creates new pointer for while loop 
a = head; 

を行い、さらに、head

void CleanList(ToDoList* node) 
{ 
    if (node == NULL) 
    { 
     return; 
    } 
    CleanList(node->next); 
    delete node; 
} 

でそれを呼び出します。代わりに、あなたは単に

a = head; 

EDIT言うべき:

固定タイプミスをして、今、この答えでコードが動作するとクラッシュしていないことを確認するためにテストしました。

関連する問題