数据结构-队列(链式队列实现)

链式队列中结点的结构体定义:

typedef struct qnode{
	DataType data;
	struct qnode *next;
}LQNode;

为了方便参数调用,通常把链式队列的头指针front和队尾指针rear定义为下面的格式:

typedef struct{
	LQNode *front;
	LQNode *rear;
}LQueue;

链式队列操作的实现:
(1),queue init

void QueueInitiate(LQueue *Q)
{
	Q->rear = NULL;
	Q->front = NULL;
}

(2),judge the queue is NULL or not
not NULL return 1, NULL return 0

int QueueNotEmpty(LQueue Q)
{
	if (Q.front == NULL)
	{
		return 0;
	}
	else{
		return 1;
	}
}

(3),insert queue
insert the data_item into the end of queue

void QueueAppend(LQueue *Q, DataType x)
{
	LQNode *p;
	p = (LQNode *)malloc(sizeof(LQNode));

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

	if (Q->rear != NULL)
	{
		Q->rear->next = p;
	}
	Q->rear = p;
	if (Q->front == NULL)
	{
		Q->front = p;
	}
}

(4),delete queue item

int QueueDelete(LQueue *Q, DataType *d)
{
	LQNode *p;
	if(Q->front == NULL)
	{
		printf("queue have empty, no item could be delete");
		return 0;
	}
	else{
		*d = Q->front->data;
		p  = Q->front;
		Q->front = Q->front->next;
		if (Q->front == NULL)
		{
			Q->rear = NULL;
		}
		
		free(p);
		return 1;
	}
}

(5),Get the head item of queue

int QueueGet(LQueue *Q, DataType *d)
{
    LQueue *p = NULL;
	if (Q->front == NULL)
	{
		printf("Queue have empty, no item could be get!");
		return 0;
	}
	else{
	    p = Q->front->next;
		*d = p->data;
		Q->front->next = p->next;

		if (Q->rear == p)
		{
			Q->rear = Q->front;
		}
		free(p);
		return 1;
	}
}

(6),Destroy queue

void Destroy(LQueue Q)
{
	LQNode *p, *p1;
	
	p = Q.front;
	while (p!= NULL)
	{
		p1 = p;
		p = p->next;
		free(p1);
	}
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值