gpt4 book ai didi

c++ - 列表丢失了列表 C 中的最后一项

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:23 24 4
gpt4 key购买 nike

这段代码有问题.....阅读方法工作正常,但当我想向我的单向列表添加新项目时,一切都“崩溃”,并且添加此项目依赖于一切按升序排序。文件中的项目按快捷方式升序排序。它有效但不完全,我什至在纸上画出所有想法以遵循我的代码,我不知道为什么当我试图在屏幕上打印所有列表时它会丢失列表中的最后一项。请帮我解决这个问题。下面的代码混合了 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

那个程序有什么问题....?

最佳答案

当您添加新记录时,您不会在 dodawanieDoListy 中递增 ileRekordow。(或者这是新代码中 addingNewItemToList 中的 howManyRecords)

这是示例,它应该是这样的:

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);

关于c++ - 列表丢失了列表 C 中的最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com