队列的链表实现

Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef int QDataType;

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

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QInit(Queue* q);
void QDestory(Queue* q);
void QPush(Queue* q,QDataType x);
void QPop(Queue* q);
//返回队列的头尾元素
QDataType QFront(Queue* q);
QDataType QBack(Queue* q);
//返回队列的元素个数
int QSize(Queue* q);
//检验队列是否为空
bool QEmpty(Queue* q);

Queue.c

#include"Queue.h"

void QInit(Queue* q)
{	
	assert(q);
	q->phead = q->ptail = NULL;
	q->size = 0;
}

void QDestory(Queue* q)
{
	assert(q);
	QNode* cur = q->phead;
	while (cur)
	{	
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->phead = q->ptail = NULL;
	q->size = 0;
}

void QPush(Queue* q, QDataType x)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
	}
	newnode->next = NULL;
	newnode->val = x;
	//队列中只有一个结点
	if (q->ptail == NULL)
	{
		q->phead = q->ptail = newnode;
	}
	else
	{
		q->ptail->next = newnode;
		q->ptail = newnode;
	}
	q->size++;
}

void QPop(Queue* q)
{
	assert(q);
	assert(q->size != 0);
	//ptail会变成野指针
	if (q->phead->next == NULL)
	{
		free(q->phead);
		q->phead = q->ptail = NULL;
	}
	else
	{
		QNode* next = q->phead->next;
		free(q->phead);
		q->phead = next;
	}
	q->size--;
}

//返回队列的头尾元素
QDataType QFront(Queue* q)
{
	assert(q);
	return q->phead->val;
}

QDataType QBack(Queue* q)
{
	assert(q);
	return q->ptail->val;
}

//返回队列的元素个数
int QSize(Queue* q)
{
	assert(q);
	return q->size;
}

//检验队列是否为空
bool QEmpty(Queue* q)
{
	assert(q);
	return q->size == 0;
}

test.c

#include"Queue.h"

void test01()
{
	Queue q;
	QInit(&q);
	QPush(&q, 1);
	QPush(&q, 2);
	QPush(&q, 3);
	QPush(&q, 4);
	while (!QEmpty(&q))
	{
		printf("%d->", QFront(&q));
		QPop(&q);
	}
	printf("NULL");
}

int main()
{
	test01();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言链表实现循环队列的代码实现和详细步骤: 1.定义循环队列的结构体和初始化函数 ```c #define MAXSIZE 100 // 循环队列的最大长度 typedef struct SLnode { int data; struct SLnode* next; }SLnode; typedef struct CQueue { SLnode* front; // 队头指针 SLnode* tail; // 队尾指针 int length; // 队列长度 }CQueue; // 初始化循环队列 void InitCQueue(CQueue* pq) { pq->front = pq->tail = (SLnode*)malloc(sizeof(SLnode)); pq->front->next = NULL; pq->length = 0; } ``` 2.实现入队操作 ```c // 入队操作 void EnCQueue(CQueue* pq, int x) { SLnode* s = (SLnode*)malloc(sizeof(SLnode)); s->data = x; s->next = NULL; pq->tail->next = s; pq->tail = s; pq->length++; } ``` 3.实现出队操作 ```c // 出队操作 int DeCQueue(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法出队!\n"); return -1; } SLnode* p = pq->front->next; int x = p->data; pq->front->next = p->next; if (pq->tail == p) { pq->tail = pq->front; } free(p); pq->length--; return x; } ``` 4.实现获取队列长度的函数 ```c // 获取队列长度 int CQueueLength(CQueue* pq) { SLnode* cur = pq->front->next; int size = 0; while (cur != NULL) { cur = cur->next; ++size; } return size; } ``` 5.实现判断队列是否为空的函数 ```c // 判断队列是否为空 int IsCQueueEmpty(CQueue* pq) { if (pq->front == pq->tail) { return 1; } else { return 0; } } ``` 6.实现判断队列是否已满的函数 ```c // 判断队列是否已满 int IsCQueueFull(CQueue* pq) { if (pq->length == MAXSIZE) { return 1; } else { return 0; } } ``` 7.实现获取队头元素的函数 ```c // 获取队头元素 int GetCQueueFront(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法获取队头元素!\n"); return -1; } return pq->front->next->data; } ``` 8.实现获取队尾元素的函数 ```c // 获取队尾元素 int GetCQueueRear(CQueue* pq) { if (pq->front == pq->tail) { printf("队列为空,无法获取队尾元素!\n"); return -1; } return pq->tail->data; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值