簡體   English   中英

在 C++ 中使用類的隊列

[英]Queue using Class in C++

我有隊列問題,刪除第一個元素。 指針“nastepca”應該在隊列中存儲下一個結構變量的地址,但它為結構中的所有數據存儲 nullptr,我無法修復它。 我嘗試了很多選擇,但沒有一個奏效。 我的隊列是否正常工作,它是否以正確的方式放置數據,之前的地址和地址? dolacz() - 意味着添加/入隊 zdejmij() - 意味着刪除/出隊 koniec - 意味着結束

#include <iostream>
#include <string>

using namespace std;
template<typename T>
class Kolejka
{
    struct Element
    {
        T dane;
        Element* nastepca;
        Element(const T& dane, Element* nastepca) : dane(dane), nastepca(nastepca) {}
    };

    Element* start = nullptr; //pusty wskaznik nullptr
    Element* koniec = nullptr; //pusty wskaznik nullptr
    int licz_elementow = 0;

public:
    void dolacz(const T& dane)
    {
        
        if (start == nullptr) //jezeli kolejka jest pusta to dodaj na poczatek (poczatek i koniec jest ten sam)
        {
            start = new Element(dane, start);
            licz_elementow++;
        }
        else
        {
            ss++;
            koniec = new Element(dane, koniec);
            licz_elementow++;
        }
        
        
    }
    void zdejmij() //nie działa
    {
        if (start == koniec)
        {
            start = koniec = nullptr;
            licz_elementow--;
        }
        else
        {
            Element* tmp = start;
            start = start->nastepca;
            delete tmp;
            licz_elementow--;
        }
        
    }

    T& gora() //zwroci referencje typu T na początek kolejki
    {
        if (koniec == nullptr)
        {
            throw runtime_error("Pusta kolejka!");
        }
        return start->dane;
    }

    T& tyl() //zwroci referencje typu T na koniec kolejki
    {
        if (koniec == nullptr)
        {
            throw runtime_error("Pusta kolejka!");
        }
        return koniec->dane;
    }

    int rozmiar()
    {
        return licz_elementow;
    }

    bool pusty()
    {
        return start == nullptr;
    }
    
};

class Auto
{
    string marka;
    friend ostream& operator <<(ostream& w, Auto& a);  //zaprzyjaźniona funkcja przeciążająca operator <<
public:
    Auto(string marka): marka(marka) {}
};
ostream& operator <<(ostream& w, Auto& a)
{
    w << a.marka;
    return w;
}
int main()
{
    Kolejka <Auto> kol;
    try
    {
        kol.dolacz(Auto("aaa"));
        kol.dolacz(Auto("bbb"));
        kol.dolacz(Auto("ccc"));
        kol.dolacz(Auto("ddd"));
        kol.zdejmij();
        cout << "Liczba elementow: ";
        cout << kol.rozmiar();
        cout << endl;
        cout << "Poczatek kolejki: ";
        cout << kol.gora();
        cout << endl;
        cout << "Koniec kolejki: ";
        cout << kol.tyl();

        
        

    }
    catch (runtime_error& BLAD)
    {
        cout << BLAD.what();
    }
        
    
}

在此處輸入圖像描述調試時:nastepca 總是有 0x00000000

start = new Element(dane, start);

應該

start = koniec = new Element(dane, nullptr);

添加第一個元素時,應更改第一個最后一個指針。

koniec = new Element(dane, koniec);

應該

Element* temp = new Element(dane, nullptr);
koniec->nastepca = temp;
koniec = temp;

當您添加一個新元素(除了第一個)時,您需要使舊的最后一個元素指向新的最后一個元素。

指針操作很棘手,你必須仔細考慮你真正在做什么。 繪制您編碼的操作的圖表可能會有所幫助。 這樣你很快就會發現你編碼的東西是不正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM