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

时间:2014-03-25 08:59:10

标签: c++

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

该计划有什么问题......?

2 个答案:

答案 0 :(得分:1)

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

这是一个例子,应该如何:

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

答案 1 :(得分:0)

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

  

gdb your_executable

     

运行

     

回溯

     

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

它会为您提供导致段错误的行。 您可能必须使用-g进行编译才能使其与调试器一起使用。

此外,我不知道是否需要使用指针,但标准库有一些带有自动内存管理的数据结构(std :: vector,std :: list等)。 如果您不必明确使用手动分配(malloc),那么在编写c ++以使用1)stl容器和2)新操作符时,如果您想自己管理内存,则更好。