2020.1.10 queue (analog array)

queue

Brief introduction

  • Queue "FIFO", similar to life on a first-come first buy, then buy later.
  • Team head (front), the tail (rear).

Queue states:

  1. Air Force: the queue has no elements.
  2. Full team: queue space have all been occupied.
  3. Overflow: overflow (overflow), overflow (underflow).

First, the basic operation of the queue.

Initialization 1.
Initialization state front = rear = 0, i.e. there is no queue element.

void clear(){
    front = rear = 0;
}

2. Empty sentence

bool empty(){
    if (front == rear)  return 1;
    else  return 0;
}

3 actually required number of elements in the queue

int size(){
    return (rear - front);
}

4. into the team

void push(int x){
    q[++rear] = x;
}

5. a team

void pop(){
    front++;
}

6. Take the first team element

int get_front(){
    return q[front + 1];
}

Second, cyclic queue

Brief introduction

  • Let arrays end to end to form a "ring" shape, i.e., so-called "circular queue."
  • When the initial circular queue, front = rear = 0, assuming a one MAXN elements enqueued when the rear = maxn, then there queued elements, it will be stored in q [0] of this unit, but also appear front = rear = 0, and the state of the same space-time team.

Important operating cycle queue

The following are used q [0] of this unit.

  1. Analyzing team full :( rear + 1)% maxn = front, the queue is full.
  2. Enqueue: If the queue is not full, the implementation of "rear = (rear + 1)% maxn; q [rear] = x".
  3. Dequeue: If the queue is not full, the implementation of "front = (front + 1)% maxn"

After the queue is convergence on the head of the queue, the first team squad, the team will head into alignment

Published 11 original articles · won praise 18 · views 1935

Guess you like

Origin blog.csdn.net/acm_durante/article/details/103924032