我有這個代碼的問題.....閱讀方法工作正常,但當我想添加新項目到我的單向列表中並添加此項時,一切都「崩潰」項目依賴於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
什麼是錯的程序....?
只是一個註釋:不要使用波蘭語標識符名稱。它非常不鼓勵非拋光型揚聲器甚至看代碼。 – fritzone
這可能聽起來有些沾沾自喜,但爲了提高獲得幫助的機會 - 用英文編寫代碼:)我很難跟蹤流,因爲變量名對我來說很亂。那是什麼 - 波蘭語? :) –
肯定給我一分鐘重寫它。 – Darek