c语言实现链式队列

c语言实现链式队列

//链式队列的实现

#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;


//定义结点类型
typedef struct QNode {
	ElemType data;
	struct QNode* next;

}LQNode, *QueuePtr;

//定义队列类型
typedef struct {
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

//初始化队列
void initQueue(LinkQueue* L) {
	L->front = L->rear = (LQNode*)malloc(sizeof(LQNode));
	L->front->next = NULL;
}

//判断队列是否为空
int emptyQueue(LinkQueue L) {
	if (L.front->next == NULL)
		return 1;
	else
		return 0;

}

//入队操作
int enQueue(LinkQueue* L, ElemType e) {
	LQNode* s;
	s = (LQNode*)malloc(sizeof(LQNode));
	s->data = e;
	s->next = NULL;
	L->rear->next = s;
	L->rear = s;
	return 1;

}
 //出队操作
int deQueue(LinkQueue* L) {
	ElemType e;
	LQNode* s;
	if (L->front->next == NULL) {
		return -1;
	}
	else {
		s = L->front->next;
		e = s->data;
		L->front->next = s->next;
		if (L->rear == s)
			L->rear = L->front;
		free(s);
		return e;
	}
}

//取队头元素
int getHead(LinkQueue L, ElemType *e) {
	
	LQNode* s;
	if (L.front = L.rear) {
		return -1;
	}
	else
	{
		s = L.front->next;
		*e = s->data;
		return 1;
	}
}

//清空队列
void clearQueue(LinkQueue* L) {
	while (L->front!=NULL)
	{
		L->rear = L->front->next;
		free(L->front);
		L->front = L->rear;
	}
}
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值