2013-02-10 34 views
1

我正在寫一個簡單的程序來處理C++中的結構,但有一個我無法解決的問題。雙鏈表總是隻包含1條記錄

我的程序收到很少的結構作爲輸入。它應該按鍵排序並打印出來。但我的代碼我有我的列表中永遠只有一個結構:

#include "iostream" 
#include "string.h" 
#include "limits" //ignore max 
#include "stdlib.h"//atof 
using namespace std; 

struct Struct { 
    char text[10]; 
    int age; 
    Struct* prev; 
    Struct* next; 
}; 

int input(string msg) { 
    char str[2]; 
    int check = 0, len = 0, 
    var = 0, 
     i = 0; 
    while (1) { 
     cout << msg; 
     cin.getline(str, 2); 
     if (cin.fail()) { 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     } 
     len = strlen(str); 
     check = 0; 
     for (i = 0; i < len; i++) { 
      if (isdigit(str[i])) { 
       check++; 
      } 
     } 
     if (check == len && !(check == 1 && str[0] == '-') && check != 0 && atoi(str) != 0) { 
      var = atoi(str); 
      return var; 
     } else { 
      cout << "Error!" << endl; 
     } 

    } 
} 

Struct* add_struct_to_list(Struct* prev) { 
    Struct* NewStruct = 0; 
    char str[10]; 
    int age; 
    cout << "Name: "; 
    cin.getline(str, 10); 
    if (cin.fail()) { 
     cin.clear(); 
     cin.ignore(numeric_limits <streamsize>::max(), '\n'); 
    } 
    age = input("Age: "); 
    NewStruct = new Struct; 
    strcpy(NewStruct->text, str); 
    NewStruct->age = age; 
    NewStruct->prev = prev; 
    NewStruct->next = 0; 
    return NewStruct; 
} 

Struct* start_new_list(int number) { 
    Struct* NewList = 0; 
    NewList = add_struct_to_list(0); 
    Struct* NewStruct = NewList; 
    int counter = 1; 
    for (counter; counter < number; counter++) { 
     NewStruct = add_struct_to_list(NewStruct); 
    } 
    return NewList; 
} 

void delete_all_list(Struct* list_begin) { 
    Struct* to_delete = list_begin->next; 
    Struct* next = 0; 
    delete[] list_begin; 
    if (to_delete != 0) { 
     do { 
      next = to_delete->next; 
      delete[] to_delete; 
     } while (next != 0); 
    } 
} 

void sort_by_age(Struct* list_begin) { 
    Struct* node = 0; 
    Struct* node2 = 0; 
    int age; 
    for (node = list_begin; node; node = node->next) { 
     for (node2 = list_begin; node2; node2 = node2->next) { 
      if (node->age < node2->age) { 
       age = node->age; 
       node->age = node2->age; 
       node2->age = age; 
      } 
     } 
    } 
} 

void print_list(Struct* list_begin) { 
    for (Struct* node = list_begin; node; node = node->next) { 
     cout << "Age: " << node->age << "; Name: " << node->text << endl; 
    } 
} 

int main() { 
    int number = input("Number of students: "); 
    Struct* NewList = start_new_list(number); 
    sort_by_age(NewList); 
    print_list(NewList); 
    delete_all_list(NewList); 
    return 0; 
} 

輸入:

Number of students: 3 
Name: as 
Age: 1 
Name: as 
Age: 2 
Name: as 
Age: 3 

輸出:

Age: 1; Name: as 

另外請注意,這是家庭作業,我必須使用struct s。

UPD:感謝所有人的幫助!

+0

歡迎來到Stack Overflow。是否有避免使用'std :: list'和'std :: sort'的特殊原因? – 2013-02-10 10:45:15

+0

而用於存儲學生姓名的'std :: string' ... – LihO 2013-02-10 10:47:29

+0

另外請學習如何正確設置代碼的格式,這樣人們可以閱讀它。 – Rapptz 2013-02-10 10:48:02

回答

1

您正在嘗試使用node->next指針通過列表進行迭代:

for (Struct* node = list_begin; node; node = node->next) { 
    cout << "Age: " << node->age << "; Name: " << node->text << endl; 
} 

但你添加新的Struct s轉換列表是錯誤的方式,因爲你總是設置next0

Struct* add_struct_to_list(Struct* prev) { 
    ... 
    NewStruct->prev = prev; 
    NewStruct->next = 0; 
    return NewStruct; 
} 

即使您分配了3個新的Struct s,它們全都將指向next等於0。添加新Struct到列表看起來是這樣的正確的方法:

Struct* start_new_list(int number) { 
    Struct* prevStruct = NULL; 
    Struct* newList = NULL;     // pointer to the first struct 
    for (int counter = 0; counter < number; counter++) { 
     Struct* newStruct = add_struct_to_list(prevStruct); 
     if (prevStruct)      // if there was previous struct: 
      prevStruct->next = newStruct; // make it point to new struct 
     if (counter == 0)     // if it is first allocated struct: 
      newList = newStruct;   // store its address 
     prevStruct = newStruct;    // store last struct as "prev" 
    } 
    return newList; 
} 

還要注意的是,當你調用new分配內存,你應該通過調用delete釋放它。您正在使用delete[],應在分配new[]時使用。清理你的名單應該是這樣的:

void delete_all_list(Struct* list_begin) { 
    Struct* structToDelete = NULL; 
    Struct* node = list_begin; 
    while (node->next) { 
     structToDelete = node; 
     node = node->next; 
     delete structToDelete; 
    } 
    delete node; 
} 

希望這有助於:)

+0

非常感謝你!但是,我還有一個問題。當我的程序試圖使用函數delete_all_list時,它會返回內存問題。我做錯了什麼? – 2013-02-10 11:45:05

+0

行動已經回答了,謝謝! – 2013-02-10 11:46:23

+0

@ user1901863:不客氣:) – LihO 2013-02-10 11:48:50

0

NewStruct->未來始終是0。這是你期待什麼?

此外,您可能希望將結構排序爲一個單元,而不是改變人們的年齡!

相關問題