《数据结构》C语言版 链式队列的基本操作实现

#include "stdio.h"
#include "stdlib.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1

typedef int QElemType;
typedef int Status;

typedef struct QNode
{
	QElemType data;//数据域
	struct QNode* next;
}QNode, * QueuePtr;//结点类型、结点指针类型

typedef struct
{
	QueuePtr front;//队头指针
	QueuePtr rear;//队尾指针
}LinkQueue;

//构造空链队列
Status InitQueue(LinkQueue* Q)
{
	Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
	if (Q->front == 0)exit(OVERFLOW);
	Q->front->next = NULL;
	return OK;
}

//销毁
Status DestroyQueue(LinkQueue* Q)
{
	while (Q->front)
	{
		Q->rear = Q->front->next;
		free(Q->front);
		Q->front = Q->rear;
	}
	return OK;
}

//判空
Status QueueEmpty(LinkQueue* Q)
{
	if (Q->front == Q->rear == NULL)
		return OK;
	//return ERROR;
}

//清空
Status ClearQueue(LinkQueue* Q)
{
	while (Q->front->next)
	{
		Q->rear = Q->front->next;
		free(Q->front);
		Q->front = Q->rear;
	}
	return OK;
}

//求长度
int QueueLength(LinkQueue Q, int count)
{
	QNode* p;
	p = Q.front;
	count = 0;
	while (p != Q.rear)
	{
		count++;
		p = p->next;
	}
	return count;
}

//队尾插入元素为新的队尾元素
Status EnQueue(LinkQueue* Q, QElemType e)
{
	QNode* p;
	p = (QueuePtr)malloc(sizeof(QNode));
	if (!p)exit(OVERFLOW);
	p->data = e; p->next = NULL;
	Q->rear->next = p;
	Q->rear = p;
	return OK;
}
//删除队头元素
Status DeQueue(LinkQueue* Q, QElemType* e)
{
	QNode* p;
	if (Q->front == Q->rear)return ERROR;
	p = Q->front->next;
	*e = p->data;
	Q->front->next = p->next;
	if (Q->rear == p)
		Q->rear = Q->front;
	free(p);
	return OK;
}

//获取队头元素
Status GetHead(LinkQueue Q, QElemType* e)
{
	QNode* p;
	p = Q.front->next;
	if (Q.front->next == NULL)
		return ERROR;
	else
	{
		*e = p->data;
	}
	return OK;
}

//遍历队列,从队头到队尾
Status QueueTraverse(LinkQueue* Q)
{
	QueuePtr p = Q->front->next;
	QElemType e;
	while (p)
	{
		e = p->data;
		printf("%d ", e);
		p = p->next;
	}
	printf("\n");
	return OK;
}

void main()
{
	LinkQueue Q;
	int choice=1;
	int length=0;
	if (InitQueue(&Q) == OK)
		printf("Init LinkQueue success!\n");
	else
		printf("failure!\n");
	while (choice != 0)
	{
		system("cls");
		printf("1.DestroyQueue  2.ClearQueue  3.QueueEmpty 4.QueueLength\n");
		printf("5.GetHead  6.EnQueue  7.DeQueue 8.QueueTraverse 0.Exit\n");
		printf("Input your choice:");
		scanf_s("%d", &choice);
		switch (choice)
		{
			case 0:
				printf("ByeBye!\n");
			case 1:
			{
				DestroyQueue(&Q);
				break;
			}
			case 2:
			{
				if (ClearQueue(&Q) == OK)
					printf("CLEAR!\n");
				else
					printf("ERROR!\n");
				break;
			}
			case 3:
			{
				if (QueueEmpty(&Q) == OK)
					printf("This queue is not empty!\n");
				else
					printf("This queue is empty!\n");
				break;
			}
			case 4:
			{
			
				printf("当前队列的长度为:%d\n", QueueLength(Q, length));
				break;
			}
			case 5:
			{
				QElemType e;
				if (GetHead(Q, &e) == OK)
					printf("The head is %d\n", e);
				else
					printf("当前链表为空!\n");
				break;
			}
			case 6:
			{
				int n;
				int i;
				QElemType e;
				printf("请输入将要输入数据的个数n:");
				scanf_s("%d", &n);
				for (i = 0; i < n; i++)
				{
					printf("Input the elem:");
					scanf_s("%d", &e);
					EnQueue(&Q, e);
				}
				break;
			}
			case 7:
			{
				QElemType e;
				if (DeQueue(&Q, &e) == OK)
					printf("删除的数据为%d\n", e);
				else
					printf("当前链表为空\n");
				break;
			}
			case 8:
			{			
				QueueTraverse(&Q);
				break;
			}
		}
		system("pause");
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值