#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");
}
}
《数据结构》C语言版 链式队列的基本操作实现
最新推荐文章于 2022-04-02 14:40:19 发布