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

C++ 列表丢失列表C中的最后一项,c++,C++,我对这个代码有一个问题。。。。。Reading方法可以很好地工作,但当我想将新项目添加到单向列表中时,所有内容都会“崩溃”,并且添加此项目依赖于所有内容都是按升序排序的。文件中的项目按升序排序,以便进行快捷操作。它可以工作,但不能完全工作,我甚至按照我的代码在纸上画出所有的墨水,我不知道为什么当我试图在屏幕上打印所有列表时,它会丢失列表中的最后一项。请帮我解决那个问题。下面的代码与C和C++混合。 #include <iostream> #include <stdio.h>

我对这个代码有一个问题。。。。。Reading方法可以很好地工作,但当我想将新项目添加到单向列表中时,所有内容都会“崩溃”,并且添加此项目依赖于所有内容都是按升序排序的。文件中的项目按升序排序,以便进行快捷操作。它可以工作,但不能完全工作,我甚至按照我的代码在纸上画出所有的墨水,我不知道为什么当我试图在屏幕上打印所有列表时,它会丢失列表中的最后一项。请帮我解决那个问题。下面的代码与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;
}

该程序有什么问题?

您应该通过调试器运行代码。很可能这与指针有关。 例如,您可以非常轻松地使用gdb查找导致问题的确切线路:

gdb您的_可执行文件

回溯

f0(或任何其他数字,以查看错误发生在代码的哪个部分)

它会给你造成故障的线路。 您可能必须使用-g进行编译,才能使其与调试器一起工作

另外,我不知道是否需要使用指针,但标准库有一些自动内存管理的数据结构(std::vector、std::list等)。
如果您不必使用手动分配(Maloc),那么如果使用C++(1)STL容器和2),如果您想自己管理内存,则最好是使用新的运算符。

< P>在添加新记录时,不要在<代码> doDaWiNeDeLoisty < /C>中增加代码> ILRekordOW < /C> >。 (或者这是
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++;

}

只是一个注释:不要使用波兰语标识符名称。这可能听起来有点自鸣得意,但为了提高你获得帮助的机会——用英语写代码:)我很难跟上流程,因为变量名对我来说是胡言乱语。那是什么-波兰语?:)当然,给我一分钟重写它。在
dodawanieDoListy
function
head=poczatek->nast可以为空。。然后你检查
head->rok_produkcji
,这会让你崩溃。同样的问题也可能发生在while循环的下一次迭代中。我也知道,但我真的不知道为什么它会丢失(而不是崩溃)列表的最后一项。我想请这里的任何人帮我解决这个问题。代码被重构了。是的,我知道有库,但我的老师要求用基本结构来重构。-我在递增它,它的值是3。@Darek,不,你没有。当你们从文件中添加记录时——你们正确地增加了howManyRecords,但当你们从键盘输入中插入新记录时——你们不会增加howManyRecords,所以当你们打印结果时——你们不会打印列表中的最后一条记录。非常感谢你们。有一件事和这么多问题:)@Darek,我为你添加了一个正确功能的示例。
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);