2014-03-25 29 views
1

我有這個代碼的問題.....閱讀方法工作正常,但當我想添加新項目到我的單向列表中並添加此項時,一切都「崩潰」項目依賴於evertything,按升序排序。文件中的項目按快捷方式升序排列。它的工作原理但並不完全,我甚至在紙上畫出了每一個想法來跟隨我的代碼,我不知道爲什麼當我試圖在屏幕上打印所有列表時,它失去了列表中的最後一項。請幫我解決這個問題。下面的代碼與C和C++混合使用。列表失去列表中的最後一項C

#include <iostream> 
#include <stdio.h> 
#include <conio.h> 
#pragma warning(disable:4996) 
using namespace std; 

int howManyRecords = 0; // how many record readed from file 


struct pojazd 
{ 
    char model[40]; // Name of the vechicle 
    int yearOfProduction; // Year of production 
    float engineCapacity; // capacity of the engine 
    struct pojazd *nast; // pointer for the next element 
}; 
struct pojazd* creatingNewItem() // method creating new object of this structure for later adding it to list 
{ 
    struct pojazd *tmpVechicle=NULL; 

    tmpVechicle = (struct pojazd*)malloc(sizeof(struct pojazd)); 

// MODEL, YEAR AND CAPACITY OF THE ENGINE 
///////////////// 
    cout << "Zaraz podasz dane nowego pojazdu. Przygotuj sie." << endl << endl; 
    cout << "Podaj model samochodu: "; cin >> tmpVechicle->model; cout << endl; 
    cout << "Podaj rok produkcji samochodu: "; cin >> tmpVechicle->yearOfProduction; cout << endl; 
    cout << "Podaj pojemnosc silnika samochodu: "; cin >> tmpVechicle->engineCapacity; cout << endl; 

    tmpVechicle->nast = NULL; 

    cout<<"Model:"<< tmpVechicle->model<<" rok:" << tmpVechicle->yearOfProduction << " pojemosc:" << tmpVechicle->engineCapacity <<endl; 

    return tmpVechicle; 

} 


//Adding new created item to the list using pointers to list and new item 
// Adding it to the list keeping ascending politic. 

void addingNewItemToList(struct pojazd *headList, struct pojazd *newItem) 
{ 

    struct pojazd *pomocnicza = NULL, *head = NULL; 

    head = headList->nast; 
    pomocnicza = headList; 

    while(true) 
    { 


      if((pomocnicza->yearOfProduction < newItem->yearOfProduction) && (newItem->yearOfProduction < head->yearOfProduction)) 
      { 
       pomocnicza->nast = newItem; 
       newItem->nast = head; 
       break; 
      } 
      else if((head->nast == NULL) && (pomocnicza->yearOfProduction < newItem->yearOfProduction)) 
      { 
       pomocnicza->nast = newItem; 
       break; 
      } 
      else 
      { 
       pomocnicza = head; 
       head = head->nast; 
      } 
    } 

} 

// READING FROM FILE AND ALLOCATING NEW OBJECT OF LIST 
///////////////////////// 
struct pojazd* uzupelnianieListy(FILE *odczytywanie) 
{ 


    struct pojazd *beggining = NULL,*nextElement = NULL; 

    while (!feof(odczytywanie)) 
    { 
     if (beggining == NULL) 
     { 
      beggining = nextElement = (struct pojazd*)malloc(sizeof(struct pojazd)); 
     } 
     else 
     { 
      nextElement->nast = (struct pojazd*)malloc(sizeof(struct pojazd)); 
      nextElement = nextElement->nast; 
     } 

     fscanf(odczytywanie, "%s %d %f", nextElement->model, &(nextElement->yearOfProduction), &(nextElement->engineCapacity)); 
     cout << nextElement->model << endl; 
     cout << nextElement->yearOfProduction << endl; 
     cout << nextElement->engineCapacity << endl; 

     cout << "\n"; 
     nextElement->nast = NULL; 

     howManyRecords++; 
     cout<< howManyRecords <<endl; 

    } 
    fclose(odczytywanie);//closing pliku 

    system("pause"); 

    return beggining; 

} 


int main() 
{ 
// INPUT OUTPUT FILE 
    char wejscie[20], wyjscie[20]; 

    FILE* odczytywanie; 
    FILE *zapisywanie; 
//HEAD OF THE LIST 
    struct pojazd *headList = NULL; 
//NEW ITEM POINTER 
    struct pojazd *newItem = NULL; 

//ADDITIONAL POINTER IN PRINTING CODE at THE BOTTOM 
    struct pojazd *helper = NULL; 


    cout << "Podaj nazwe pliku do odczytu: "; cin >> wejscie; 
    odczytywanie = fopen(wejscie, "r"); 




    headList = uzupelnianieListy(odczytywanie); 


    newItem = creatingNewItem(); // Creating new Item 

    addingNewItemToList(headList, newItem); 

    helper = headList; 

    /// NEW LIST OF ITEMS 
    //// 
    cout << "*************************Nowa lista*********************" << endl; 
    for(int i = 0; i < howManyRecords; i++) 
    { 

     cout << helper->model << endl; 
     cout << helper->yearOfProduction << endl; 
     cout << helper->engineCapacity << endl; 

     helper = helper->nast; 

    } 
    cout << "*************************koniec Nowa lista*********************" << endl; 


    _getch(); 
    return 0; 
} 

這裏是內容的文件:

// NAME YEAR CAPACITY 

Syrena 1977 650 
Maluch 1999 3800 
Polonez 2004 1774 

什麼是錯的程序....?

+0

只是一個註釋:不要使用波蘭語標識符名稱。它非常不鼓勵非拋光型揚聲器甚至看代碼。 – fritzone

+2

這可能聽起來有些沾沾自喜,但爲了提高獲得幫助的機會 - 用英文編寫代碼:)我很難跟蹤流,因爲變量名對我來說很亂。那是什麼 - 波蘭語? :) –

+0

肯定給我一分鐘重寫它。 – Darek

回答

1

當您添加新記錄時,您不會在dodawanieDoListy中增加ileRekordow。 (或這是howManyRecordsaddingNewItemToList在你的新代碼)

這裏是例子,應該如何:

void addingNewItemToList(struct pojazd **headList, struct pojazd *newItem) 
{ 

    struct pojazd *pomocnicza = NULL, *head = NULL; 

    head = (*headList)->nast; 
    pomocnicza = *headList; 

    while(true) 
    { 


     if(head == NULL) 
     { 
      pomocnicza->nast = newItem; 
      break; 
     }else if((pomocnicza->yearOfProduction <= newItem->yearOfProduction) && (newItem->yearOfProduction < head->yearOfProduction)) 
     { 
      pomocnicza->nast = newItem; 
      newItem->nast = head; 
      break; 
     }else if (pomocnicza->yearOfProduction>newItem->yearOfProduction){ 
      newItem->nast=pomocnicza; 
      (*headList)=newItem; 
      break; 
     } 
     else 
     { 
      pomocnicza = head; 
      head = head->nast; 
     } 
    } 
    howManyRecords++; 

} 

而且,因爲我改變了這個函數的聲明,它應該被稱爲像這個:

addingNewItemToList(&headList, newItem); 
+0

我正在增加它,它的值爲3. – Darek

+0

@Darek,不,你不知道。當你從文件中添加記錄時 - 你可以正確地增加howManyRecords的數量,但是當你從鍵盤輸入插入新記錄時 - 你不會增加howManyRecords的數量,所以當你打印結果時,你不會在列表中打印最後一條記錄。 – Ryzhehvost

+0

非常感謝。有一件事,這麼多的問題:) – Darek

0

你應該通過調試器來運行你的代碼。有可能與指針有關。 例如,你可以用GDB很容易找到確切行導致了一個問題:

GDB your_executable

運行

回溯

F0(或任何其他號碼,看看其中部分錯誤發生的代碼)

它會給你導致段錯誤的行。 您可能必須使用-g進行編譯才能使其與調試器協同工作。

此外,我不知道是否需要使用指針,但標準庫有一些自動內存管理(std :: vector,std :: list等)的數據結構。 如果你不必顯式地使用手動分配(malloc),當你編寫C++來使用1)stl容器和2)新操作符時,如果你想自己管理內存,這是一個更好的做法。

+0

是的,我知道有圖書館,但我的老師要求使用基本結構來做到這一點-.- – Darek

相關問題