Data Structure - Chapter 3 - Stack and Queue (5) - Chain Team

data structure


⚡️Data Structures-Chapter 1
⚡️Abstract Data Type Case
⚡️Data Structures-Chapter 2(1)-Linear Structures
⚡️Data Structures-Chapter 2(2)-Sequential Representation and Implementation of Linear Tables
⚡️Data Structures-Chapter 2 (3) - Sequence table (with code)
⚡️ Data structure - Chapter 2 (4) - Sequence table case (with code)
⚡️ Data structure - Chapter 2 (5) - Chained storage structure
⚡️ Data structure - Chapter 2 (6) - Implementation of basic operations of singly linked list
⚡️ Data structure - Chapter 2 (7) - Doubly linked list and circular linked list
⚡️ Data structure - Chapter 2 (8) - Application of linear list (linear list merging)

Chain Queue - Chain representation and implementation of queues

Chain teams are usually represented by a singly linked list

Chain team definition

typedef struct QNode
{
    
    
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
    
    
	QueuePtr front; //队头指针
	QueuePtr rear;	//队尾指针
}LinkQueue;

insert image description here
insert image description here

chain queue initialization

【Algorithm steps】

  • ① Generate a new node as the head node, and the head and tail of the queue point to this node

  • ② The pointer field of the head node is empty

【Algorithm Description】

Status InitQueue(LinkQueue &Q)
{
    
    //构造一个空队列Q
	Q.front=Q.rear=new QNode;
	if(!Q.front) exit(OVERFLOW);
	Q.front->next=NULL;//头节点的指针域置为空
	return OK;
}

insert image description here

Destroy the chain queue

insert image description here

join the team

【Algorithm Description】

  • ① Allocate the node space for the enqueued element and use the pointer p to point to it

  • ② Set the new node data field to e

  • ③ Insert the new node at the end of the queue

  • ④ Modify the tail pointer to p

【Algorithm steps】

Status EnQueue(LinkQueue &Q, QElemType e)
{
    
    
	p = new QNode;
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;//将新节点插入到对位
	q.rear=p;//修改队尾指针
	return OK;
}

insert image description here

out of the team

【Algorithm Description】

  • ① Determine whether the queue is empty, and return ERROR if it is empty.

  • ② Temporarily save the space for the head element of the queue

  • ③ Modify the pointer field of the head node to point to the next node.

  • ④ Determine whether the dequeuing element is the last element

  • ⑤ Release the space of the head element

【Algorithm steps】

Status DeQueue(LinkQueue &Q, QElemType &e)
{
    
    
	if(Q.front == Q.rear)
		return ERROR;//队空
	p=Q.front->next; //临时变量p保存
	e=p->data;	//e返回值
	Q.front->next=p->next//修改头节点的指针域
	if(Q.rear==p) Q.rear=Q.front;//最后一个元素
	delete p;//释放空间
	return OK;
}

insert image description here
insert image description here

Find the head element of the chain queue

【Algorithm Description】

SElemType GetHead(LinkQueue Q)
{
    
    
	if(Q.front != Q.rear)	//队列非空
		return Q.front->next->data;	//返回值
}

insert image description here

Summarize

I look forward to your communication with me, leave a message or private message, learn together and make progress together!

Guess you like

Origin blog.csdn.net/CltCj/article/details/122636876