Chapter 7: C Language Data Structure and Algorithm Elementary Queue

Series Article Directory



foreword

Queue: A special linear table that only allows data to be inserted at one end and data to be deleted at the other end.


1. Queue

insert image description here
Queues are like queuing in our lives. They are only allowed to leave at the head of the queue and join at the end of the queue. The end of the insertion is the tail of the queue, and the end of the deletion is the head of the queue. So the queue hasfirst in first outspecialty.

Second, the realization of the queue

typedef int QDataType;

typedef struct QueueNode
{
    
    
	QDataType data;
	struct QueueNode* next;
}QNode;

typedef struct Queue
{
    
    
	QNode* head;
	QNode* tail;
	int size;
}Queue;

In the above definition, we first define the elements in the queue, and implement the queue structure in the form of a linked list. In fact, the reason is very simple. The queue involves the problem of head deletion, and the time complexity of head deletion in the sequence table is O(N). Obviously, the efficiency of the sequence table to implement the queue is very low. Therefore, we use the form of a linked list.
We found that every time the tail is inserted, it is necessary to find the tail node. The complexity of this process is O(N). Its efficiency is very low. Therefore, we can define a tail pointer to record the tail node. So we have another structure about the queue. Two variables are defined in this structure, one is the head pointer and the other is the tail pointer. size can be used to represent the number of elements, so that there is no need to traverse O(n), space for time.
insert image description here

3. Realization of interface functions

1. Initialization

void QueueInit(Queue* pq)
{
    
    
	assert(pq);

	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;
}

Make both pointers point to null pointers.

2. Destroy

void QueueDestory(Queue* pq)
{
    
    
	assert(pq);

	QNode* cur = pq->head;
	while (cur)
	{
    
    
		QNode* del = cur;
		cur = cur->next;
		free(del);
	}
	pq->head = NULL;
	pq->tail = NULL;
	pq->size = 0;

}

Set the head pointer and tail pointer to null to avoid wild pointers.

3. Joining/exiting the team

void QueuePush(Queue* pq, QDataType x)
{
    
    
	assert(pq);

	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);

	newnode->data = x;
	newnode->next = NULL;

	if (pq->tail == NULL)
	{
    
    
		pq->head = pq->tail = newnode;
	}
	else
	{
    
    
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;

}
void QueuePop(Queue* pq)
{
    
    
	assert(pq);
	assert(!QueueEmpty(pq));

	if (pq->head == pq->tail)
	{
    
    
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
    
    
		QNode* prev = pq->head;
		pq->head = prev->next;
		free(prev);
	}
	pq->size--;
}

It is necessary to judge the situation where the element is empty, and be careful of wild pointers.

4. Team head/tail

QDataType QueueFront(Queue* pq)
{
    
    
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
	
}

QDataType QueueBack(Queue* pq)
{
    
    
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->data;
}

Check if the element is empty.

5. Empty judgment/number of elements

bool QueueEmpty(Queue* pq)
{
    
    
	assert(pq);

	return pq->head == NULL && pq->tail == NULL;
}

int QueueSize(Queue* pq)
{
    
    
	assert(pq);
	
	return pq->size;
}

The head pointer points to the first element, so if the head pointer points to empty, then the queue is empty.

4. Access to elements in the queue

while (!QueueEmpty(&q))
	{
    
    
		printf("%d ", QueueFront(&q));
		QueuePop(&q);
	}
	printf("\n");

Since the elements in the queue meet the characteristics of first-in-first-out and last-in-last-out, we can only access the head and tail. If we want to access the second element, we must delete the head of the queue. Therefore, we can combine the above interface functions to simulate the implementation of the queue.


Summarize

The characteristic of the queue is first in last out.
Life should be like a candle, which is always bright from the top to the bottom. —— Xiao Chunv

Guess you like

Origin blog.csdn.net/yanyongfu523/article/details/129469192