数据结构---队列(带头结点)

/*
    队列     链式存储结构
*/
/*
	队列	 链式存储结构
*/
#include <stdlib.h>
#include <stdio.h>
#define ERROR -1
#define OK 0
typedef int ElemType;

typedef struct Node 
{
	ElemType value;
	struct Node *next;
}Node;

typedef struct LinkQueue
{
	Node *front;
	Node *rear;
	int count;
}LinkQueue;

int initQueue(LinkQueue *q)
{
	Node *p = (Node *)malloc(sizeof(Node));
	p->value = 0;
	p->next = NULL;

	q->rear = q->front = p;
	if (q->front == NULL)
	{
		return ERROR;
	}
	q->count = 0;
	return OK;
}

int inQueue(LinkQueue *q, ElemType value)
{
	Node *p = (Node *)malloc(sizeof(Node));
	if (!p)
	{
		return ERROR;
	}
	p->value = value;
	p->next = NULL;
	q->rear->next = p;
	q->rear = p;
	q->count += 1;
	printf("inQueue count=%d\n", q->count);
	return OK;
}

int outQueue(LinkQueue *q,ElemType *value)
{
	Node *del = q->front->next;
	if (!del)
	{
		return ERROR;
	}
	*value = del->value;
	q->front->next = del->next;
	if (del == q->rear)
	{
		q->rear = q->front;
	}
	free(del);
	q->count -= 1;
	printf("outQueue count=%d\n", q->count);
	return OK;
}

int main()
{
	LinkQueue *q = (LinkQueue*)malloc(sizeof(LinkQueue));
	initQueue(q);
	for (int i = 1; i <= 10; i++)
	{
		inQueue(q,i);
	}
	for (int i = 0;i<=10;i++)
	{
		int data = 0;
		int ret = outQueue(q, &data);
		printf("outQueue i=%d ret=%d data=%d\n", i, ret, data);
	}
    free(q);
	return 0;
}

队列是只允许在一端插入另外一端删除操作的线性表。。。。

代码经过vs2017测试过,有啥问题欢迎交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值