以 “ 链式队列 " 为数据结构所编写的。
源代码:
#include#include #include #define newNode (stu*)malloc(sizeof(stu)) typedef struct Student{ char name[20]; char id[20]; struct Student *next; }stu; typedef struct p{ struct Student *head,*tail; }headnode; void init(headnode *h){//初始化 h->head=h->tail=NULL; return; } void add(headnode *h){//1、入队 stu *p; p=newNode; p->next=NULL; printf("请输入学生的信息:n"); printf("姓名:"); scanf("%s",p->name); printf("学号:"); scanf("%s",p->id); if(h->head==NULL){//如果队列为空,新插入的结点为队列中唯一一个结点 h->head=p; h->tail=p; return; } //队列不为空 h->tail->next=p; h->tail=p; return; } node *pop(queue *q){ node *p; p=q->head; if(p==NULL) { printf("队列为空"); return q;} q->front=p->next; free(p); if(q->front==NULL) q->rear=NULL; return q; } int empty(headnode *h){ return h->tail=h->head?1:0; } void print(headnode *h){//3、遍历 for(stu *i=h->head; i!=NULL; i=i->next){ printf("姓名:%sn",i->name); printf("学号:%sn",i->id); } } void xiugai(headnode *h){//4、修改 char nu[20]; int state; printf("请输入您要修改的学生学号:n"); scanf("%s",nu); stu *q=h->head; while(q!=NULL){ if(strcmp(q->id,nu)==0){ state=1; printf("请输入您要修改的信息选项:1.姓名 2.学号n"); int choose; scanf("%d",&choose); if(choose==1){ printf("请输入您要修改的名字:"); scanf("%s",q->name); printf("修改名字成功!n"); } if(choose==2){ printf("请输入您要修改的学号:"); scanf("%s",q->id); printf("修改学号成功!n"); } } q=q->next; } if(state==0) { printf("该生不存在n"); } } void find(headnode *h){//5、检索 char nu[20]; int state; printf("请输入学生学号:"); scanf("%s",nu); stu *q=h->head; while(q!=NULL){ if(strcmp(q->id,nu)==0){ state=1; printf("姓名:%sn",q->name); printf("学号:%sn",q->id); } q=q->next; } if(state==0) { printf("该生不存在!n"); } } int main() { printf("学生信息队列基本功能菜单n"); printf("==========================n"); printf("1.添加学生信息(入队)n"); printf("2.删除学生信息(出队)n"); printf("3.显示学生信息(遍历)n"); printf("4.修改学生信息(修改)n"); printf("5.查找学生信息(检索)n"); printf("6.退出学生信息队列操作n"); printf("==========================n"); //初始化队列 headnode *h; h=newNode; init(h); while(1){ printf("请选择功能:"); int x;scanf("%d",&x); switch(x){ case 1:add(h);break; case 2:{ stu *q; q=pop(h); if(q!=NULL){ printf("姓名:%sn",q->name); printf("学号:%sn",q->id); } break; } case 3:print(h);break; case 4:xiugai(h);break; case 5:find(h);break; case 6:printf("系统已退出!");exit(0); default: printf("请输入正确的选择:"); break; } } return 0; }



