Queue using Class in C++

0

I have a problem with queue, with deleting first elements. Pointer "nastepca" should store address of next structure variable in a queue, but it stores nullptr for all of data in the structure and I cannot fixed it. I have tried many option but none of them worked. Is my queue works properly, is it put data in correct way, address alongside address before? dolacz() - means add/equeue zdejmij() - means delete/dequeue koniec - means end

#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();
    }


}

enter image description here While debugging: nastepca always have 0x00000000

c++
queue
asked on Stack Overflow Mar 29, 2020 by JaJaJacob

1 Answer

0
start = new Element(dane, start);

should be

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

When you add the first element, you should change the first and last pointers.

koniec = new Element(dane, koniec);

should be

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

When you add a new element (apart from the first) you need to make the old last element point to the new last element.

Pointer operations are tricky, you have to think carefully about what you are really doing. It might help to draw diagrams of the operations you have coded. That way you'd qucikly see that what you coded wasn't right.

answered on Stack Overflow Mar 29, 2020 by john

User contributions licensed under CC BY-SA 3.0