数据结构测试二.docx

上传人:b****6 文档编号:7144830 上传时间:2023-01-21 格式:DOCX 页数:17 大小:265.95KB
下载 相关 举报
数据结构测试二.docx_第1页
第1页 / 共17页
数据结构测试二.docx_第2页
第2页 / 共17页
数据结构测试二.docx_第3页
第3页 / 共17页
数据结构测试二.docx_第4页
第4页 / 共17页
数据结构测试二.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

数据结构测试二.docx

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

数据结构测试二.docx

数据结构测试二

数据结构测试二

4-1 邻接矩阵存储图的深度优先遍历   (20分)

试实现邻接矩阵存储图的深度优先遍历。

函数接口定义:

voidDFS(MGraphGraph,VertexV,void(*Visit)(Vertex));

其中MGraph是邻接矩阵存储的图,定义如下:

typedefstructGNode*PtrToGNode;

structGNode{

intNv;/*顶点数*/

intNe;/*边数*/

WeightTypeG[MaxVertexNum][MaxVertexNum];/*邻接矩阵*/

};

typedefPtrToGNodeMGraph;/*以邻接矩阵存储的图类型*/

函数DFS应从第V个顶点出发递归地深度优先遍历图Graph,遍历时用裁判定义的函数Visit访问每个顶点。

当访问邻接点时,要求按序号递增的顺序。

题目保证V是图中的合法顶点。

裁判测试程序样例:

#include

typedefenum{false,true}bool;

#defineMaxVertexNum10/*最大顶点数设为10*/

#defineINFINITY65535/*∞设为双字节无符号整数的最大值65535*/

typedefintVertex;/*用顶点下标表示顶点,为整型*/

typedefintWeightType;/*边的权值设为整型*/

typedefstructGNode*PtrToGNode;

structGNode{

intNv;/*顶点数*/

intNe;/*边数*/

WeightTypeG[MaxVertexNum][MaxVertexNum];/*邻接矩阵*/

};

typedefPtrToGNodeMGraph;/*以邻接矩阵存储的图类型*/

boolVisited[MaxVertexNum];/*顶点的访问标记*/

MGraphCreateGraph();/*创建图并且将Visited初始化为false;裁判实现,细节不表*/

voidVisit(VertexV)

{

printf("%d",V);

}

voidDFS(MGraphGraph,VertexV,void(*Visit)(Vertex));

 

intmain()

{

MGraphG;

VertexV;

G=CreateGraph();

scanf("%d",&V);

printf("DFSfrom%d:

",V);

DFS(G,V,Visit);

return0;

}

/*你的代码将被嵌在这里*/

输入样例:

给定图如下

5

输出样例:

DFSfrom5:

5130246

(无答案)

 

4-5 顺序表操作集   (10分)

本题要求实现顺序表的操作集。

函数接口定义:

ListMakeEmpty();

PositionFind(ListL,ElementTypeX);

boolInsert(ListL,ElementTypeX,PositionP);

boolDelete(ListL,PositionP);

其中List结构定义如下:

typedefintPosition;

typedefstructLNode*List;

structLNode{

ElementTypeData[MAXSIZE];

PositionLast;/*保存线性表中最后一个元素的位置*/

};

各个操作函数的定义为:

ListMakeEmpty():

创建并返回一个空的线性表;

PositionFind(ListL,ElementTypeX):

返回线性表中X的位置。

若找不到则返回ERROR;

boolInsert(ListL,ElementTypeX,PositionP):

将X插入在位置P并返回true。

若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGALPOSITION”并返回false;

boolDelete(ListL,PositionP):

将位置P的元素删除并返回true。

若参数P指向非法位置,则打印“POSITIONPEMPTY”(其中P是参数值)并返回false。

裁判测试程序样例:

#include

#include

#defineMAXSIZE5

#defineERROR-1

typedefenum{false,true}bool;

typedefintElementType;

typedefintPosition;

typedefstructLNode*List;

structLNode{

ElementTypeData[MAXSIZE];

PositionLast;/*保存线性表中最后一个元素的位置*/

};

ListMakeEmpty();

PositionFind(ListL,ElementTypeX);

boolInsert(ListL,ElementTypeX,PositionP);

boolDelete(ListL,PositionP);

intmain()

{

ListL;

ElementTypeX;

PositionP;

intN;

L=MakeEmpty();

scanf("%d",&N);

while(N--){

scanf("%d",&X);

if(Insert(L,X,0)==false)

printf("InsertionError:

%disnotin.\n",X);

}

scanf("%d",&N);

while(N--){

scanf("%d",&X);

P=Find(L,X);

if(P==ERROR)

printf("FindingError:

%disnotin.\n",X);

else

printf("%disatposition%d.\n",X,P);

}

scanf("%d",&N);

while(N--){

scanf("%d",&P);

if(Delete(L,P)==false)

printf("DeletionError.\n");

if(Insert(L,0,P)==false)

printf("InsertionError:

0isnotin.\n");

}

return0;

}

/*你的代码将被嵌在这里*/

输入样例:

6

123456

3

651

2

-16

输出样例:

FULLInsertionError:

6isnotin.

FindingError:

6isnotin.

5isatposition0.

1isatposition4.

POSITION-1EMPTYDeletionError.

FULLInsertionError:

0isnotin.

POSITION6EMPTYDeletionError.

FULLInsertionError:

0isnotin.

答案:

(10分)

ListMakeEmpty(){//置空

ListL;

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

L->Last=-1;

returnL;

}

PositionFind(ListL,ElementTypeX){

inti;

for(i=0;i<=L->Last;i++){//查找

if(L->Data[i]==X)

returni;

}

returnERROR;

}

boolInsert(ListL,ElementTypeX,PositionP){

if(L->Last+1==MAXSIZE){//若最后一个元素下标+1为max则顺序表已满

printf("FULL");

returnfalse;

}

if(0>P||P>(L->Last+1)){//插入位置必须大于等于0,或者小于等于Last+1

printf("ILLEGALPOSITION");

returnfalse;

}

for(inti=L->Last;i>=P;i--){//从后面开始移动

L->Data[i+1]=L->Data[i];

}

L->Data[P]=X;

L->Last++;

returntrue;

}

boolDelete(ListL,PositionP){

if(P<0||P>L->Last){//删除位置必须是[0,Last]之间

printf("POSITION%dEMPTY",P);

returnfalse;

}

else{

inti;

for(i=P;iLast;i++){

L->Data[i]=L->Data[i+1];

}

L->Last--;

returntrue;

}

}

 

4-5 在一个数组中实现两个堆栈   (20分)

本题要求在一个数组中实现两个堆栈。

函数接口定义:

StackCreateStack(intMaxSize);

boolPush(StackS,ElementTypeX,intTag);

ElementTypePop(StackS,intTag);

其中Tag是堆栈编号,取1或2;MaxSize堆栈数组的规模;Stack结构定义如下:

typedefintPosition;

structSNode{

ElementType*Data;

PositionTop1,Top2;

intMaxSize;

};

typedefstructSNode*Stack;

注意:

如果堆栈已满,Push函数必须输出“StackFull”并且返回false;如果某堆栈是空的,则Pop函数必须输出“StackTagEmpty”(其中Tag是该堆栈的编号),并且返回ERROR。

裁判测试程序样例:

#include

#include

#defineERROR1e8

typedefintElementType;

typedefenum{push,pop,end}Operation;

typedefenum{false,true}bool;

typedefintPosition;

structSNode{

ElementType*Data;

PositionTop1,Top2;

intMaxSize;

};

typedefstructSNode*Stack;

StackCreateStack(intMaxSize);

boolPush(StackS,ElementTypeX,intTag);

ElementTypePop(StackS,intTag);

OperationGetOp();/*detailsomitted*/

voidPrintStack(StackS,intTag);/*detailsomitted*/

intmain()

{

intN,Tag,X;

StackS;

intdone=0;

scanf("%d",&N);

S=CreateStack(N);

while(!

done){

switch(GetOp()){

casepush:

scanf("%d%d",&Tag,&X);

if(!

Push(S,X,Tag))printf("Stack%disFull!

\n",Tag);

break;

casepop:

scanf("%d",&Tag);

X=Pop(S,Tag);

if(X==ERROR)printf("Stack%disEmpty!

\n",Tag);

break;

caseend:

PrintStack(S,1);

PrintStack(S,2);

done=1;

break;

}

}

return0;

}

/*你的代码将被嵌在这里*/

输入样例:

5

Push11

Pop2

Push211

Push12

Push212

Pop1

Push213

Push214

Push13

Pop2

End

输出样例:

Stack2Empty

Stack2isEmpty!

StackFull

Stack1isFull!

PopfromStack1:

1

PopfromStack2:

131211

答案:

(20分)

StackCreateStack(intMaxSize){

StackS=(Stack)malloc(sizeof(structSNode));

S->MaxSize=MaxSize;

S->Data=(ElementType*)malloc(sizeof(ElementType)*MaxSize);

S->Top2=MaxSize;

S->Top1=-1;

returnS;

}

boolPush(StackS,ElementTypeX,intTag){

if(S==NULL)

returnfalse;

if((S->Top1+1)==S->Top2){

printf("StackFull\n");

returnfalse;

}

if(1==Tag){

S->Data[++(S->Top1)]=X;

returntrue;

}

elseif(2==Tag){

S->Data[(--S->Top2)]=X;

returntrue;

}

returnfalse;

}

ElementTypePop(StackS,intTag){

if(S==NULL)

returnERROR;

if(1==Tag){

if(-1==S->Top1){

printf("Stack%dEmpty\n",Tag);

returnERROR;

}

returnS->Data[(S->Top1)--];

}elseif(2==Tag){

if(S->MaxSize==S->Top2){

printf("Stack%dEmpty\n",Tag);

returnERROR;

}

returnS->Data[(S->Top2)++];

}

returnERROR;

}

 

4-7 学生成绩链表处理   (20分)

本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。

函数接口定义:

structstud_node*createlist();

structstud_node*deletelist(structstud_node*head,intmin_score);

函数createlist利用scanf从输入中获取学生的信息,将其组织成单向链表,并返回链表头指针。

链表节点结构定义如下:

structstud_node{

intnum;/*学号*/

charname[20];/*姓名*/

intscore;/*成绩*/

structstud_node*next;/*指向下个结点的指针*/

};

输入为若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束。

函数deletelist从以head为头指针的链表中删除成绩低于min_score的学生,并返回结果链表的头指针。

裁判测试程序样例:

#include

#include

structstud_node{

intnum;

charname[20];

intscore;

structstud_node*next;

};

structstud_node*createlist();

structstud_node*deletelist(structstud_node*head,intmin_score);

intmain()

{

intmin_score;

structstud_node*p,*head=NULL;

head=createlist();

scanf("%d",&min_score);

head=deletelist(head,min_score);

for(p=head;p!

=NULL;p=p->next)

printf("%d%s%d\n",p->num,p->name,p->score);

return0;

}

/*你的代码将被嵌在这里*/

输入样例:

1zhang78

2wang80

3li75

4zhao85

0

80

输出样例:

2wang80

4zhao85

答案:

(16分)

structstud_node*createlist()

{

intn=0;

structstud_node*head,*p1,*p2;

head=NULL;

p1=p2=(structstud_node*)malloc(sizeof(structstud_node));

scanf("%d",&p1->num);

if(p1->num==0)

returnhead;

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

while

(1)

{

n++;

if(n==1)

head=p2=p1;

else

p2->next=p1;

p2=p1;

p1=(structstud_node*)malloc(sizeof(structstud_node));

scanf("%d",&p1->num);

if(p1->num==0)

break;

else

{

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

}

}

p2->next=NULL;

returnhead;

}

structstud_node*deletelist(structstud_node*head,intmin_score)

{

structstud_node*p1,*p2;

p1=p2=head;

while(p1)

{

if(p1->score

{

if(p1==head)

{

p1=p1->next;

head=p2=p1;

}

elseif(p1->next==NULL)

{

p2->next=NULL;

}

else

{

p1=p1->next;

p2->next=p1;

}

}

else

{

p2=p1;

p1=p1->next;

}

}

returnhead;

}

 

4-5 逆序数据建立链表   (20分)

本题要求实现一个函数,按输入数据的逆序建立一个链表。

函数接口定义:

structListNode*createlist();

函数createlist利用scanf从输入中获取一系列正整数,当读到-1−1时表示输入结束。

按输入数据的逆序建立一个链表,并返回链表头指针。

链表节点结构定义如下:

structListNode{

intdata;

structListNode*next;

};

裁判测试程序样例:

#include

#include

structListNode{

intdata;

structListNode*next;

};

structListNode*createlist();

intmain()

{

structListNode*p,*head=NULL;

head=createlist();

for(p=head;p!

=NULL;p=p->next)

printf("%d",p->data);

printf("\n");

return0;

}

/*你的代码将被嵌在这里*/

输入样例:

1234567-1

输出样例:

7654321

答案:

(19分)

structListNode*createlist()

{

structListNode*p1,*head,*p2;

head=NULL;

intn=0;

p1=p2=(structListNode*)malloc(sizeof(structListNode));

scanf("%d",&p1->data);

while(p1->data!

=-1)

{

n++;

if(n==1)

p1->next=NULL;

else

p1->next=p2;

p2=p1;

p1=(structListNode*)malloc(sizeof(structListNode));

scanf("%d",&p1->data);

}

head=p2;

returnhead;

}

 

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

当前位置:首页 > 总结汇报 > 学习总结

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

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