一个链表程序,支持建立,插入,删除,输出;学生的学号和分数
2011-06-22 15:19
429 查看
#include <iostream.h> #include <malloc.h> #define NULL 0 #define LEN sizeof(struct stu) #define BY (struct stu *)malloc(LEN) typedef struct stu STU; struct stu { long int num; float score; struct stu *next; }; int n; struct stu *creat()//建立链表 { struct stu *p2,*p1,*head; n=0; p1=p2=BY; cin>>p1->num>>p1->score; head=NULL; for(;p1->num!=NULL;) { n+=1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=BY; cin>>p1->num>>p1->score; } p2->next=NULL; return head; } void print(struct stu *head)//查询链表 { struct stu *p; cout<<"putout the students' number and score:/n"; p=head; for(;p!=NULL;) { cout<<"number:"<<p->num<<"/tscore:"<<p->score<<endl; p=p->next; } } STU *insert(STU *head,STU *std)//插入链表 { STU *p0,*p1,*p2; p1=head; p0=std; if(head==NULL) { head=p0; p0->next=NULL; } else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(head==p1)head=p0; else p2->next=p0; p0->next=p1; } else { p1->next=p0; p0->next=NULL; } } n+=1; return head; } STU *dele(STU *head,long int num)//删除链表 { STU *p1,*p2; if(head==NULL) {cout<<"empty list!/n";goto end;} p1=head; while(p1->num!=num&&p1->next!=NULL) {p2=p1;p1=p1->next;} if(p1->num==num) {if(p1==head)head=p1->next; else p2->next=p1->next; n=n-1; free(p1); } else cout<<"the node not been found!/n"; end: return head; } int main(int argc, char* argv[]) { struct stu *cl,*stud; long int num; int i; cout<<"*************************************************/n" <<endl <<"selet the item *****/n" <<"1:set the students' resoure *****/n" <<"2:query the students' resoure *****/n" <<"3:insert the students' resoure *****/n" <<"4:delete the students' resoure *****/n" <<"0:ESC the program! *****/n" <<endl <<"************************************************/n"; do { cout<<"select the number to next/n"; cin>>i; switch(i) { case 1: { cout<<"Please input the students' number and score:"<<endl <<"number/tscore"<<endl; cl=creat(); }break; case 2: { cout<<"number/tscore"<<endl; print(cl); }break; case 4: { cout<<"input the delete number:/n"; cin>>num; cl=dele(cl,num); }break; case 3: { cout<<"input the insert number and score:/n"; stud=BY; cin>>stud->num>>stud->score; cl=insert(cl,stud); }break; case 0: break; default:cout<<"input err/n";break; } }while(i!=0); return 0; }
相关文章推荐
- 编写程序:建立一个学生数据链表,每个节点的信息包括如下内容:学号,姓名,性别年龄专业。对链表做如下处理。输入一个学号(专业),如果链表中的节点包含此学号(专业),则删去该结点。
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- 链表建立输出删除插入的综合实例
- 【4.17】建立一个对象数组,内放6个学生的数据(学号、成绩),用指针指向数组首元素,输出第2,4,6个学生的数剧。
- 创建一个链表,可以插入学生信息、删除学生信息
- 链表的建立,输出,删除,插入运算的c语言实现
- 学生信息是:姓名,学号,性别,年龄,用一个链表,把这些信息连在一起,给出一个age, 在些链表中删除学生年龄等于age的学生信息。
- 例 9.8 建立一个如图9.9所示的简单链表,它由3个学生数据的结点组成,要求输出各结点中的数据。
- 单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 链表的简单操作(建立,输出,插入,删除)
- 【碎片知识(2)· C语言】链表的建立、输出、特定点的删除和插入以及malloc函数的应用
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- [4.21]编写一个程序,已有若干个学生的数据,包括学号、姓名、成绩、要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。
- 建立一个对象数组,内放5个学生数据(学号、成绩),用指针指向数组首元素,输出学生的数据
- 建立一个数组,内存放5个学生的数据(学号、成绩),用指针指向数组首元素,输出1、3、5学生的数据
- c++链表 建立 输入 输出 删除结点 插入结点 释放链表 的函数
- 链表(建立+插入+删除+输出)
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 写一个程序,能够动态生成多条插入语句如:insert into MyStudents values(“人名1”,年龄,‘男’,分数1,分数2)。将生成的插入语句输出到记事本文件 SQLStr.txt中