数据结构.docx

上传人:b****7 文档编号:8859557 上传时间:2023-02-02 格式:DOCX 页数:33 大小:23.50KB
下载 相关 举报
数据结构.docx_第1页
第1页 / 共33页
数据结构.docx_第2页
第2页 / 共33页
数据结构.docx_第3页
第3页 / 共33页
数据结构.docx_第4页
第4页 / 共33页
数据结构.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

数据结构.docx

《数据结构.docx》由会员分享,可在线阅读,更多相关《数据结构.docx(33页珍藏版)》请在冰豆网上搜索。

数据结构.docx

数据结构

一、试题查看

标题:

约瑟夫环

时限:

500ms

内存限制:

2000K

总时限:

3000ms

描述:

约瑟夫环

编号为1,2,3,……,n的n个人按顺时针方向围坐一圈。

任选一个正整数作为报数上限m,从第一个人开始按顺时针方向自1开始顺序报数,报到m时停止报数。

报m的人出列,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有人全部出列为止。

设计程序输出出列顺序。

输入:

人数n报数上限m

人员记录1(格式为:

姓名学号性别年龄班级健康状况)

人员记录2

人员记录n

输出:

第1次报数出列的人员记录

第2次报数出列的人员记录

第n次报数出列的人员记录

输入样例:

53

安弥邵10000001女28计43一般

宰觅10000002男23计79健康

顾健10000003男27计29一般

宓顽芳10000004女20计17健康

能纸垄10000005男18计11健康

输出样例:

顾健10000003男27计29一般

安弥邵10000001女28计43一般

能纸垄10000005男18计11健康

宰觅10000002男23计79健康

宓顽芳10000004女20计17健康

提示:

循环表

来源:

返回

#include

#include

structLnode

{

char*name;

char*number;

char*sex;

intage;

char*classnum;

char*health;

intnum;

structLnode*next;

}*L;

typedefstructLnodeLinklist;

voidCreat(Linklist*q,intn);

voidOpe(Linklist*p,intm);

intmain()

{

intn,m;

scanf("%d%d",&n,&m);

L=(Linklist*)malloc(sizeof(Linklist));

Creat(L,n);

Ope(L,m);

return0;

}

voidCreat(Linklist*L,intn)

{

inti=1;

Linklist*p,*q;

 

p=L;

p->name=(char*)malloc(10*sizeof(char));

p->number=(char*)malloc(10*sizeof(char));

p->sex=(char*)malloc(4*sizeof(char));

p->classnum=(char*)malloc(10*sizeof(char));

p->health=(char*)malloc(6*sizeof(char));

scanf("%s%s%s%d%s%s",p->name,p->number,p->sex,&p->age,p->classnum,p->health);

p->num=i;

p->next=NULL;

for(i=1;i

{

q=(Linklist*)malloc(sizeof(Linklist));

q->name=(char*)malloc(10*sizeof(char));

q->number=(char*)malloc(10*sizeof(char));

q->sex=(char*)malloc(4*sizeof(char));

q->classnum=(char*)malloc(10*sizeof(char));

q->health=(char*)malloc(6*sizeof(char));

scanf("%s%s%s%d%s%s",q->name,q->number,q->sex,&q->age,q->classnum,q->health);

q->num=i+1;

p->next=q;

p=q;

}

p->next=L;

}

voidOpe(Linklist*L,intm)

{

inti=1;

Linklist*pre,*cur;

cur=pre=L;

while(cur->next!

=cur)

{

for(;i

{

pre=cur;

cur=cur->next;

}

pre->next=cur->next;

i=1;

printf("%s%s%s%d%s%s\n",cur->name,cur->number,cur->sex,cur->age,cur->classnum,cur->health);

free(cur);

cur=pre->next;

}

printf("%s%s%s%d%s%s\n",cur->name,cur->number,cur->sex,cur->age,cur->classnum,cur->health);

free(cur);

}

 

#include

#include

#include//malloc()等

#include//INT_MAX等

#include//EOF(=^Z或F6),NULL

#include//atoi()

#include//eof()

#include//floor(),ceil(),abs()

#include//exit()

#defineTRUE1

#defineFALSE0

#defineOK1

#defineERROR0

#defineINFEASIBLE-1

typedefstruct

{

charname[20];

charnum[10];

charsex[3];

intage;

charclas[10];

charhealth[20];

structnode*next;

}ElemType;

typedefstructLNode

{

ElemTypedata;

structLNode*next;

}LNode,*LinkList;

voidRun(LinkListL,intm)

{

LinkListp=L,q;

intj;

if(m==1)

{

for(q=p;q->next!

=p;q=q->next)

printf("%s%s%s%d%s%s\n",q->data.name,q->data.num,q->data.sex,q->data.age,q->data.clas,q->data.health);

printf("%s%s%s%d%s%s\n",q->data.name,q->data.num,q->data.sex,q->data.age,q->data.clas,q->data.health);

}

else

{

while(p->next!

=p)

{

for(j=1;j

{

q=p;

p=p->next;

}

printf("%s%s%s%d%s%s\n",p->data.name,p->data.num,p->data.sex,p->data.age,p->data.clas,p->data.health);

q->next=p->next;

free(p);

p=q->next;

}

printf("%s%s%s%d%s%s\n",p->data.name,p->data.num,p->data.sex,p->data.age,p->data.clas,p->data.health);

}

}

voidGetList(LinkList&L,intn)

{

LinkListp,q;

inti;

L=(LinkList)malloc(sizeof(LNode));

scanf("%s%s%s%d%s%s",L->data.name,L->data.num,L->data.sex,&(L->data.age),L->data.clas,L->data.health);

q=L;

for(i=1;i

{

p=(LinkList)malloc(sizeof(LNode));

scanf("%s%s%s%d%s%s",p->data.name,p->data.num,p->data.sex,&(p->data.age),p->data.clas,p->data.health);

q->next=p;

q=p;

}

q->next=L;

}

intmain()

{

inti,j;

scanf("%d%d",&j,&i);

LinkListL;

GetList(L,j);

Run(L,i);

return0;

}

二、试题查看

标题:

学生信息管理

时限:

1000ms

内存限制:

20000K

总时限:

3000ms

描述:

用链式存储结构实现对一个班级学生信息管理。

设计程序求出每个人的平均成绩并按平均成绩由高到底排序后输出学生记录。

输入:

人数n

人员记录1(格式为:

学号姓名成绩1成绩2成绩3)

人员记录2

输出:

人员记录x1

人员记录y2

人员记录zn

输入样例:

3

1孙俪莉767889

2章子怡725667

3刘德华568490

输出样例:

1孙俪莉76788981.001

3刘德华56849076.672

2章子怡72566765.003

提示:

来源:

返回

#include

#include

#include

typedefintElemType;

typedefstructStudent

{

charnum[30];

charname[10];

floatscore1;

floatscore2;

floatscore3;

floataverage;

structStudent*next;

}student,*stu;

voidCreatCList(stuL,ElemTypen)

{

stup1;

while(n>0)

{

p1=(stu)malloc(sizeof(student));

scanf("%s",p1->num);

scanf("%s",p1->name);

scanf("%f",&p1->score1);

scanf("%f",&p1->score2);

scanf("%f",&p1->score3);

L->next=p1;

L=p1;

n--;

}

L->next=NULL;

}

voidAverage(stuL)

{

stuh;

h=L->next;

while(h)

{

h->average=((h->score1)+(h->score2)+(h->score3))/3;

h=h->next;

}

}

voidList_Sort(stuL)

{

stup,q,r,s,l;

l=L;

p=L->next;

while(p)

{

r=p;

q=p->next;

while(q)

{

if(r->averageaverage)

r=q;

q=q->next;

}

s=p;

if(s->average==r->average&&s->num==r->num)

{

p=p->next;

l->next=r;

l=r;

}

else

{

while(s->next->num!

=r->num)

s=s->next;

s->next=r->next;

l->next=r;

l=r;

}

}

l->next=NULL;

}

voidTraverse(stuL,ElemTypen)

{

ElemTypei=0;

stup;

p=L->next;

while(p)

{

i++;

if(i==n&&n>10)

printf("%s%s%.2f%.2f%.2f%.2f%d\n",p->num,p->name,p->score1,p->score2,p->score3,p->average,i);

else

printf("%s%s%.2f%.2f%.2f%.2f%d\n",p->num,p->name,p->score1,p->score2,p->score3,p->average,i);

p=p->next;

}

}

intmain()

{

ElemTypen;

stuL;

L=(stu)malloc(sizeof(student));

L->next=NULL;

scanf("%d",&n);

CreatCList(L,n);

Average(L);

List_Sort(L);

Traverse(L,n);

return0;

}

三、试题查看

标题:

顺序表上的基本操作实现

时限:

1000ms

内存限制:

10000K

总时限:

3000ms

描述:

在顺序存储结构实现基本操作:

初始化、创建、插入、删除、查找、遍历、逆置、合并运算。

输入:

请输入线性表La的长度:

n

a1a2a3...an(数值有序,为降序)

请输入要插入到线性表La中的数字x和插入的位置i:

xi

请输入要删除数字的位置:

i

请输入要查找的数字:

x

请输入线性表长度:

m

b1b2...bm(数值有序,为升序)

输出:

创建好的线性表La=a1a2...an

插入一个数字后的线性表a1a2...an+1

删除一个数字后的线性表a1a2...an

查找一个输入的数字后如果找到,输出该数字的位置i,如果没有找到,输出"没有找到x"的信息。

逆置a1a2...an后的线性表anan-1...a1

合并两个线性表后的线性表

输入样例:

请输入线性表La的长度:

5

请输入线性表La中的元素:

14111095

请输入要插入到线性表La中的数字x和插入的位置i:

84

线性表La=141110895

请输入要删除的数字的位置:

4

线性表La=14111095

请输入要查找的数字:

10

找到,10在第3个位置

逆置后的线性表La=59101114

请输入线性表Lb的长度:

4

请输入线性表Lb中的元素:

1369

合并La和Lb后的线性表为:

135699101114

输出样例:

提示:

来源:

#include

#include

#include//malloc()等

#include//INT_MAX等

#include//EOF(=^Z或F6),NULL

#include//atoi()

#include//eof()

#include//floor(),ceil(),abs()

#include//exit()

#defineTRUE1

#defineFALSE0

#defineOK1

#defineERROR0

#defineINFEASIBLE-1

typedefintStatus;//Status是函数的类型,其值是函数结果状态代码,如OK等

typedefintElemType;

#defineLIST_INIT_SIZE5//线性表存储空间的初始分配量

#defineLISTINCREMENT2//线性表存储空间的分配增量

structSqList

{

ElemType*elem;//存储空间基址

intlength;//当前长度

intlistsize;//当前分配的存储容量(以sizeof(ElemType)为单位)

};

StatusInitList(SqList&L)//算法2.3

{//操作结果:

构造一个空的顺序线性表

L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));

if(!

L.elem)

exit(OVERFLOW);//存储分配失败

L.length=0;//空表长度为0

L.listsize=LIST_INIT_SIZE;//初始存储容量

returnOK;

}

StatusDestroyList(SqList&L)

{//初始条件:

顺序线性表L已存在。

操作结果:

销毁顺序线性表L

free(L.elem);

L.elem=NULL;

L.length=0;

L.listsize=0;

returnOK;

}

intListLength(SqListL)

{//初始条件:

顺序线性表L已存在。

操作结果:

返回L中数据元素个数

returnL.length;

}

StatusGetElem(SqListL,inti,ElemType&e)

{//初始条件:

顺序线性表L已存在,1≤i≤ListLength(L)

//操作结果:

用e返回L中第i个数据元素的值

if(i<1||i>L.length)

exit(ERROR);

e=*(L.elem+i-1);

returnOK;

}

intLocateElem(SqListL,ElemTypee,Status(*equal)(ElemType,ElemType))

{//初始条件:

顺序线性表L已存在,compare()是数据元素判定函数(满足为1,否则为0)

//操作结果:

返回L中第1个与e满足关系compare()的数据元素的位序。

//若这样的数据元素不存在,则返回值为0。

算法2.6

ElemType*p;

inti=1;//i的初值为第1个元素的位序

p=L.elem;//p的初值为第1个元素的存储位置

while(i<=L.length&&!

equal(*p++,e))

++i;

if(i<=L.length)

returni;

else

return0;

}

StatusListInsert(SqList&L,inti,ElemTypee)//算法2.4

{//初始条件:

顺序线性表L已存在,1≤i≤ListLength(L)+1

//操作结果:

在L中第i个位置之前插入新的数据元素e,L的长度加1

ElemType*newbase,*q,*p;

if(i<1||i>L.length+1)//i值不合法

returnERROR;

if(L.length>=L.listsize)//当前存储空间已满,增加分配

{

if(!

(newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType))))

exit(OVERFLOW);//存储分配失败

L.elem=newbase;//新基址

L.listsize+=LISTINCREMENT;//增加存储容量

}

q=L.elem+i-1;//q为插入位置

for(p=L.elem+L.length-1;p>=q;--p)//插入位置及之后的元素右移

*(p+1)=*p;

*q=e;//插入e

++L.length;//表长增1

returnOK;

}

StatusListDelete(SqList&L,inti,ElemType&e)//算法2.5

{//初始条件:

顺序线性表L已存在,1≤i≤ListLength(L)

//操作结果:

删除L的第i个数据元素,并用e返回其值,L的长度减1

ElemType*p,*q;

if(i<1||i>L.length)//i值不合法

returnERROR;

p=L.elem+i-1;//p为被删除元素的位置

e=*p;//被删除元素的值赋给e

q=L.elem+L.length-1;//表尾元素的位置

for(++p;p<=q;++p)//被删除元素之后的元素左移

*(p-1)=*p;

L.length--;//表长减1

returnOK;

}

voidMergeList(SqListLa,SqListLb,SqList&Lc)

{

inti,j=1,k=0;

intLa_len,Lb_len;

ElemTypeai,bj;

InitList(Lc);

i=La_len=ListLength(La);

Lb_len=ListLength(Lb);

while(i>=1&&j<=Lb_len)

{

GetElem(La,i,ai);

GetElem(Lb,j,bj);

if(ai<=bj)

{

ListInsert(Lc,++k,ai);

--i;

}

else

{

ListInsert(Lc,++k,bj);

++j;

}

}

while(i>=1)

{

GetElem(La,i--,ai);

ListInsert(Lc,++k,ai);

}

while(j<=Lb_len)//表Lb非空且表La空

{

GetElem(Lb,j++,bj);

ListInsert(Lc,++k,bj);

}

}

Statusequal(ElemTypec1,ElemType

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 农学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1