tags: algorithm
Queue, also known as queue, is a linear table of FIFO, First-In-First-Out. In specific applications, it is usually implemented with linked lists or arrays. The queue only allows insert operations at the back end (called rear) and delete operations at the front end (called front).
The operation of the queue is similar to that of the stack. The only difference is that the queue only allows new data to be added in the backend.
-Source: Wikipedia

element follows first-in-first-out (similar to the situation of queuing in supermarkets in life)
The operation of element satisfies: enter the queue from the end of the line, leave the queue from the head of the line
template<typename DataType>class Queue
{
public:
Queue(int size)
{
maxSize = size;
front = 0;
rear = 0;
count = 0;
elements = new DataType[maxSize];
//If space allocation fails, exit the program directly
if(elements==NULL)
exit(1);
}
~Queue()
{
delete [] elemenets;
}
bool insert(DataType data);
DataType pop();
bool isempty();
bool isfull();
DataType getF();
DataType getR();
private:
int count;
int maxSize;
int front;//The top position of the team
int rear;//The tail position
DataType *elements;
};
The basic operations of the queue include entering the team, leaving the team, judged empty, judged full, return to the head element, and return to the end element;
template<typename DataType>bool Queue<DataType>::insert(DataType data)
{
if(count==maxSize)
return false;
elements[rear]=data;
rear=(rear+1)%maxSize;//Circular queue, save space
count++;
return true;
}
template<typename DataType>DataType Queue<DataType>::pop()
{
if(count==0)
exit(1);
DataType temp=elements[front];
front=(front+1)%maxSize;
count--;
return temp;
}
template<typename DataType>bool Queue<DataType>::isempty()
{
if(count==0)return true;
return false;
}
template<typename DataType>bool Queue<DataType>::isfull()
{
if(count==maxSize)return true;
return false;
}
template<typename DataType>DataType Queue<DataType>::getF()
{
if(count==0)
exit(1);
return elements[front];
}
template<typename DataType>DataType Queue<DataType>::getR()
{
if(count==0)
exit(1);
return elements[rear];
}
Without considering the complexity of the program, how do you use the stack to implement queue operations?
answer:
The characteristic of the stack is first-in-last-out (FILO)
The queue is characterized by first in first out (FIFO)
The first-in-last-out feature of the stack has been changed to realize the first-in-first-out of elements
Set up two stacks, one is responsible for the enqueue operation, when you want to leave the queue, the elements in the stack of the enqueue operation will be popped out of the stack, and once into the second stack, then the second stack will be queued Popping, that is, after dequeuing, the elements in the second stack are returned to the first stack

Reference book "Interesting Algorithm (C++ Language Implementation)"
In the previous article, we mentioned how the Activity handles when the screen size changes. There are two types: Restart the APP to adapt to the screen change; Manually process data to avoid APP rest...
Overview: Do some preparations for learning Boostrap This chapter involves content: Download and customize Boostrap Bootstrap file structure Boostrap application analysis Develop the first Boostrap ex...
Note: Return can be used to end methods NexTline cannot be used after NEXTINT, because nextline will skip NextLine after NEXTINT. In order to solve this problem, you can add more next to nextint. Arra...
CUDA Thread execution model analysis (2) the army did not move grain and grass first ------GPURevolution Preface: Today may be a relatively unsatisfactory day, from the first call in the morning to th...
table of Contents 1. The application of stack in bracket matching 2. The application of stack in expression evaluation (part 1) 3. The application of stack in expression evaluation (below) 4. The appl...
1. The string in the Go language can only use double quotes "," and cannot use single quotes. (Other languages can be) 2. Single quota package called characters, not string Then the charac...
Like the bidirectional stack, it looks like this is dynamically allocated, but in fact it is static, and a fixed-size MAXQSIZE space is allocated. Because when the static queue is full, realloc needs ...
The queue is constructed as an ordered set of items added at the end of the line and removed from the head of the line. The queue maintains FIFO ordering properties. The queue operation is as follows....
Quest: advanced first, complexity in and out of time o (1), find O (n) Stack: Later One: Use a queue to implement a stack Please use only two queues to achieve one stack in the first -mounted (LIFO), ...
3.4 Create your first AI: the weakest AI imaginable After implementing the Go board and game state classes, you can build your first Go AI. This robot will be a very weak player, but it will lay the f...