Non-STL Solution to Queue Problem

Posted by nicx on Sat, 27 Jul 2019 14:43:30 +0200

Perennial use of STL to solve queue problems, so that serious alienation of the basic principle of queue...
Until today, when the teacher was forced to use the algorithmic principle to solve the problem, I realized that I didn't know anything about queues...
.... It was not until after a series of abductions that some clues were sorted out, in this note.
Concepts will not be repeated here, because this is not a lot of things to search for.

Loop Queue-Array Storage Scheme:

What we need:

1. An array to store the queue.

int ique[capa];//capa is the maximum capacity of an array (queue). See below_

2. Head and tail pointers, indicating the head and tail of the queue.

int ihead=0,itail=0;

3. Two more quantities -- the current length of the queue (as a variable) and its maximum capacity (as a constant, defined at the time of opening the array, are opened shorter here for demonstration, only to 4)

int ilen=0;
const int capa=4;

Next are some basic operations:

1. Bring elem into the team:

int elem;
ique[itail]=elem;//The tail pointer always points to the next position at the end of the queue, so we just need to change the number at the tail pointer.
itail++;//The tail pointer increases and points to the next position.
itail=itail % capa;//Circle key points, see below_
ilen++;//Because there is one more element and the length increases, the ilen increases by itself.

itail=itail % capa;

We've been increasing the tail pointer, and the length of the array is only 4. In this way, we can achieve the circular entry through redundancy. In this way, when the tail pointer increases beyond capa, the tail pointer can automatically jump back to the beginning of the array through this statement, so as to realize cyclic reading and cyclic storage.

Here the tail pointer always points to the next position at the end of the queue, so that it points to the element at the end of the queue.

2. Get out of the team and deposit it in elem:

int elem;
elem=ique[ihead];//Remove the elements directly from the header pointer
ihead++;//Head pointer moves down
ihead=ihead % capa;//Same as above
ilen--;//Because there is one element missing and the length decreases, the ilen decreases by itself.

The key operation is to make the pointer redundant to capa, which can make the pointer form a loop and realize the cyclic storage of the queue. In other places, if we need to use circular storage, it's a good idea to redundant its capacity. Of course, the size of practical application is not only 4 so small, we should also pay attention to the reasonable arrangement of data scope space, or there are too many problems or empty is very difficult.

This is the basic operation (is it simple) (is it somewhat difficult to understand) (or is it rather simple to be said by me.)

For instance:

Luogu P1996 josephus problem

Title:

N individuals (n <= 100) form a circle. They start counting from the first person, count to m and then start counting again from the next person, count to m and then go out of the circle. By analogy, until all the people are out of the circle, please output the number of the people out of the circle in turn.

Input format

n m

Output format

Number of Out of Circle

sample input

10 3

sample output

3 6 9 2 7 1 8 5 10 4

Explain

m,n≤100

With STL, you don't need to use your brain. You can do it in 5 minutes. As follows:

#Include < queue >// Header files necessary for queues
#include<cstdio>
using namespace std;
queue <int> set;//Create the queue
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        set.push(i);//Enter the number of each person into the team and prepare for the report.
    }
    int counti=1;//Counter for counting
    while(!set.empty())
    {
        if(counti!=m)
        {
            set.push(set.front());//Instead of m, the loop pushes the head of the team into the end of the team to realize the cycle.
            set.pop();
        }
        if(counti==m)
        {
            printf("%d ",set.front());//It's the man m who comes out and outputs his number.
            set.pop();
            counti=1;//Start from 1 again
            continue;//Continue
        }
        counti++;
    }
    return 0;
}

Without STL, it would be a little more cumbersome to build queues, but it would be better to understand their principles and applications. However, STL is only a tool in any case, we should master the principle of its implementation. The following is a solution without STL:

//luogu.org P1996 -- NO STL Solution
//queue--Array Solution

//IZWB003-2019/07/24

#include<cstdio>

//set queue // set the queue
int head=0,tail=0;//Pointer 2
int length=0;const int capacity=120;//Size and length
int queue[capacity];//Store queues through arrays
int element;

using namespace std;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        queue[tail]=i+1;//Enter the team at the tail pointer
        tail++;
        tail=tail%capacity;//The above method
        length++;//Length change
    }
    int i=0;//Counter with i as count
    while(true)
    {
        element=queue[head];//Out of the line at the head pointer
        head++;
        head=head%capacity;//The above method
        length--;//Length change
        i++;//Number off
        if(i==m)//The place of departure
        {
            printf("%d ",element);//output
            i=0;//Reporting
        }
        else//If not, then join the team.
        {
            queue[tail]=element;
            tail++;
            tail=tail%capacity;
            length++;
        }
        if(length==0) break;//Team air is over
    }
    return 0;
}

If you have time to build queues, you can add it later.

Topics: PHP