Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Symulacja kolejki "first in first out".

Ostatnio zmodyfikowano 2017-02-11 06:43
Autor Wiadomość
Mateusz99
Temat założony przez niniejszego użytkownika
Symulacja kolejki "first in first out".
» 2017-02-11 03:43:55
j.w
Mam dylemat co do metody dodającej osobę do kolejki. Chyba nie do końca rozumiem sens istnienia instrukcji else.
Czy nie jest tak, że mimo przez przypisanie add do rear->next zostanie to zamazane przez wykonanie rear = add? Zdaje sobie sprawę, że add jest tu adresem ale mimo tego wciąż mam z tym straszny kłopot.
Poniżej załączam kod klasy.
klasa:
C/C++
typedef std::string Item;
class Queue
{
private:
    enum { Q_SIZE = 10 };
    struct Node
    {
        Item item;
        struct Node * next;
    };
    Node * front;
    Node * rear;
    int items;
    const int qsize;
public:
    Queue( int qs = Q_SIZE );
    ~Queue();
    bool isempty() const;
    bool isfull() const;
    int queuecount() const;
    bool enqueue( const Item & item );
    bool dequeue( Item & item );

i ta konkretna metoda:
C/C++
bool Queue::enqueue( const Item & item )
{
    if( isfull() )
         return false;
   
    Node * add = new Node;
    add->item = item;
    add->next = nullptr;
    items++;
    if( front == nullptr )
         front = add;
    else
         rear->next = add;
   
    rear = add;
   
    return true;
}
P-157598
mateczek
» 2017-02-11 06:43:24
rear->next zostanie to zamazane przez wykonanie rear = add?

C/C++
if( front == nullptr )
     front = add; // kolejka jest pusta utwórz pierwszy elemet
else
     rear->next = add; //istnieje już element więc nowo tworzony przypisz na końcu

rear = add; // a teraz zaktualizuj ostatni element w kolejce
P-157599
« 1 »
  Strona 1 z 1