queue

Time:2022-1-2

queue

Basic concepts

Logical structure

definition

  • queue(queue): only in the tableone endInsert and delete at the other endLinear table
  • Team leader(front): the end of a linear table that allows deletion.
  • Team tail(rear): the end that allows insertion.
  • Empty queue: an empty table without any elements.

characteristic

First in first out(First In First Out,FIFO)

basic operation

  1. Initqueue:.
  2. Queueempty (q): judge whether queue q is empty, empty – > true.
  3. Enqueue (& Q, x): enter the stack. If the team is not full, add x to make it the head of the new team.
  4. Dequeue (& Q, & x): out of the stack, delete the queue header element and return it with X.
  5. Gethead (Q, & x): get the header element. If it is not empty, return with X.
  6. Destroy & queue: destroy the stack and free up the occupied storage space.

storage structure

Sequential storage
Sequential queue
  1. Sequential storage
  2. Use a groupAddress continuityThe data element is stored in the storage unit of, and two pointers front and rear are set at the same time to indicate the next position of the queue head element and the queue tail element respectively.
  3. False overflow
//Can only judge the team empty
QueueEmpty(){
  Q.front==Q.rear==0;
}
Circular queue
  1. Logically, it is regarded as a ring
//Initialization
void InitQueue(SqQueue &Q){
    Q.rear = Q.front = 0;
}
////////Sacrifice a unit, the air force is full/////////////
//Judge the team empty
bool isEmpty(SqQueue Q){
    if(Q.rear == Q.front) return true;
    else return false;
}

//Join the team
bool EnQueue(SqQueue &Q,ElemType x){
    //Team full
    if((Q.rear+1)%MaxSize==Q.front) 
        return false;
    Q.data[Q.rear]=x;
    //Modular operations logically turn the storage space into a 'ring'
    //End of line pointer backward
    Q.rear=(Q.rear+1)%MaxSize;
    //Sacrificing a storage unit
    //Number of elements in the queue: (q.rear-q.front + maxsize)% maxsize
    return true;
}

//Out of the team
bool DeQueue(SqQueue &Q,ElemType &x){
    //Team air
    if(Q.rear==Q.front)
        return false;
    //Get team leader
    x=Q.data[Q.front];
    //The queue head pointer moves back
    Q.front=(Q.front+1)%MaxSize;
    return true;
}

/////Add a data member representing the number of elements/////
//Team air:Q.size==0
//Team full:Q.Size==MaxSize
/////Add tag data member///////////
//Initialization:rear==front==tag==0;
//Delete: tag = 0;
//Insert: tag = 1;
//Team air:front==rear && tag==0
//Team full:front==rear && tag==1
Linked Storage
Chain queue
  1. With both queue head pointer and queue tail pointerSingle linked list
  2. The head pointer points to the head node and the tail pointer points to the tail node.
  3. It is suitable for the case of large changes in data elements without overflow.
  4. The single linked list of the leading node and the single linked list of the non leading node.
  5. When joining the team, the first element joins the team, and when leaving the team, the last element leaves the team.
////Leading node//////

//Initialization
void InitQueue(LinkQueue &Q){
    //Create header node
    Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next=NULL;
    
    //No lead node
    //Q.front = NULL;
    //Q.rear = NULL;
}

//Judge the team empty
//Return true when the queue is empty and false when the queue is full
bool IsEmpty(LinkQueue Q){
    //No lead node
    //if(Q.front==NULL)
    if(Q.front==Q.rear)
        return true;
    else return false;
    
}

//Join the team
void EnQueue(LinkQueue &Q,ElemType x){
    LinkNode *s=(LinkNOde*)malloc(sizeof(LinkNode));
    s->data=x;
    s->next=null;
    /*if(Q.front==null){
	Q.front=s;
	Q.rear=s;
}else{
	Q.rear->next=s;
	Q.rear=s;
}	*/
    Q.rear->next=s;
    Q.rear=s; 
}

//Out of the team
bool DeQueue(LinkQueue &Q,ElemType &x){
    if(Q,front==Q.rear) return false;
    LinkNode *p=Q.front->next;
    x=p->data;
    Q.front->next=p->next;
    //There is only one node in the queue
    if(Q.rear==p)
        Q.rear=Q.front;
    free(p);
    return true;
    /*LinkNode *p=Q.front;
    x=p->data;
    Q.front=p->next;
    if(Q.rear==p){
    	Q.front=null;
    	Q.rear=null;
    }*/
}
Double ended queue

A queue that can be queued in and out at both ends.

Linear structure.

It is divided into front end and back end. Both ends can join and leave the team.

Double ended queue with limited output and double ended queue with limited input. Elements inserted from an endpoint can only be deleted from that endpoint: two stacks connected at the bottom of the stack.

A valid output sequence in the stack must be valid in the double ended queue.

Data operation

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters:https://www.cnblogs.com/xiegongzi/p/15782921.html Link to component development:https://www.cnblogs.com/xiegongzi/p/15823605.html 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]