Linear structure of data structure (queue)

This article is a linear structure (queue) of the data structure, and it is an integrated note based on the online course.

What is a queue

Queue : a linear table with certain operational constraints

Insert and delete operations: You can only insert at one end and delete at the other end.

  • Data insertion: into the queue (AddQ)
  • Data deletion: out of the queue (DeleteQ)
  • First come first serve
  • First in first out: FIFO

Description of the abstract data type of the queue

类型名称: Queue

数据对象集: A finite linear table with 0 or more elements.

操作集: Queue Q "Queue with a length of Maxsize, queue element item "ElementType

1.Queue CreatQueue(int MaxSize): Generate an empty queue with a length of MaxSize;

2.int IsFullQ(Queue q,int MaxSize): Determine whether the queue Q is full;

3.void AddQ (Queue Q, ElementType item): insert the data element item into the queue Q;

4.int IsEmptyQ(Queue Q): Determine whether the queue Q is empty;

5. ElementType DeleteQ (Queue Q): delete the head data element from the queue and return it.

Queue's sequential storage implementation

The sequential storage structure of the queue usually consists of a one-dimensional array and a variable front that records the position of the head element of the queue (pointing to the previous one of the head element of the queue, or -1 if there is no deletion) and a variable rear that records the position of the tail element of the queue . (When front===rear, the queue is empty)

#define MaxSize<储存数据元素的最大个数>
struct 	QNode{
    
    
	ElementType Data[MaxSize];
	int rear;
	int front;
};
typedef struct QNode*Queue;

[Example] A work queue

Insert picture description here
Insert picture description here
Thinking:

(1) This scheme: What are the criteria for judging whether the stack is empty or full?

solution:

  • Use additional tags: Size (number of elements, +1 when adding an element, -1 when deleting an element, empty or full according to Size of 0 or 1) or tag (tag is 1 when inserting an element, deleting one When the element tag is 0, the last operation is judged according to the tag being 0 or 1, so as to judge whether it is empty or full)
  • Use only n-1 array spaces

(2) Why can't distinguish between empty and full? root cause?

Judging by the gap between front and rear, there are 6 situations in the above picture: 0,1,2,3,4,5

In the above figure, there are 7 types of loading elements in the queue: 0 (empty queue), 1 (one element), 2, 3, 4, 5, 6

Therefore, there are n types of gaps in the queue and n+1 types of the number of elements loaded in the queue, which must be contradictory.

(1) Into the queue

void AddQ(Queue PtrQ,ElementType item)
{
    
    
	if((PtrQ->rear+1)%MaxSize == PtrQ->front){
    
    
		printf("队列满")return;
	}
	PtrQ->rear = (PtrQ->rear+1)%MaxSize;
	PtrQ->Data[PtrQ->rear]=item;
}

(2) Out of the queue

ElementType DeleteQ(Queue PtrQ)
{
    
    
	if(PtrQ->front == PtrQ->rear){
    
    
		printf("队列空")return ERROR;
	}else{
    
    
		PtrQ->front = (PtrQ->front+1)%MaxSize;
		return PtrQ->Data[PtrQ->front];
	}
}

The movement of the front and rear pointers adopts the " add 1 to take the remainder " method, which reflects the " recycling use " of sequential storage .

Chain storage implementation of queue

The chained storage structure of the queue can also be implemented with a singly linked list . Insert and delete operations are performed at the two ends of the linked list respectively

struct Node{
    
    
	ElementType Data;
	struct Node *Next;
};
struct QNode{
    
      /*链队列结构*/
	struct Node *rear; /*指向队尾结点*/
	struct Node *front;/*指向队头结点*/
}typedef struct QNode*Queue;
Queue PtrQ;

Idea map:

Insert picture description here

  • Chain without head node queue dequeue one example:
ElementType DeleteQ(Queue PtrQ)
{
    
    
	struct Node *FrontCell;
	ElementType FrontElem;
	if(PtrQ->front==NULL){
    
    
		printf("队列空")return ERROR;
	}
	FrontCell=Ptrq->front;
	if(PtrQ->front==PtrQ->Rear)/*若队列只有一个元素*/
		PtrQ->front = PtrQ->rear = NULL;/*删除后队列置为空*/
	else
		PtrQ->front = PtrQ->front->Next;
	FrontElem = FrontCell->Data;
	free(FrontCell);/*释放被删除结点空间*/
	return FrontElem;
}

Disclaimer: Part of the information comes from the Internet, if there is any infringement, please contact me to delete it!
If there are errors, confusions, etc. in the article, criticisms and corrections are welcome!

Guess you like

Origin blog.csdn.net/hxtxsdcxy/article/details/113796815