1. Common basic operations and condition judgment of sequential queues
队空: Q.front=Q.rear
队满: Q.rear=Maxlen
求队长: Q.rear-Q.front
Enqueue : 1) The new element is added at the position indicated by rear
2) rear = rear + 1 the tail pointer of
the team plus one out of the team : 1) the element indicated by the front is taken out.
2) front = front + 1 team head pointer plus one
2. Type definition of sequential queue
#define MAXLEN 100
typedef struct
{datatype Q[MAXLEN];
int front=0;
int rear=0;
} SeqQueue,*P;
Q: What is "fake overflow"? How to solve?
Answer: In the sequential team, when the tail pointer has reached the upper bound of the array, there can be no more enqueue operations, but there are actually empty positions in the array, which is called "false overflow".
Ways to Solve False Overflow——Adopt Cycling Queue
Circular queue
Think of it as a ring connected end to end, and call this vector a circular vector, and the queue stored in it is called a circular queue.
New problem: in the circular queue, the empty team is characterized by Q.front = Q.rear; when the team is full, there will also be Q.front = Q.rear; the judgment conditions will appear ambiguous!
There are three solutions:
① use a counter Record the number of elements in the queue (that is, the length of the queue);
②Add a flag bit, set 1 when deleting, and set 0 when inserting, then you can identify what kind of situation Q.front = Q.rear belongs to
③ artificially wasting a unit, then The team full feature can be changed to Q.front = (Q.rear + 1)% MAXLEN;
Scheme 3 is often used in practice (a unit is wasted artificially):
one of front and rear points to the real element, and the other points to the idle element.
队空条件 : Q.front =Q. rear (初始化时:Q.front = Q.rear )
队满条件: Q.front = (Q.rear+1) % N (N=maxsize)
队列长度(即数据元素个数):L=(N+Q.rear-Q.front)% N
Example 1: The array Q [n] is used to represent a circular queue, f is the previous position of the head element of the current queue, and r is the position of the tail element. Assumed that the queue number of elements less than n, calculated as the queue element
is: A) R & lt-F (B) (n-+ F-R & lt)% n-
(C) n-+ R & lt-F (D) (n-+ R & lt-F)% n-
To Which of the four formulas is the most reasonable?
When r ≥ f, (A) is reasonable;
when r <f, (C) is reasonable; combining two situations, the expression (D) is the most reasonable.
Example 2: In a circular queue, if the team head pointer is agreed to point to the team head The previous position of the element. Then deleting an element from the circular queue, the operation is to move the pointer head of the queue , the removed element .
How to form a circular queue?
When performing dequeue and enqueue operations in the circular queue, the head and tail pointers still need to increase by 1 and move forward.
It's just that when the head-to-tail pointer points to the upper bound of the vector (MAXLEN-1), the result of adding 1 is to point to the lower bound of the vector, 0.
The result of adding 1 is the way to point to the lower bound of the vector, 0
*(front+1)%M (rear+1)%M*
Team empty: Q.front = Q. rear
Team full: Q.front = (Q.rear + 1)% MAXLEN Enqueue
: Q.rear = (Q.rear + 1)% MAXLEN Dequeue
: Q.front = ( front + 1)% MAXLEN;
seeking the captain: (Q.rear-Q.front + MAXLEN)% MAXLEN
-3) Type definition of circular queue
#define MAXLEN 100
typedef struct
{datatype *data[MAXLEN];
int front;
int rear;
int n;/*判队空,队满的另一方法*/
} CseqQueue
-4) Implementation of circular queue operation
① Initialize the queue
CseqQueue * IniQueue (CseqQueue *Q)
{ //构造空队列
Q= (CseqQueue *) malloc(sizeof(CseqQueue ));
Q->rear = Q->front = 0;
return Q;
}
②The team is empty
int QueueEmpty (CseqQueue *Q )
{
return(Q->rear == Q->front);
}
③The team is full
int QueueFull (CseqQueue *Q )
{
return((Q->rear+1) % MAXLEN == Q->front);
}
④The team is full
int QueueFull (CseqQueue *Q )
{
return( (Q->n) == MAXLEN);
}
⑤Entry
int InQueue (CseqQueue *Q, datatype x )
{
if (QueueFull (Q) )
return 0;
else
{Q->data[Q->rear] = x;
Q->rear = ( Q->rear+1) % MAXLEN;
Q->n++;
return 1;
}
}
⑥Leaving the team
int DeQueue (CseqQueue *Q, datatype *x )
{
if ( QueueEmpty (Q) )
return 0;
else
{*x = Q->data[Q->front];
Q->front = ( Q->front+1) % MAXLEN;
Q->n--;
return 1;
}
}
⑦ Take the team head
int GetFront (CseqQueue *Q, datatype *x )
{
if ( QueueEmpty (Q) )
return 0;
*x = Q->data[Q->front];
return 1;
}
⑧ Find the queue length
int QueueLength (CseqQueue *Q)
{
return( (Q->rear – Q->front + MAXSIZE) % MAXSIZE);
}
Chain queue structure
**
Schematic diagram of chain queue
(2) Description of chain queue
Similar to sequential queue, we also encapsulate these two pointers together and define the type of link queue LinkQueue as a structure type:
typedef struct queuenode
{ datatype data;
struct queuenode *next;
}queuenode;
typedef struct
{ queuenode *front;
queuenode *rear;
}Linkqueue;
3) Basic operations implemented on the chain queue
1) Construct an empty queue (headed node empty queue)
void initqueue(Linkqueue *q)
{ q.front=(queuenode *)malloc(sizeof(queuenode));
q.rear=q.front
q.front->next=q.rear->next= NULL;
}
2) Enqueue operation
Enqueue operation algorithm
void inqueue(Linkqueue *q,datatype x)
{queuenode *p
p=(queuenode * )malloc(sizeof(queuenode));
p–>data=x;
p–>next=NULL;
q.rear–>next=p;
q.rear=p;
}
3) Judgment of queue
int queueempty(Linkqueue *q)
{
return (q.front->next= =NULL &&
q.rear->next= =NULL);
}
- Dequeue operation
4) Dequeue operation algorithm
Datatype dequeue (Linkqueue * q)
{datatype x;
queuenode * p
if (queueempty (q))
{printf ("Queue is empty"); return;}
p = q.front–> next;
x = p–> data;
q.front–> next = p–> next;
if (q.rear = = p) / delete the tail node /
q.rear = q.front;
free§;
return x;
}
If you have any questions, please leave a message and amend it in time