用c语言定义一个完整包含学生信息的链队列,使其具有以下功能:1.初始化一个空队列,2.诸葛输入学生信息并入队,3.出队,4.删除队列,5统计当前队列排队学生个数,6.设计一个绿色通道,设计完整代码
时间: 2023-05-27 13:03:50 浏览: 109
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct student{
char name[MAX_NAME_LEN];
int age;
char gender;
struct student *next;
} Student;
void initQueue(Student **front, Student **rear) {
*front = (Student*)malloc(sizeof(Student));
(*front)->next = NULL;
*rear = *front;
}
void enqueue(Student **rear) {
Student *s = (Student*)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", s->name);
printf("请输入学生年龄:");
scanf("%d", &(s->age));
printf("请输入学生性别(M/F):");
scanf(" %c", &(s->gender));
s->next = NULL;
(*rear)->next = s;
(*rear) = s;
}
void dequeue(Student **front) {
if ((*front)->next == NULL) {
printf("队列已空,无法出队!\n");
}
else {
printf("%s出队!\n", (*front)->next->name);
(*front) = (*front)->next;
}
}
void deleteQueue(Student **front) {
Student *p = NULL;
while ((*front)->next) {
p = (*front)->next;
(*front)->next = p->next;
free(p);
}
printf("队列删除成功!\n");
}
int getQueueLength(Student *front) {
int len = 0;
Student *p = front->next;
while (p) {
p = p->next;
len++;
}
return len;
}
void greenChannel(Student **front) {
if ((*front)->next == NULL) {
printf("当前队列已空,无需绿色通道!\n");
}
else {
printf("%s通过绿色通道!\n", (*front)->next->name);
(*front) = (*front)->next;
}
}
void menu() {
printf("1.初始化一个空队列\n");
printf("2.添加学生信息并入队\n");
printf("3.出队\n");
printf("4.删除队列\n");
printf("5.统计当前队列排队学生个数\n");
printf("6.绿色通道\n");
printf("请输入相应数字选择功能:");
}
int main() {
int choice = 0;
Student *front, *rear;
initQueue(&front, &rear);
while(1) {
menu();
scanf("%d", &choice);
if (choice == 1) {
printf("初始化队列成功!\n");
}
else if (choice == 2) {
enqueue(&rear);
}
else if (choice == 3) {
dequeue(&front);
}
else if (choice == 4) {
deleteQueue(&front);
initQueue(&front, &rear);
}
else if (choice == 5) {
printf("当前队列排队的学生个数为:%d\n", getQueueLength(front));
}
else if (choice == 6) {
greenChannel(&front);
}
else {
printf("输入的数字不在选项之内,请重新输入!\n");
}
}
return 0;
}
```
阅读全文