S
S
sddvxd2018-04-02 15:36:08
C++ / C#
sddvxd, 2018-04-02 15:36:08

Can you please explain the meaning of strings?

Hello! I make a queue class according to the example

//head.h

//Any classes....
class Queue{
private:
  struct Node
  {
    String str; //Мой класс
    Node *next;
  };
  enum{
    Q_SIZE = 10
  };
  int items;
  const int qsize;
  Node *front,*rear;
public:
  Queue(int);
  bool isfull()const;
  bool isempty()const;
  bool enqueue(const String&);
};

The object can add to the queue, check for fullness and emptiness
//func.cpp

//Any methods...
Queue::Queue(int qs = Q_SIZE):qsize(qs),front(nullptr),rear(nullptr),items(0){}
bool Queue::isfull()const{
  if(items==qsize)return true;
  else return false;
}
bool Queue::isempty()const{
  if(items==0)return true;
  else return false;
}
bool Queue::enqueue(const String &st){
  if(isfull())return false;
  Node *add = new Node;
  add->str = st;
  add->next = nullptr;
  items++;
  if(front==nullptr)front = add;
  else rear->next = add;
  rear = add;
  cout<<st<<" posted in "<<add<<" end has "<<items<<" position\n";
  return true;
}

It’s not entirely clear to me why first write the address of the next structure in the structure field, and then assign the address of the new structure to the end of the queue
if(front==nullptr)front = add;
  else rear->next = add;
  rear = add;

There are no such "terrible" indents in the book. I wrote my own

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JaxxDexx, 2018-04-02
@sddvxd

Very simple! When you add a new element to your queue, rear points to your last element in the queue. Its next is set to nullptr, so in order not to lose the connection between the last element in the queue and the one you are adding, you set the rear->next pointer to the address of the new element. And after that, tell your entire queue that the new element now has the address add (rear = add).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question