📄 实现程序.txt
字号:
#include <stdio.h>
#include <stdlib.h>
typedef int elemType;
/************************************************************************/
/* 以下是关于队列链接存储操作的6种算法 */
/************************************************************************/
struct sNode{
elemType data; /* 值域 */
struct sNode *next; /* 链接指针 */
};
struct queueLK{
struct sNode *front; /* 队首指针 */
struct sNode *rear; /* 队尾指针 */
};
/* 1.初始化链队 */
void initQueue(struct queueLK *hq)
{
hq->front = hq->rear = NULL; /* 把队首和队尾指针置空 */
return;
}
/* 2.向链队中插入一个元素x */
void enQueue(struct queueLK *hq, elemType x)
{
/* 得到一个由newP指针所指向的新结点 */
struct sNode *newP;
newP = malloc(sizeof(struct sNode));
if(newP == NULL){
printf("内存空间分配失败! ");
exit(1);
}
/* 把x的值赋给新结点的值域,把新结点的指针域置空 */
newP->data = x;
newP->next = NULL;
/* 若链队为空,则新结点即是队首结点又是队尾结点 */
if(hq->rear == NULL){
hq->front = hq->rear = newP;
}else{ /* 若链队非空,则依次修改队尾结点的指针域和队尾指针,使之指向新的队尾结点 */
hq->rear = hq->rear->next = newP; /* 注意赋值顺序哦 */
/*hq->rear=hq->rear->next;*/
/*hq->rear=newP;*/
}
return;
}
/* 3.从队列中删除第一个元素 */
elemType outQueue(struct queueLK *hq)
{
struct sNode *p;
elemType temp;
/* 若链队为空则停止运行 */
if(hq->front == NULL){
printf("队列为空,无法删除! ");
exit(1);
}
temp = hq->front->data; /* 暂存队尾元素以便返回 */
p = hq->front; /* 暂存队尾指针以便回收队尾结点 */
hq->front = p->next; /* 使队首指针指向下一个结点 */
/* 若删除后链队为空,则需同时使队尾指针为空 */
if(hq->front == NULL){
hq->rear = NULL;
}
free(p); /* 回收原队首结点 */
return temp; /* 返回被删除的队首元素值 */
}
/* 4.读取队首元素 */
elemType peekQueue(struct queueLK *hq)
{
/* 若链队为空则停止运行 */
if(hq->front == NULL){
printf("队列为空,无法删除! ");
exit(1);
}
return hq->front->data; /* 返回队首元素 */
}
/* 5.检查链队是否为空,若为空则返回1, 否则返回0 */
int emptyQueue(struct queueLK *hq)
{
/* 判断队首或队尾任一个指针是否为空即可 */
if(hq->front == NULL){
return 1;
}else{
return 0;
}
}
/* 6.清除链队中的所有元素 */
void clearQueue(struct queueLK *hq)
{
struct sNode *p = hq->front; /* 队首指针赋给p */
/* 依次删除队列中的每一个结点,最后使队首指针为空 */
while(p != NULL){
hq->front = hq->front->next;
free(p);
p = hq->front;
} /* 循环结束后队首指针已经为空 */
hq->rear = NULL; /* 置队尾指针为空 */
return;
}
void read(struct queueLK *hq)
{
struct sNode *p;
struct sNode *q;
p=hq->front;
q=p->next;
if(hq->front==NULL)
printf("clear\n");
else
{
while(q->next!=NULL)
{
printf("%d ",p->data);
p=q;
q=p->next;
}
printf("%d ",p->data);
printf("%d ",q->data);
printf("\n");
}
}
/************************************************************************/
int main()
{
int m=0;
char s;
int sys=1;
struct queueLK q1;
struct queueLK q2;
struct queueLK q3;
double a[8] = {200721, 200715, 200719, 200726, 200728, 200767, 200772,200770};
int i;
int newstudent;
int update;
initQueue(&q1);
initQueue(&q2);
initQueue(&q3);
for(i = 0; i < 8; i++){
if(m<5)
{
enQueue(&q1, a[i]);
m++;
}
else if(m>=5 && m<10)
{
enQueue(&q2,a[i]);
m++;
}
else
{enQueue(&q3,a[i]);
m++;
}
}
while(sys)
{
printf(" 系统功能 \n");
printf(" a 学生预约实验时间\n");
printf(" b 教师查询 \n");
printf(" c 删除预约 \n");
printf(" d 输出每个时间预约情况\n");
printf(" e 退出 \n");
printf("请选择");
scanf("%c",&s);
switch(s)
{
case 'a':
{
printf("请输入要插入的学号\n"); printf("********\n");
scanf("%d",&newstudent);
a[m]=newstudent ;
if(m<5)
{
enQueue(&q1, a[i]);
m++;
i++;
read(&q1);
printf("\n");
}
else if(m>=5 && m<10)
{
enQueue(&q2,a[i]);
m++;
i++;
read(&q2);
printf("\n");
}
else if(m>=10 && m<15)
{
enQueue(&q3,a[i]);
m++;
i++;
read(&q3);
printf("\n");
}
}
break;
case 'b':
{
int i;
printf("请输入要查询哪个时间段的预约 周一(1) 周三(2) 周五(3)\n");
scanf("%d",&i);
if(i==1)
{
if(!emptyQueue(&q1))
{
printf("grop1********\n");
read(&q1);
}
}
else if(i==2)
{
if(!emptyQueue(&q2))
{
printf("grop2********\n");
read(&q2);
}
}
else if(i==3)
{
if(!emptyQueue(&q3))
{
printf("grop3********\n");
read(&q3);
}
}
else
printf("输入错误\n");
}
break;
case 'c':
{
int i;
printf("请输入您要删除的时间段周一(1) 周三(2) 周五(3)\n");
scanf("%d",&i);
if(i==1)
clearQueue(&q1);
else if(i==2)
clearQueue(&q2);
else if(i==3)
clearQueue(&q3);
else
printf("输入错误\n");
}
break;
case 'd':
{
if(!emptyQueue(&q1)){
printf("周一********\n");
read(&q1);
}
if(!emptyQueue(&q2)){
printf("周三********\n");
read(&q2);
}
if(!emptyQueue(&q3)){
printf("周五********\n");
read(&q3);
}
printf("%d",m);
printf("*********************\n");
}
break;
case 'e':
sys=0;
break;
default:
printf("请重新选择\n");
break;
}
}
system("pause");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -