c++创建线性链表记录学生信息
来源:互联网 发布:sigmaplot linux 编辑:程序博客网 时间:2024/06/16 06:07
记录如图所示的学生信息表格
调用程序时先输入学生的个数,然后依次输入学生的学号,姓名等信息
#include<iostream>#include<string>using namespace std;/* 创建节点 */struct Student{ char num[10]; string name; string sex; int age; string nation; string political; string place; Student *next; //存放下一个节点的地址};/* 创建一个链表 */Student *Creat(){ Student *p, *q, *head; //p存放新节点地址,q存放上一个节点地址(当前链表的最后一个节点地址),head存放头节点地址 int i = 1, number; //number记录学生个数 cout << "please input numbers' number: " << endl; cin >> number; cout << endl; head = NULL; q = NULL;// 指针初始化 while (i <= number) { p = new Student; //创建一个新节点(空间大小为Student型占用的空间大小) cout << "please input number: "<< i << endl; cin >> p->num; cout << "please input name: "<< i << endl; cin >> p->name; cout << "please input sex: "<< i << endl; cin >> p->sex; cout << "please input age: "<< i << endl; cin >> p->age; cout << "please input nation: "<< i << endl; cin >> p->nation; cout << "please input political: "<< i << endl; cin >> p->political; cout << "please input place: "<< i << endl; cin >> p->place; if (head == NULL) //头节点为0,表示链表为空 { head = p; //将p设置为头节点的地址 q = p; //创建第一个节点时,q也指向头节点 } else //已有头节点,将新创建的节点p放在上一个节点q后面 { q->next = p; //新创建的节点放在q后面 q = p; //将q指向新链表的最后一个节点 } i++; //当前节点数加1 } q->next = NULL; //最后一个节点的next成员为NULL return head;}int main(){ Student *head,*p; cout<<"start\n"<<endl; head = Creat(); p=head; cout<<"print\n"<<endl; cout<<"number name sex age nation political place"<<endl; while(p!=NULL) { cout<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->age; cout<<p->nation<<" "<<p->political<<" "<<p->place<<endl; p = p->next; } return 0;}
附:查找年龄为20岁的算法,在上面主程序的main函数 return 0 语句前面添加一句find_age(head,20);
,然后将查找子函数放到main函数前面即可,如图所示:
查找函数程序为:
Student *find_age(Student *f,int age){ cout<<"start to find age!"<<endl; while(f!=NULL) { if(f->age == age) { cout<<f->num<<" "<<f->name<<" "<<f->sex<<" "<<f->age; cout<<f->nation<<" "<<f->political<<" "<<f->place<<endl; } f = f->next; } return 0;}
顺便再补充打印链表长度和删除链表元素的函数:
(一)首先是打印链表长度函数:
Student *lengthList(Student *l){ cout<<"start to compute length of list !"<<endl; int i=0; while (l!=NULL) { i++; l = l->next; } cout<<"the length of list is "<< i << endl; return 0;}
(二)然后是删除链表元素函数:(此函数功能是删除链表,并打印删除后的链表以及链表长度)
Student *delete_one(Student *l){ string num1; string num2; num1 = "003"; num2 = "005"; Student *s,*p; s = l;p=l; cout<<"start to delet !"<<endl; while(l!=NULL) { if(l->next==NULL) break; if(l->next->num==num1) l->next = l->next->next; else if (l->next->num == num2) l->next=l->next->next; else l=l->next; } cout<<"number name sex age nation political place"<<endl; while (s!=NULL) { cout<<s->num<<" "<<s->name<<" "<<s->sex<<" "<<s->age; cout<<s->nation<<" "<<s->political<<" "<<s->place<<endl; s = s->next; } lengthList(p); return 0;}
0 0
- c++创建线性链表记录学生信息
- C 链表记录学生信息
- 合并表记录(C/C++)
- 创建块定义(块表记录)
- 创建表记录数据库虚拟日志文件的情况
- postgresql通过创建规则(RULE)实现表记录
- postgresql通过创建规则(RULE)实现表记录
- 循环删除表记录
- 随机取表记录
- oracle表记录恢复
- mysql复制表记录
- 表记录的检索
- 合并表记录
- 合并表记录
- 合并表记录
- 合并表记录
- 合并表记录
- 合并表记录
- 5-52 单词长度 (15分)
- hibernate的小问题
- 如鹏java学习进程 将输入的正整数相加,ok结尾
- 适配器模式Adapter
- 问题记录:myeclipse配置好struts2后,web项目运行出现404
- c++创建线性链表记录学生信息
- 计蒜客 跳跃游戏 贪心
- Android开发:Toolbar基本使用和自定义Toolbar
- try 代码块中含 return 语句时,代码执行顺序
- 浅淡Webservice、WSDL三种服务访问的方式(附案例)
- LVM介绍及使用
- COJ-1060-Nearest Sequence
- scala中yield的使用
- 关于spring AOP的浅薄理解