简单的学生信息链表
来源:互联网 发布:怎么编程游戏我的世界 编辑:程序博客网 时间:2024/06/10 06:05
题目设定是:
建立一个链表,每个节点的信息为:学号是16位数字,学生信息包括学号,姓名,年龄,籍贯基本信息,完成以下功能。支持学生信息增加、查找、删除。
#include<iostream>#include<string.h>#include<stdio.h>#include<conio.h>using namespace std;typedef struct student{char stuid[20];char name[10];int age;char birthplace[10];struct student *next;}node;char redo(){char action;cout<<"Next operation:"<<endl;cout<<"1: To continue on the current operation"<<endl;cout<<"2: To return to the main menu"<<endl;// 不停的等待用户输入,直到输入正确while(1){cin>>action;if(action!='1' && action!='2'){cout<<"No corresponding operation! Please check and reset:";}else{return action;break;}}}void Print(node *p){printf("%16s%16s%16d%16s\n",p->stuid,p->name,p->age,p->birthplace);}void printAll(node* head){node *p;p = head->next;printf("%16s%16s%16s%16s\n","STUID","NAME","AGE","BIRTHPLACE");for(int i = 0; i<64 ; i++)cout<<"*";cout<<endl;while(p != NULL){printf("%16s%16s%16d%16s\n", p->stuid, p->name, p->age, p->birthplace);p = p->next;}printf("\nNo corresponding menu.Please press any key to return the main menu..."); getch();}void seek(node *head, char num)//{char str[20];int count = 0;cout<<"please input the information you seek:";cin>>str;node *p = head->next;while(p != NULL){switch(num){case '1': if(strcmp(p->stuid, str) == 0) {count++;Print(p);} break;case '2': if(strcmp(p->name , str) == 0) {count++;Print(p);}break;}p = p->next;}if(count == 0)cout<<"No relative information about '"<<str<<"'!"<<endl;}void menu(void){cout<<"* * * * * * * * * * * * * * * * * *"<<endl;cout<<"* Linklist Menu *"<<endl;cout<<"* 1: Add *"<<endl;cout<<"* 2: Search *"<<endl;cout<<"* 3: Delete *"<<endl;cout<<"* 4: Print *"<<endl;cout<<"* 0: Exit *"<<endl;cout<<"* * * * * * * * * * * * * * * * * *"<<endl;cout<<endl;cout<<"Input Linklist Menu Number:";}bool select(char option){bool decision;switch(option){case 'y':case 'Y':return decision = true;break;default:return decision = false;break;}return decision;}node *create(void)//创建空链表,返回表头,head{node *head;head = (node*)malloc(sizeof(node));head->next = NULL;return head;}node *add(node *head)//返回表头{cout<<"-*- - - - - - - - - - -Add- - - - - - - - - - - -*-"<<endl;cout<<"Please input STUID, NAME, AGE, BIRTHPLACE in order!"<<endl;node *p, *s;bool cycle = true;char selection;p = head;while(p->next != NULL){p = p->next;}while(cycle){cout<<"Add Infu:"<<endl;s = (node*)malloc(sizeof(node));p->next = s;cin>>s->stuid;cin>>s->name;cin>>s->age;cin>>s->birthplace;p = s;cout<<"Continue to enter(Y/any key):";cin>>selection;cycle = select(selection);}p->next =NULL;cout<<endl;return head;}node *search(node *head){cout<<"* * * * * * * * * * * * * * * * * * * * * "<<endl;cout<<"* Search Preferences *"<<endl;cout<<"* 1.by stuid *"<<endl;cout<<"* 2.by name *"<<endl;cout<<"* * * * * * * * * * * * * * * * * * * * *"<<endl;cout<<endl;cout<<"Input Search Mode:";char i;while(1){cin>>i;if(i != '1' && i != '2')cout<<"No corresponding menu. Please reset:";elsebreak;}seek(head, i);return head;}node* del(node *head)//删除节点操作需要三个节点,前一个h0,当前h1,下一个h2.{cout<<"-*- - - - - - - - - - -Del- - - - - - - - - - - -*-"<<endl;cout<<endl;node *p0, *p, *s;//p存储当前的节点h1,p0存储p的前一个节点h0。int count = 0;p0 = head;p = head->next;cout<<"Please input the NAME in the information you will delete: ";char str1[20];cin>>str1;while(p != NULL){if(strcmp(p->name ,str1) == 0)//若p为所删节点h1{count++;s = p; //p赋给s,s为h1p = p->next; //此时p指向下一个节点为h2p0->next = p; //p0为h0,所以要释放h1的空间,需要h0->next指向h2(也就是p)的首地址。free(s);cout<<"Deleted Successfully!"<<endl;continue;}p0 = p;p = p->next;}if(count == 0){cout<<"No relative information about that you will delete!"<<endl;}return head;}void main(void){node *head = NULL;char num;head = create();while(1){system("CLS");menu();cin>>num;system("CLS");switch(num){case '1': head = add(head); break;case '2': while(1) { head = search(head); if(redo() == '1') system("CLS"); else break; } break;case '3': while(1) { head = del(head); if(redo() == '1') system("CLS"); else break; } break;case '4': printAll(head);break;case '0': exit(0);break;default : printf("No corresponding menu.Please press any key to return the main menu..."); getch();}}}
0 0
- 简单的学生信息链表
- 简单的学生信息表
- 第八周项目-建立一个简单的学生信息表
- 学生信息链表
- C++ 简单的学生信息存储系统
- 简单的学生信息录入程序
- 学生-简单链表
- 学生信息链表实现
- 链表保存学生信息
- 建立学生信息链表
- 建立学生信息链表
- 建立学生信息链表
- 学生信息是:姓名,学号,性别,年龄,用一个链表,把这些信息连在一起,给出一个age, 在些链表中删除学生年龄等于age的学生信息。
- 链表实现一个学生信息统计的功能
- 数据结构 链表的应用——学生信息统计
- 【Java】StudentsInfoQuery(简单的学生信息查询系统)
- 使用ListView实现一个简单的学生信息展示效果
- 一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息。
- 软件架构师推荐书籍
- 0910Android音乐播放器
- 【C++面向对象学习笔记】-构造函数和析构函数
- MEMS惯性传感器
- System中的一些属性(Properties属性)如何获取系统属性和指定系统属性
- 简单的学生信息链表
- ngrok 内网穿透利器
- java设计模式第5弹--责任链模式
- C语言学习之结构体struct
- android开发步步为营之76:android导出anr日志
- uva 12299 - RMQ with Shifts--- 线段树单点更新
- 黑马程序员——C语言入门总结
- USACO 1.2 Palindromic Squares (进制转换)
- caffe for windows 模型中 solver.prototxt 代码解读