当前位置:网站首页>队列(C语言)

队列(C语言)

2022-06-11 22:55:00 小白丷

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

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

void QueueInit(Queue* pq)
{
    
	assert(pq);
	pq->head = pq->tail = NULL;
}

void QueueDestroy(Queue* pq)
{
    
	QNode* cur = pq->head;
	while (cur)
	{
    
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}

void QueuePush(Queue* pq, QDataType x)
{
    
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
    
		pq->head = pq->tail = newnode;
	}
	else
	{
    
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

void QueuePop(Queue* pq)
{
    
	QNode* del = pq->head;
	pq->head = pq->head->next;
	free(del);
	if (pq->head == NULL)
	{
    
		pq ->tail = NULL;
	}
}

QDataType QueueFront(Queue* pq)
{
    
	return pq->head->data;
}

QDataType QueueBack(Queue* pq)
{
    
	return pq->tail->data;
}

bool QueueEmpty(Queue* pq)
{
    
	return pq->tail == NULL;
}

int QueueSize(Queue* pq)
{
    
	QNode* cur = pq->head;
	int size = 0;
	while (cur)
	{
    
		size++;
		cur = cur->next;
	}
	return size;
}
原网站

版权声明
本文为[小白丷]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Moon_God__/article/details/125114903