实验内容
设有n个人站成一排,从左向右的编号分别为1~n,现在从左往右报数“1,2,1,2,…”,数到“1”的人出列,数到“2”的立即站到队伍的最右端。报数过程反复进行,直到n个人都出列为止。要求给出他们的出列顺序。
算法思想
让所有元素入队
当队列不为空时,第奇数个元素出队输出,第偶数个元素重新进队
链式队列:
结果图:
/*
Author:LCH南安
Time:2021.11.17
*/
#include "stdio.h"
#include "stdlib.h"
typedef int ElemType;
typedef struct QNode
{
int data;
struct QNode *next;
} QType;
typedef struct
{
QType *front; // 队头指针
QType *rear; // 队尾指针
} LinkQueue;
//初始化队列
void InitQueue(LinkQueue *&Q)
{
Q = (LinkQueue *)malloc(sizeof(LinkQueue));
Q->rear = Q->front = NULL;
}
//销毁队列
void DestroyQueue(LinkQueue *&Q)
{
QType *pre = Q->front, *p;
if (pre != NULL) //非空队
{
if (pre == Q->rear) //队头与队尾相等,即只有一个数据接点的情况
free(pre); //释放pre接点
else //有两个或多个数据结点的情况
{
p = pre->next;
while (p != NULL)
{
free(pre); //释放pre结点
pre = p;
p = p->next; //pre、p同步后移
}
free(pre); //释放尾结点
}
}
free(Q); //释放链队结点
}
//判断队列是否为空队列
int QueueEmpty(LinkQueue *Q)
{
if (Q->front == NULL) //队空返回1
return 1;
else
return 0; //队不空返回0
}
//进队运算
int EnQueue(LinkQueue *&Q, ElemType e)
{
QType *s;
s = (QType *)malloc(sizeof(QType)); //创建新结点p插入链队末尾
s->data = e; //e的值赋值给p的data域
s->next = NULL; //next域置空
if (Q->front == NULL) //队为空的情况
Q->rear = Q->front = s; //front与rear均指向p
else //队不为空的情况
{
Q->rear->next = s; //结点p链到队尾指针的next域
Q->rear = s; //rear指向p
}
return 1;
}
// 出队运算
int DeQueue(LinkQueue *&Q, ElemType &e)
{
//用 e 返回其值,并返回1;否则返回0
QType *p;
if (Q->front == NULL) //空队情况
{
return 0;
}
p = Q->front; //p指向队头结点
e = p->data; //取队头元素值
if (Q->rear == Q->front) //若原队列只有一个结点,删除后变成空队
Q->rear = Q->front = NULL;
else
Q->front = Q->front->next;
free(p);
return 1;
}
//显示队列元素
int DispQueue(LinkQueue *Q)
{
QType *p;
if (QueueEmpty(Q))
return 0;
else
{
p = Q->front;
while (p != NULL)
{
printf("%d", p->data);
p = p->next;
}
printf("\n");
return 1;
}
}
int baoshu(LinkQueue *&Q, ElemType n)
{
int i, count = 1;
ElemType e;
for (i = 1; i <= n; i++)
{
EnQueue(Q, i);
}
printf("原队列元素:");
DispQueue(Q);
printf("排列后队列:");
while (!QueueEmpty(Q))
{
DeQueue(Q, e);
if (count % 2 == 0)
{
EnQueue(Q, e);
}
else
{
printf("%d ", e);
}
count++;
}
printf(" ");
DestroyQueue(Q);
return 0;
}
int main()
{
LinkQueue *Q;
InitQueue(Q);
printf("初始队%s\n", (QueueEmpty(Q) == 1 ? "空" : "不空"));
int n;
printf("请输入人员数量:");
scanf("%d", &n);
baoshu(Q, n);
return 0;
}
顺序式队列:
结果图:
/*
Author:LCH南安
Time:2021.11.17
*/
#include "stdio.h"
#include "stdlib.h"
#define MaxSize 1024
typedef int ElemType;
typedef struct
{
ElemType data[MaxSize]; //保存队头中元素
int front, rear; //队头和队尾指针
} SqQueue;
//创建队列
void InitQueue(SqQueue &sq)
{
sq.rear = sq.front = 0;
}
//销毁队列
void DestroyQueue(SqQueue sq)
{
}
//判断队列是否为空队列
int QueueEmpty(SqQueue sq)
{
if (sq.front == sq.rear)
return 1;
else
return 0;
}
//向队尾插入一个数据元素
int EnQueue(SqQueue &sq, ElemType x) //循环队列操作
{
if ((sq.rear + 1) % MaxSize == sq.front) //循环队列判断队满条件:(rear+1)%MaxSize==front
return 0;
sq.rear = (sq.rear + 1) % MaxSize; //队列循环进一
sq.data[sq.rear] = x;
return 1;
}
//出队
int DeQueue(SqQueue &sq, ElemType &x) //x为引用参数类型
{
//用 x 返回其值,并返回1;否则返回0
if (sq.front == sq.rear)
return 0;
sq.front = (sq.front + 1) % MaxSize;
x = sq.data[sq.front];
return 1;
}
void baoshu(SqQueue &sq, int n)
{
int i, count = 1, e;
InitQueue(sq);
for (i = 1; i <= n; i++)
EnQueue(sq, i);
printf("排列后队列:");
while (!QueueEmpty(sq))
{
DeQueue(sq, e);
if (count % 2 == 1)
printf("%d ", e);
else
EnQueue(sq, e);
count++;
}
printf("\n");
DestroyQueue(sq);
}
int main()
{
SqQueue sq;
InitQueue(sq);
int n;
printf("初始队%s\n", (QueueEmpty(sq) == 1 ? "空" : "不空"));
printf("请输入人员数量:");
scanf("%d", &n);
baoshu(sq, n);
return 0;
}