链队列的各项操作
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define OK 1
#define ERROW -1
#define OVERFLOW 0
#define LN LinkQuNode *
#define LEN sizeof(LinkQuNode)
#define DN DataNode*
#define DEN sizeof(DataNode)
typedef int ElemType;
typedef struct qnode
{
ElemType data;
struct qnode * next;
}DataNode;
typedef struct
{
DataNode* front;
DataNode* rear;
}LinkQuNode;
int InitQueue(LinkQuNode *L)
{
L->front = L->rear = (DN)malloc(DEN);
L->front ->next =NULL;
printf("初始化成功\n");
system("pause");
return OK;
}
int CreatQueue(LinkQuNode * L)
{
if (L == NULL)
return 0;
DataNode * q;
int i, n;
printf("请输入要创建队列的长度:\n");
scanf_s("%d", &n);
printf("请输入数据:\n");
for (i = 0; i < n; i++)
{
q = (DN)malloc(DEN);
if (!q)
{
printf("error\n");
return 0;
}
scanf_s("%d", &q->data);
q->next = NULL;
L->rear->next = q;
L->rear = q;
}
printf("创建成功\n");
system("pause");
return OVERFLOW;
}
int GetLength(LinkQuNode * L)
{
DataNode * p= L->front;
int len = 0;
while (p) {
len++;
p = p->next;
}
printf("队列长度为:%d", len);
system("pause");
return 0;
}
int GetHead(LinkQuNode *L)
{
if (L->front != L->rear)
{
return L->front->next->data;
}
return OK;
}
int InQueue(LinkQuNode *L)
{
int i;
DataNode* q;
q = (DN)malloc(DEN);
if (!q)
{
printf("error\n");
system("pause");
return 0;
}
printf("请输入创建数据的方式,1为手动输入数字,2为随机数。\n");
scanf_s("%d", &i);
if (i == 1)
{
printf("请输入入列的数据:\n");
scanf_s("%d", &q->data);
q->next = NULL;
if (L->rear == NULL)
{
L->front = L->rear = q;
}
else
{
L->rear->next = q;
L->rear = q;
printf("随机数入列成功\n");
}
}
else if (i == 2)
{
q->data=rand()%10+12;
q->next = NULL;
if (L->rear == NULL)
{
L->front = L->rear = q;
}
else
{
L->rear->next = q;
L->rear = q;
printf("随机数入列成功\n");
}
}
system("pause");
return 0;
}
int OutQueue(LinkQuNode * L,ElemType &e)
{
DataNode* p;
if (L->rear==NULL)
{
return false;
}
p = L->front;
if (L->front == L->rear)
{
L->front = L->rear = NULL;
printf("队列空\n");
system("pause");
return OVERFLOW;
}
else
{
L->front = L->front->next;
}
e = p->data;
free(p);
printf("出列成功\n");
return 0;
}
int EmptyQueue(LinkQuNode* L)
{
if (L->front == L->rear)
printf("空队列\n");
else
{
printf("非空队列\n");
}
system("pause");
return 0;
}
int OutPut(LinkQuNode* L)
{
DataNode* p;
if (L->front == L->rear)
{
printf("空队列\n");
system("pause");
return 0;
}
p = L->front->next;
while (p != NULL)
{
printf("%d\t", p->data);
p = p->next;
}
system("pause");
return 0;
}
void menu()
{
printf("\t\t 队列链式存储结构基本操作学习系统\n");
printf("\t\t *** 主 菜 单 ***\n\n");
printf("\t\t *1 初始化链队列\n");
printf("\t\t *2 创建长度为n的链列表\n");
printf("\t\t *3 打印链队列的表头元素\n");
printf("\t\t *4 打印链队列的表头元素\n");
printf("\t\t *5 对链队列入列\n");
printf("\t\t *6 对链队列出列\n");
printf("\t\t *7 判断是否空队列\n");
printf("\t\t *8 输出链队列表的值\n");
printf("\t\t *0 结束程序\n");
}
int main()
{
LinkQuNode L;
menu();
for (;;)
{
int t;
scanf_s("%d", &t);
switch (t)
{
case 1:
{
long start, end;
start = clock();
InitQueue(&L);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 2:
{
long start, end;
start = clock();
CreatQueue(&L);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 3:
{
long start, end;
start = clock();
GetLength(&L);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 4:
{
long start, end;
start = clock();
int a= GetHead(&L);
printf("队头元素为%d\n", a);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 5:
{
long start, end;
start = clock();
InQueue(&L);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 6:
{
long start, end;
start = clock();
ElemType e;
OutQueue(&L,e);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 7:
{
long start, end;
start = clock();
EmptyQueue(&L);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 8:
{
long start, end;
start = clock();
OutPut(&L);
end = clock();
printf("该模块运行时长为%d ms\n", end - start);
continue;
}
case 0: break;
default:
{
printf("输入有误,可以重新选择,退出按 0!!!!!!\n");
}
}
}
}