




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上 数据结构第一次作业1.源程序#include<stdio.h>#include<malloc.h>#include<string.h>#include <stdlib.h>#include <iostream>typedef structint no;char name100;char Snumber20;char Pnumber20;char QQ20;char sex50; student; /学生信息的类型 typedef student elemptype;typedef struct nodeel
2、emptype data;struct node *next;linklist; /定义单链表 linklist *head; /定义单链表头 int start() /控制linklist*creatlist()的结束 char n10="end"char str110; printf("结束输入end,否则请按回车!nn"); printf("请输入:");gets(str1);printf("nn"); if(strcmp(n,str1)=0)getchar();return(0); else getchar
3、();return(1);linklist*creatlist() int a; char ch10; elemptype x; linklist *head,*r,*p; p=(linklist*)malloc(sizeof(linklist); head=p; p->next=NULL; r=p; while(1) printf("n请输入学号:n"); scanf("%d",&x.no); printf("n请输入姓名:n"); scanf("%s",&); getchar(
4、); printf("n请输入专业n"); scanf("%s",&x.Snumber); getchar(); printf("n请输入电话号码n"); scanf("%s",&x.Pnumber); getchar(); printf("n请输入QQn"); scanf("%s",&x.QQ); getchar(); printf("n请输入性别n"); scanf("%s",&x.sex); get
5、char(); p=(linklist*)malloc(sizeof(linklist); p->data=x; p->next=NULL; r->next=p; r=r->next; a=start(); if(a=0)break; system("pause");system("cls"); return(head); void insert(linklist*head,int x,elemptype y)linklist *q,*p,*r;r=(linklist*)malloc(sizeof(linklist); r->
6、;data=y; if(head->next=NULL) head->next=r; r->next=NULL; else q=head;p=head->next; while(p!=NULL)&&(p->data.no!=x) q=p;p=p->next; if(p!=NULL) q->next=r; r->next=p; else q->next=r; r->next=NULL; linklist *del(linklist*head,int i)int j=0;linklist *p,*s,*q;p=head;j
7、=0;while(p->next!=NULL)&&(j<i-1)p=p->next; j+;if(p->next!=NULL)q=p->next;p->next=p->next->next;free(q);else return NULL; s=head; return s; linklist *locate(linklist *head,int k) linklist *s;s=head->next;while(s!=NULL)if(s->data.no!=k) s=s->next; else break; r
8、eturn s;linklist *get(linklist *head,int i)int j;linklist *p;p=head;j=0;while(p->next!=NULL)&&(j<i)p=p->next;j+;if(i=j)&&(i!=0) return p;else return NULL;void outlin(linklist*h) linklist*p; p=h->next; while(p!=NULL) printf("%d,%s,%s,%s,%s,%sn",p->data.no,p-&g
9、t;,p->data.Snumber,p->data.Pnumber,p->data.QQ,p->data.sex); p=p->next; printf("n 输出结束 n"); void Menu_one() /主菜单 printf("nnnnnnn");printf(" *n");printf(" 存储学生信息的线性表nn");printf(" 1、用单链表来创建n");printf(" 2、用顺序表来创建n");prin
10、tf(" 3、返回n");printf(" *n");printf("nnntt");void Menu_two_2() /次菜单 printf("nnnnnnn");printf(" *n");printf(" 用单链表存储学生信息nn");printf(" 1、录入学生信息(只能录入一次!)n");printf(" 2、往线性表插入一个信息n");printf(" 3、在线性表中删除一个信息n");printf(
11、" 4、按值检索n");printf(" 5、按序号检索n");printf(" 6、遍历线性表n");printf(" 7、退出n");printf(" *n");printf("nnntt");void main_switch_2(char j)int i;linklist *k; elemptype m,n; int a,b; switch(j) case '1': printf("请添加学生信息n"); head=creatlist
12、(); outlin(head); system("pause");system("cls"); break; case '2': printf("n在第几个信息之前插入?(输入学号)");printf("n请输入:");scanf("%d",&m.no);printf("nnn准备插入的新信息nnn");printf("n请输入学号:n"); scanf("%d",&n.no); printf(&quo
13、t;n请输入姓名:n"); scanf("%s",&); printf("n请输入专业n"); scanf("%s",&n.Snumber); printf("n请输入电话号码n"); scanf("%s",&n.Pnumber); printf("n请输入QQn"); scanf("%s",&n.QQ); printf("n请输入性别n"); scanf("%s"
14、;,&n.sex);insert(head,m.no,n) ;printf("nn新的学生信息表n"); outlin(head); system("pause");system("cls"); break; case '3': outlin(head);printf("n请输入要删除第几个信息:n");scanf("%d",&a);printf("n新的信息表n");del(head,a);outlin(head);system("
15、pause");system("cls"); break; case '4':printf("n请输入你要查找的学号:n");scanf("%d",&a);k=locate(head,a);printf("%d,%s,%s,%s,%s,%sn",k->data.no,k->,k->data.Snumber,k->data.Pnumber,k->data.QQ,k->data.sex); system("pause&qu
16、ot;);system("cls");break; case '5':printf("n请输入你要查找的序号:n");scanf("%d",&a);k=get(head,a);printf("%d,%s,%s,%s,%s,%sn",k->data.no,k->,k->data.Snumber,k->data.Pnumber,k->data.QQ,k->data.sex);system("pause");system(&
17、quot;cls"); break; case '6': printf("n学生信息表nn"); outlin(head); system("pause");system("cls"); break; case '7': exit(0); int main1()char a100; head=(linklist*)malloc(sizeof(linklist); head->next=NULL; /生成头结点 while(1) Menu_two_2();printf("nt请输
18、入功能编号:");gets(a);system("pause");system("cls"); if(a1!='0')printf("n输入错误n");system("pause");system("cls");continue; elseif(a0='0') break;main_switch_2(a0);return 0; /-以下为顺序表的函数-using namespace std;struct student1 int id; / 学号 cha
19、r name30; / 姓名 char sex2; / 性别 char Pnumber20; char QQ20; float gread; / 成绩;typedef struct student1 DataType; / 指定struct student为DataTypestruct SeqList int MAX; / 顺序表中最大元素的个数 int count; / 存放线性表中元素的个数count <= MAXLENGTH DataType *element; / element0, element1, ., elementn - 1存放线性表中的元素 DataType *m;t
20、ypedef struct SeqList *MySeqList; / 初始化并创建空顺序表/ 功能:在顺序表中求某元素的下标,没有查找到,则返回-1int locateSeqList(MySeqList mySeqList, int id) for (int i = 0; i < mySeqList->count; +i) if (mySeqList->elementi.id = id) / 传入一个元素x,查找到后返回下标i return (i); return (-1);int updateSeqList(MySeqList mySeqList, int id) /学生
21、信息修改 int iRc = locateSeqList(mySeqList, id); if (iRc = -1) printf("不存在指定下标!n"); return (0); cout<<"姓名:" cin>>mySeqList->elementiR; cout<<"学号: " cin>>mySeqList->elementiRc.id; cout<<"性别: " cin>>mySeqList->eleme
22、ntiRc.sex; cout<<"电话: " cin>>mySeqList->elementiRc.Pnumber; cout<<"QQ: " cin>>mySeqList->elementiRc.QQ; cout<<"成绩: " cin>>mySeqList->elementiRc.gread; return 1; / 功能: 创建空顺序表MySeqList initSeqList(int m) MySeqList mySeqList = (
23、MySeqList)malloc(sizeof(struct SeqList); / 分配内存空间 if (mySeqList != NULL) mySeqList->element = (DataType*)malloc(sizeof(DataType) * m); / 为里面的元素分配m个DataType大小的内存空间,相当于初始化了一个长度为m的数组 if (mySeqList->element) mySeqList->MAX = m; / 如果创建了元素,MAXLENGTH为最大元素的个数 mySeqList->count = 0; / 空表长度为0 retur
24、n (mySeqList); else free(mySeqList); / 记得要手动释放空间,否则很容易产生内存泄漏 printf("内存空间不足,请关闭一些程序,然后再试!n"); / 存储分配失败,提示空间不足 return NULL;/ 功能: 判断线性表是否为空int isEmptySeqList(MySeqList mySeqList) return (mySeqList->count =0);/ 功能:顺序表的pos下标后面插入,插入成功返回1,失败返回0int insertNextSeqList(MySeqList mySeqList, int po
25、s, DataType x) if (pos < 0 | pos >= mySeqList->count) printf("不存在指定下标!n"); return (0); +mySeqList->count; if (mySeqList->count >= mySeqList->MAX) -mySeqList->count; printf("表产生了溢出!n"); return (0); for (int i = mySeqList->count - 1; i != pos + 1; -i) myS
26、eqList->elementi = mySeqList->elementi - 1; / 同样地,把pos+1插入位置及之后的元素均后移一个位置 mySeqList->elementi = x; / 插入元素x return (1); / 功能:顺序表的删除(根据下标删除)int deleteSeqList(MySeqList mySeqList, int pos) if (pos < 0 | pos >= mySeqList->count) / 不存在下标为pos的元素,注意下标范围是从0到count-1 printf("不存在指定下标!n&q
27、uot;); return (0); for (int i = pos; i < mySeqList->count - 1; +i) mySeqList->elementi = mySeqList->elementi + 1; / 被删除元素之后的元素均前移一个位置 -mySeqList->count; / 元素个数减1 return (1);/ 功能:根据元素值删除,实现顺序表的删除int deleteSeqListByValue(MySeqList mySeqList, int id) int pos = locateSeqList(mySeqList, id
28、); if (pos = -1) printf("不存在指定下标!n"); return (0); deleteSeqList(mySeqList, pos); return (1); / 输出线性表的元素值void printSeqList(MySeqList &mySeqList) for (int i = 0; i < mySeqList->count; +i) / 输出线性表的元素值 cout<< "学号:" << mySeqList->elementi.id << ",姓名
29、:" << mySeqList-> << ",性别:" << mySeqList->elementi.sex ; cout<< ",电话:" << mySeqList->elementi.Pnumber<< ",QQ:" << mySeqList->elementi.QQ<<",成绩:" << mySeqList->elementi.gread
30、; cout<<endl; cout << endl;/ 根据学生id,输出线性表的元素值void printSeqListById(MySeqList &mySeqList,int id) for (int i = 0; i < mySeqList->count; +i) / 输出线性表的元素值 if (id = mySeqList->elementi.id) cout<< "学号:" << mySeqList->elementi.id << ",姓名:" &l
31、t;< mySeqList-> << ",性别:" << mySeqList->elementi.sex ; cout<< ",电话:" << mySeqList->elementi.Pnumber<< ",QQ:" << mySeqList->elementi.QQ<<",语文:" << mySeqList->elementi.gread; cout<
32、<endl; break; int main2()MySeqList mySeqList = initSeqList(20); / 初始化一个长20的表L: system("cls"); printf("nnnnnnn");printf(" *n");printf(" 用顺序表存储学生信息nn");printf(" 1、录入学生信息n");printf(" 2、查找学生信息n");printf(" 3、删除学生信息n");printf("
33、4、修改学生信息n");printf(" 5、遍历学生信息n");printf(" 6、退出学生系统n");printf(" *n");printf("nnntt"); int i; cout<<"请选择一个操作(1-5):" cin>>i; system("pause"); system("cls"); if (i = 1) mySeqList->count = 1; int iRc = 0; while(true
34、&&mySeqList->count<20) cout<<endl<<"请添加学生信息(输入*退出添加):"<<endl; cout<<"姓名:" cin>>mySeqList->elementiR; if (strcmp(mySeqList->elementiR,"*") = 0) mySeqList->count-; goto L; cout<<"学号: " cin>
35、;>mySeqList->elementiRc.id; cout<<"性别: " cin>>mySeqList->elementiRc.sex; cout<<"电话: " cin>>mySeqList->elementiRc.Pnumber; cout<<"QQ: " cin>>mySeqList->elementiRc.QQ; cout<<"成绩: " cin>>mySeqList->
36、;elementiRc.gread; cout << "成功添加学生成绩信息成绩。"<<endl; printSeqList(mySeqList); system("pause"); system("cls"); mySeqList->count+; iRc+; else if (i = 2) L4: cout<<"请输入要查找的学生学号:"<<endl; int sid; cin>>sid; if (locateSeqList(mySeqList,
37、sid) != -1) cout<<"成功查询学号为"<<sid<<"的学生成绩。"<<endl; printSeqListById(mySeqList,sid); else cout<<"查询学生成绩错误,可能不存在学号为"<<sid<<"的学生."<<endl; int iopselect; cout<<endl<<"还要继续查询吗?(按0返回主菜单,否则继续此操作。)"&
38、lt;<endl; cin>>iopselect; if (iopselect = 0) goto L ; else goto L4; else if (i = 3) L1: cout<<"请输入要删除的学生学号:"<<endl; int stu_id; cin>>stu_id; if (deleteSeqListByValue(mySeqList,stu_id) = 1) cout<<"成功删除学生成绩。"<<endl; else cout<<"删除学生
39、成绩出错。"<<endl; printSeqList(mySeqList); int iop; cout<<endl<<"还要继续删除吗?(按0返回主菜单,否则继续此操作。)"<<endl; cin>>iop; if (iop = 0) goto L ; else goto L1; else if (i = 4) L3: cout<<"请输入要修改的学生学号:"<<endl; int id; cin>>id; if(updateSeqList(myS
40、eqList,id) =1) cout << "成功修改学生成绩信息成绩。"<<endl; else cout << "修改学生成绩信息成绩出错。"<<endl; printSeqList(mySeqList); int iselect; cout<<endl<<"还要继续修改吗?(按0返回主菜单,否则继续此操作。)"<<endl; cin>>iselect; if (iselect = 0) goto L ; else goto L3;
41、else if (i = 5) system("cls"); printSeqList(mySeqList); system("pause"); system("cls"); goto L ; else if (i = 6) system("cls"); cout<<"您已经出本系统,欢迎下次再使用."<<endl; return 0;int main() int q;Menu_one();printf("请输入序号:"); scanf("%d",&q);getchar();system("cls");if(q=1)main1();else if(q=2)main2(); elseexit(0); 2. 结果截图单链表:1录入信息2 插入信息3 删除信息4 按值检索6 按序号检索顺序表:1 录入信息2 查找信息3 删除信息4 修改信息小组成员任务分配单链表: 湛嘉琪,王小雨顺序
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 清明上河图的历史背景与艺术价值:八年级美术教案
- 时间极限皮秒课件
- 关于梦想的中考作文(12篇)
- 早期发现课件
- 商业智能咨询及服务合同条款
- 500字左右的教师节作文14篇
- 产品采购供应合同及质量保证条款
- 工地混凝土输送泵车出租合同
- 纪念七七事变课件
- 2025年磨工(中级)考试试卷:磨削加工教育与培训体系
- 2024年10月成都市金牛区人民政府西华街道办事处公开招考1名编外人员笔试历年典型考题(历年真题考点)解题思路附带答案详解
- 2025年牙医资格证技能试题及答案
- 初中道德与法治跨学科项目化学习的设计与实施讲座提纲
- DG-TG08-12-2024 普通中小学建设标准
- 《物业管理培训课件:业主满意度提升策略》
- 2025船舶抵押合同范本
- 金融标准化知识培训课件
- 2024年医销售药销售工作总结
- 2025年中国茯苓种植市场全面调研及行业投资潜力预测报告
- 医师规范化培训
- 监理跟踪、平行检测计划
评论
0/150
提交评论