Answer the question
In order to leave comments, you need to log in
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&);
};
//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;
}
if(front==nullptr)front = add;
else rear->next = add;
rear = add;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question