`````` #include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXQSIZE 100

typedef int ElementType;

//链式存储类型定义
typedef struct Node *PtrToNode;
struct Node
{
ElementType Data;
PtrToNode Next;
};
typedef PtrToNode QNode;
typedef struct
{
PtrToNode Rear;
}*Queue;

//顺序存储类型定义
typedef struct
{
ElementType *Base;
int Rear;
}*SqQueue;

//链式存储部分函数
Queue InitQueue();
void PrintQueue(Queue Q);
void EnQueue(Queue Q,ElementType e);
ElementType DeQueue(Queue Q);

//顺序存储部分函数
SqQueue InitQueue_Sq();
int EnQueue_Sq(SqQueue Sq,ElementType e);
ElementType DeQueue_Sq(SqQueue Sq);
void PrintQueue_Sq(SqQueue Sq);

int main()
{
int a,b,c;
ElementType x;
Queue Q=NULL;
SqQueue Sq;
printf("******************************************************\n");
printf("***     请选择要进行的操作，输入数字后按回车键     ***\n");
printf("******************************************************\n");
printf("\n");
printf("  1 队列的链式存储应用\n");
printf("  2 队列的顺序存储应用\n");
printf("\n");
printf("*****************0 结束操作并退出*********************\n");
printf("\n");
scanf("%d",&a);

while(a)
{
switch(a)
{
case 1:
Q=InitQueue();
printf("\n");
printf("***********链式队列已成功初始化,请继续选择操作************\n");
printf("\n");
printf("  1 入队\n");
printf("  2 出队\n");
printf("  3 重置队列\n");
printf("\n");
printf("*********0 回到主菜单*********-1 结束操作退出系统*********\n");
printf("\n");
scanf("%d",&b);

while(b)
{
switch(b)
{
case 1:
printf("请输入需要入队的整数\n");
printf("\n");
scanf("%d",&x);
printf("\n");
EnQueue(Q,x);
printf("\n");
printf("更新后的队列为\n");
PrintQueue(Q);
printf("\n");
break;
case 2:
printf("出队元素为:%d\n",DeQueue(Q));
printf("\n");
printf("更新后的队列为\n");
PrintQueue(Q);
printf("\n");
break;
case 3:

printf("\n");
printf("队列已重置\n");
printf("\n");
break;
case -1:exit(0);break;
default:printf("输入错误\n");break;
}
printf("***请继续选择操作***\n");
scanf("%d",&b);
}
break;
case 2:
Sq=InitQueue_Sq();
printf("\n");
printf("***********顺序循环队列已成功初始化,请继续选择操作************\n");
printf("\n");
printf("  1 入队\n");
printf("  2 出队\n");
printf("  3 重置队列\n");
printf("\n");
printf("********0 回到主菜单********-1 结束操作退出系统********\n");
printf("\n");
scanf("%d",&c);
while(c)
{
switch(c)
{
case 1:
printf("请输入需要入队的整数\n");
printf("\n");
scanf("%d",&x);
printf("\n");
EnQueue_Sq(Sq,x);
printf("\n");
printf("更新后的队列为\n");
PrintQueue_Sq(Sq);
printf("\n");
break;
case 2:
printf("出队元素为:%d\n",DeQueue_Sq(Sq));
printf("\n");
printf("更新后的栈为\n");
PrintQueue_Sq(Sq);
printf("\n");
break;
case 3:
Sq=InitQueue_Sq();
printf("\n");
printf("队列已重置\n");
printf("\n");
break;
case -1:exit(0);break;
default:printf("输入错误\n");break;
}
printf("***请继续选择操作***\n");
scanf("%d",&c);
}break;
default:printf("输入错误\n");break;
}
printf("******************************************************\n");
printf("***     请选择要进行的操作，输入数字后按回车键     ***\n");
printf("******************************************************\n");
printf("\n");
printf("  1 队列的链式存储应用\n");
printf("  2 队列的顺序存储应用\n");
printf("\n");
printf("*****************0 结束操作并退出*********************\n");
printf("\n");
scanf("%d",&a);
}
return 0;
}

//链式存储部分函数
Queue InitQueue()
{
Queue Q;
return Q;
}

void PrintQueue(Queue Q)//队头为头结点的下一个结点
{
QNode p;
{
printf("队列为空\n");return;
}
printf("队头<-");
while(p)
{
printf("%d<-",p->Data);
p=p->Next;
}
printf("队尾\n");

}

void EnQueue(Queue Q,ElementType e)
{
QNode temp;
temp=(QNode)malloc(sizeof(QNode));
temp->Next=NULL;
temp->Data=e;
Q->Rear->Next=temp;
Q->Rear=temp;
}

ElementType DeQueue(Queue Q)
{
QNode temp;
ElementType e;
return 0;
e=temp->Data;
free(temp);
return e;
}

//顺序存储部分函数

SqQueue InitQueue_Sq()
{
SqQueue Sq;
Sq->Base=(ElementType*)malloc(MAXQSIZE*sizeof(ElementType));
if(!Sq->Base) return NULL;
return Sq;
}

int EnQueue_Sq(SqQueue Sq,ElementType e)
{
return 0;
Sq->Base[Sq->Rear]=e;
Sq->Rear=(Sq->Rear+1)%MAXQSIZE;
return 1;
}

ElementType DeQueue_Sq(SqQueue Sq)
{
ElementType e;
{
printf("队列为空\n");return 0;
}
return e;
}

void PrintQueue_Sq(SqQueue Sq)
{
{
printf("队列为空\n");return;
}
printf("队头<-");
while(i != Sq->Rear)
{
if(i<0)
i=i+MAXQSIZE;
printf(" %d <-",Sq->Base[i]);i++;
}
printf("队尾\n");
}

/*
SqQueue Sq;
Sq=InitQueue_Sq();
EnQueue_Sq(Sq,1);PrintQueue_Sq(Sq);
EnQueue_Sq(Sq,2);PrintQueue_Sq(Sq);
EnQueue_Sq(Sq,3);PrintQueue_Sq(Sq);
DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
EnQueue_Sq(Sq,4);PrintQueue_Sq(Sq);
DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
EnQueue_Sq(Sq,5);PrintQueue_Sq(Sq);
DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
DeQueue_Sq(Sq);PrintQueue_Sq(Sq);

//链式
Queue Q;//带头结点
Q=InitQueue();
EnQueue(Q,1);PrintQueue(Q);
EnQueue(Q,2);PrintQueue(Q);
EnQueue(Q,3);PrintQueue(Q);
EnQueue(Q,4);PrintQueue(Q);
DeQueue(Q);PrintQueue(Q);
EnQueue(Q,5);PrintQueue(Q);
DeQueue(Q);PrintQueue(Q);
DeQueue(Q);PrintQueue(Q);
DeQueue(Q);PrintQueue(Q);
DeQueue(Q);PrintQueue(Q);
*/

``````

3个回答