[Postgraduate entrance examination data structure] Chapter 3 Basic knowledge and operations of queues

Article directory

Basic concepts of queues

Queue sequential storage

sequential queue

 storage type

Basic operations

sequential queue

 storage type

Basic operations

Three solutions for empty and full detection of circular queues  

Method 1: Sacrifice a storage unit

Method 2: Add a record variable size to the type

Method 3: Add a flag variable tag to the type

Queue chain storage

Storage type of chain team

Basic operations of chain team

initialization

Call it short

Join the team

Dequeue

deque

Summarize


Basic concepts of queues

        Queue, referred to as queue, is a linear table with limited operations . Only insertions are allowed at one end of the table and deletions at the other end. Inserting elements into the queue is called enqueuing or enqueuing , and deleting elements is called dequeuing or leaving the queue . Similar to queuing in life, it has the characteristics of First In First Out.

Related terms

  • Front: The end that is allowed to be deleted, also known as the front
  • Rear: the end that allows insertion
  • Empty queue: an empty list without any elements

Basic operations

  • InitQueue(&Q): Initialize the queue
  • QueueEmpty(Q): Determine whether the queue is empty
  • EnQueue(&Q,x): Enter the queue. If the queue is not full, add x to the queue, which is called the new tail of the queue.
  • DeQueue(&Q,&x): Dequeue, if the queue is not empty, delete the head element and store it in x and return
  • GeyHead(Q,&x): Read the head element of the queue. If the queue Q is not empty, assign the head element to x

Queue sequential storage

sequential queue

        The sequential implementation of the queue refers to allocating a continuous storage unit to store the elements in the queue, and attaching two pointers: the queue head pointer front points to the queue head element, and the queue tail pointer rear points to the next position of the queue tail element (it can also point directly to The elements at the end of the team will be analyzed in detail according to the specific situation of the question ).

 storage type

//顺序队列的存储类型
#define MaxSize 50
typedef struct{
	ElemType data[MaxSize];
	int front,reat;
}SqQueue; 

Basic operations

  • Initial state (team empty state): Q.front==Q.rear==0
  • Operation of entering the team: When the team is not full, first send the value to the end element of the team, and then add 1 to the end pointer of the team.
  • Dequeue operation: When the queue is not empty, first take the value of the queue head element, and then add 1 to the queue head pointer.


sequential queue

         The sequential queue is imagined as a ring- shaped space, that is, the table that stores the queue elements is logically regarded as a ring , which is called a circular queue . When the team head pointer Q.front=MaxSize-1 , it will automatically be 0 if it advances one position. This can be achieved by using the division remainder operation (%) .

 storage type

//循环队列的存储类型
#define MaxSize 50
typedef struct{
	ElemType data[MaxSize];
	int front,reat;
}SqQueue; 

Basic operations

  • Initialization: Q.front=Q.rear=0
//队列的初始化
void InitQueue(SqQueue &Q){
	Q.rear=Q.front=0;//初始化队尾与队首指针 
} 
  • Short judgment: Q.rear==Q.front
//判断队空
bool isEmpty(SqQueue Q){
	if(Q.rear==Q.front)return true;//队空的条件 
	else return false;//队非空 
} 
  • Dequeue: Q.front=(Q.front+1)%MaxSize
//出队
bool DeQueue(SqQueue &Q,int &x){
	//如果队空,则出队失败 
	if(Q.rear==Q.front){
		return false;
	}
	x=Q.data[Q.front];
	Q.front=(Q.front+1)%MaxSize;
	return true; 
} 
  • Enqueue: Q.rear=(Q.rear+1)%MaxSize
//入队
bool EnQueue(SqQueue &Q,int x){
	//如果队满,则入队失败 
	if((Q.rear+1)%MaxSize==Q.front){
		return false;
	}
	Q.data[Q.rear]=x;
	Q.rear=(Q.rear+1)%MaxSize;
	return true;
} 
  • Queue length: (Q.rear+MaxSize-Q.front)%MaxSize
//求队列的长度 
int getLength(SqQueue Q){
	//如果队空,则返回0 
	if(Q.rear==Q.front){
		return 0;
	}
	return (Q.rear+MaxSize-Q.front)%MaxSize;//返回队列长度 
} 

When entering or leaving the queue, the pointer advances by 1 in a clockwise direction, as shown in the following figure:

Three solutions for empty and full detection of circular queues  

As shown in the figure above, the condition for the team to be empty is Q.front==Q.rear, but we found a problem: when the speed of entering the team is faster than the speed of leaving the team , the tail pointer of the team will soon catch up with the head of the team . Pointer, as shown in d1 in the figure above, there is also the condition Q.front==Q.rear when the queue is full. In this way, it is impossible to truly distinguish whether the circular queue is empty or full. There are three solutions for this:

Method 1: Sacrifice a storage unit

Sacrifice one unit to distinguish between empty and full queues, that is, use one less queue unit         when joining the queue . It is agreed that " the position of the queue head pointer and the queue tail pointer is used as the queue full mark ", as shown in d2 in the above figure.

After using this method:

  • Team full condition: (Q.rear+1)% MaxSize==Q.front
  • Team empty condition: Q.rear==Q.front
  • Number of queue elements: (Q.rear-Q.front+MaxSize)%MaxSize

Method 2: Add a record variable size to the type

        Add a new member variable size that represents the number of elements to the storage type of the queue , so that the condition for the queue to be empty is Q.size==0 ; the condition for the queue to be full is Q.size=MaxSize

Method 3: Add a flag variable tag to the type

Add a new tag member variable         to the queue's storage type to distinguish between empty and full queues. When tag==0 , if Q.rear==Q.front is caused by deletion, the queue is empty ; when tag==1 , if Q.rear==Q.front is caused by deletion, the queue is full ;

 


Queue chain storage

The chain storage         of queues is called chain queue, or chain queue for short . It is a singly linked list with both a head pointer and a tail pointer . The head pointer points to the head node of the queue, and the tail pointer points to the tail node of the queue, which is the last node of the singly linked list.

 

Storage type of chain team

#define MaxSize 50
//链队中的结点元素 
typedef struct LinkNode{
	int data;
	struct LinkNode *next;
}LinkNode;
//链队 
typedef struct{
	LinkNode *front,*rear;//队列的队头指针与队尾指针 
}LinkQueue;

Basic operations of chain team

initialization

//链队的初始化
void InitQueue(LinkQueue &Q){
	Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));//建立头结点
	Q.front->next=NULL;//初始为空 
} 

Call it short

//判断队空
bool isEmpty(LinkQueue Q){
	if(Q.rear==Q.front)return true;//队空的条件 
	else return false;//队非空 
} 

Join the team

//入队
bool EnQueue(LinkQueue &Q,int x){
	//创建新结点 
	LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode)); 
	s->data=x; s->next=NULL;
	//将新结点插入到链尾,类似单链表的尾插法 
	Q.rear->next=s;
	Q.rear=s;
	return true; 
} 

Dequeue

//出队
bool DeQueue(LinkQueue &Q,int &x){
	//如果队空,则出队失败
	if(Q.rear==Q.front){
		return false;
	} 
	LinkNode *p=Q.front->next;//新建一个临时结点存储当前队头元素,即待删除的元素 
	x=p->data;//将需要删除的元素值存储进x并返回 
	Q.front->next=p->next;//修改头结点的指针域,使其指向原来链表的第二个结点,达到逻辑上删除队头结点的效果
	//若原链队中只有一个结点,则删除后将变空,需要修改队头与队尾指针使二者相等达到队空的条件 
	if(Q.rear==p){
		Q.rear=Q.front;
	} 
	free(p);//释放待删除结点的内存空间,达到物理上真正的删除 
	return true;
} 

deque

        A double-ended queue refers to a queue that allows both ends to perform enqueue or dequeue operations. The logical structure of its elements is still a linear structure . The two ends of the queue are called the front end and the back end respectively . Both ends can perform queue entry and dequeue operations.

 Double-ended queues are often examined in the form of multiple-choice questions, which do not require high coding requirements. Interested students please refer to the following article.

C language to implement double-ended queue icon-default.png?t=N3I4https://blog.csdn.net/Quarrie/article/details/104402199?ops_request_misc=&request_id=&biz_id=102&utm_term=%E6%95%B0%E6%8D%AE%E7%BB%93 %E6%9E%84%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97C%E8%AF%AD%E8%A8%80%E5%AE%9E%E7 %8E%B0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-104402199.142^v84^koosearch_v1,239^v2^insert_chatgpt&spm=1018.2226.3001.4187


Summarize

  • The queue has a first-in-first-out characteristic, which can be understood based on the queuing phenomenon in life . Pay attention to distinguish it from the last-in-first-out characteristic of the stack.
  • Both stacks and queues are linear lists with limited operations . Stacks only allow adding and deleting elements at the top of the stack, that is, operations on one end of the list; queues only allow adding elements at the end of the queue and deleting elements at the head of the queue, that is, operations on both sides of the list. Operations are performed on the end; therefore, not all operations on linear tables can be applied to stacks and queues . For example , it is impossible to read an element in the middle of the stack or queue at will.

  • In the sequential queue , pay attention to whether the tail pointer Q.rear points to the next element of the queue tail element or the current queue tail element . Pay attention to the question review.
  • Familiar with the full condition and empty condition in the circular queue , finding the number of queue elements , and how the head pointer of the queue changes when leaving the queue and the tail pointer of the queue when joining the queue. This is a common test point and a key content.
  • Familiar with the three major solutions for distinguishing the empty and full conditions of the circular queue , as well as the conditions for judging the empty and full conditions of the circular queue under each solution.
  • Method 1: Sacrifice a storage unit

  •  Method 2: Add a member variable size that represents the number of elements to the type

 

  • Method 3: Add a new flag variable tag to the type

  • Understand how to determine whether a double-ended queue can be enqueued by a double-ended queue based on the known dequeue sequence . If so, be able to write a reasonable double-ended queue enqueue sequence. Key points

END.

Guess you like

Origin blog.csdn.net/qq_52487066/article/details/130233228