数据结构实验指导书.docx

上传人:b****4 文档编号:4460571 上传时间:2022-12-01 格式:DOCX 页数:27 大小:24.52KB
下载 相关 举报
数据结构实验指导书.docx_第1页
第1页 / 共27页
数据结构实验指导书.docx_第2页
第2页 / 共27页
数据结构实验指导书.docx_第3页
第3页 / 共27页
数据结构实验指导书.docx_第4页
第4页 / 共27页
数据结构实验指导书.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

数据结构实验指导书.docx

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

数据结构实验指导书.docx

数据结构实验指导书

《数据结构》实验指导书

注意事项:

在磁盘上创建一个目录,专门用于存储数据结构实验的程序。

实验一顺序表操作

一、实验目的

1、掌握使用TurboC2.0上机调试线性表的基本方法;

2、掌握线性表的基本操作:

插入、删除、查找以及线性表合并等运算在顺序存储结构和链接存储结构上的运算。

二、实验要求

1、认真阅读和掌握本实验的程序。

2、上机运行本程序。

3、打印出程序的运行结果,并结合程序进行分析。

4、按照你对线性表的操作需要,重新改写主程序并运行,打印出文件清单和运行结果

三、实验内容

  程序:

线性表基本操作的实现

  这个程序中演示了顺序表的创建、插入、删除和查找和合并。

程序如下:

#include

#include

#include

#defineERROR0

#defineOK1

#defineEQUAL1

structSTU{

charname[20];

charstuno[10];

intage;

intscore;

}stu[50];

typedefstructSTUElemType;

structLIST

{

ElemTypeelem[50];

intlength;

};

typedefstructLISTList;

intinit(List**L)

{

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

(*L)->length=0;

}

intListLength(List*L)

{

returnL->length;

}

voidGetElem(ListL,inti,ElemType*e)

{*e=L.elem[i];

}

intEqualList(ElemType*e1,ElemType*e2)

{if(strcmp(e1->name,e2->name))

return0;

if(strcmp(e1->stuno,e2->stuno))

return0;

if(e1->age!

=e2->age)

return0;

if(e1->score!

=e2->score)

return0;

return1;

}

intLocateElem(List*La,ElemTypee,inttype)

{inti;

switch(type)

{

caseEQUAL:

for(i=0;ilength;i++)

if(EqualList(&La->elem[i],&e))

return1;

break;

default:

break;

}

return0;

}

voidUnionList(List*La,List*Lb)

{intLa_len,Lb_len;

inti;

ElemTypee;

La_len=ListLength(La);Lb_len=ListLength(Lb);

for(i=0;i

{GetElem(*Lb,i,&e);

if(!

LocateElem(La,e,EQUAL))

ListInsert(La,++La_len,e);

}

}

intprintlist(ListL)

{inti;

printf("namestunoagescore\n");

for(i=0;i

printf("%-10s%s\t%d\t%d\n",L.elem[i].name,L.elem[i].stuno,

L.elem[i].age,L.elem[i].score);

printf("\n");

}

intListInsert(List*L,inti,structSTUe)

{structSTU*p,*q;

if(i<1||i>L->length+1)returnERROR;

q=&(L->elem[i-1]);

for(p=&L->elem[L->length-1];p>=q;--p)

*(p+1)=*p;

*q=e;

++L->length;

returnOK;

}/*ListInsertBeforei*/

main()

{structSTUe;

List*La,*Lb;

clrscr();

printf("\n\n-------------------ListDemoisrunning...----------------\n\n");

printf("FirstisInsertListfunction.\n");

init(&La);

strcpy(e.name,"stu1");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

ListInsert(La,1,e);

strcpy(e.name,"stu2");

strcpy(e.stuno,"100002");

e.age=80;

e.score=1000;

ListInsert(La,2,e);

printlist(*La);

printf("ListAlengthnowis%d.\n\n",La->length);

getch();

strcpy(e.name,"stu3");

strcpy(e.stuno,"100003");

e.age=80;

e.score=1000;

ListInsert(La,3,e);

printlist(*La);

printf("ListAlengthnowis%d.\n\n",La->length);

getch();

init(&Lb);

strcpy(e.name,"zmofun");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

ListInsert(Lb,1,e);

strcpy(e.name,"bobjin");

strcpy(e.stuno,"100002");

e.age=80;

e.score=1000;

ListInsert(Lb,2,e);

strcpy(e.name,"stu1");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

ListInsert(Lb,3,e);

printlist(*Lb);

printf("ListBlengthnowis%d.\n\n",Lb->length);

getch();

printf("SecondisUnionListfunction.\n");

printf("NowunionListAandListB.....\n");

UnionList(La,Lb);

printlist(*La);

printf("ListAlengthnowis%d.\n\n",La->length);

getch();

clrscr();

}

实验二单链表操作

一、实验目的

掌握握单链表的基本操作:

插入、删除、查找等运算。

二、实验要求

1.认真阅读和掌握本实验的程序。

2.上机运行本程序。

3.打印出程序的运行结果,并结合程序进行分析。

4.按照你对单链表的操作需要,重新改写主程序并运行,打印出文件清单和运行结果

三、实验内容

程序:

线性单链表基本操作的实现

这个程序中演示了单链表的创建、插入、删除和查找。

参考程序如下:

#include

#include

#include

#defineERROR0

#defineOK1

#defineEQUAL1

#defineOVERFLOW-1

#defineLIST_INIT_SIZE100

#defineLISTINCREMENT10

structSTU{

charname[20];

charstuno[10];

intage;

intscore;

}stu[50];

typedefstructSTUElemType;

structLNODE

{

ElemTypedata;

structLNODE*next;

};

typedefstructLNODELNode;

typedefstructLNODE*LinkList;

intinit(LinkList*L)

{

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

if(!

L)exit(ERROR);

(*L)->next=NULL;

returnOK;

}/*init*/

intListLength(LinkListL)

{

intj=0;

while(L->next)

{

L=L->next;

j++;

}

returnj;

}

intGetElem(LinkListL,inti,ElemType*e)

{

LinkListp;intj;

p=L->next;j=1;

while(p&&j

{p=p->next;++j;}

if(!

p||j>1)returnERROR;

*e=p->data;

returnOK;

}

intEqualList(ElemType*e1,ElemType*e2)

{

if(strcmp(e1->name,e2->name)==0)

return1;

else

return0;

}

intLess_EqualList(ElemType*e1,ElemType*e2)

{

if(strcmp(e1->name,e2->name)<=0)

return1;

else

return0;

}

intLocateElem(LinkListLa,ElemTypee,inttype)

{

inti;

LinkListp;

p=La;

switch(type)

{

caseEQUAL:

while(p->next)

{

p=p->next;

if(EqualList(&p->data,&e))

return1;

}

return0;

break;

default:

break;

}

return0;

}

voidMergeList(LinkListLa,LinkListLb,LinkList*Lc)

{

LinkListpa,pb,pc;

pa=La->next;pb=Lb->next;

*Lc=pc=La;

while(pa&&pb)

{

if(Less_EqualList(&pa->data,&pb->data))

{

pc->next=pa;pc=pa;pa=pa->next;

}

else

{

pc->next=pb;pc=pb;pb=pb->next;

}

}

pc->next=pa?

pa:

pb;

free(Lb);

}

intprintlist(LinkListL)

{

inti;

LinkListp;

p=L;

printf("namestunoagescore\n");

while(p->next)

{

p=p->next;

printf("%-10s%s\t%d\t%d\n",p->data.name,p->data.stuno,

p->data.age,p->data.score);

}

printf("\n");

}

intListInsert(LinkListL,inti,ElemTypee)

{

LinkListp,s;

intj;

p=L;j=0;

while(p&&j

{p=p->next;++j;}

if(!

p||j>i-1)returnERROR;

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

s->data=e;

s->next=p->next;

p->next=s;

returnOK;

}/*ListInsertBeforei*/

 

main()

{

structSTUe;

LinkListLa,Lb,Lc;

clrscr();

printf("\n\n-------------------ListDemoisrunning...----------------\n\n");

printf("FirstisInsertListfunction.\n");

init(&La);

strcpy(e.name,"stu1");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

ListInsert(La,1,e);

strcpy(e.name,"stu3");

strcpy(e.stuno,"100002");

e.age=80;

e.score=1000;

ListInsert(La,2,e);

printlist(La);

getch();

strcpy(e.name,"stu5");

strcpy(e.stuno,"100003");

e.age=80;

e.score=1000;

ListInsert(La,3,e);

printlist(La);

getch();

init(&Lb);

strcpy(e.name,"stu2");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

ListInsert(Lb,1,e);

strcpy(e.name,"stu4");

strcpy(e.stuno,"100002");

e.age=80;

e.score=1000;

ListInsert(Lb,2,e);

strcpy(e.name,"stu6");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

ListInsert(Lb,3,e);

printlist(Lb);

getch();

MergeList(La,Lb,&Lc);

printlist(Lc);

getch();

}

实验三栈的基本操作和应用

一、实验目的

掌握栈的基本操作:

初始化栈、判栈为空、出栈、入栈等运算。

二、实验要求

1.认真阅读和掌握本实验的算法。

2.上机将本算法实现。

3.打印出程序的运行结果,并结合程序进行分析。

三、实验内容

程序1:

栈的顺序存储表示与实现以及栈的基本操作的算法

InitStack(&S)构造一个空栈S

DestroyStack(&S)栈S存在则栈S被销毁

ClearStack(&S)栈S存在则清为空栈

StackEmpty(S)栈S存在则返回TRUE,否则FALSE

StackLength(S)栈S存在则返回S的元素个数,即栈的长度

GetTop(S,&e)栈S存在且非空则返回S的栈顶元素

Push(&S,e)栈S存在则插入元素e为新的栈顶元素

Pop(&S,&e)栈S存在且非空则删除S的栈顶元素并用e返回其值

StackTraverse(S,visit())栈S存在且非空则从栈底到栈顶依次对S的每个数据元素调用函数visit()一旦visit()失败,则操作失败

参考程序如下:

#include

#include

#include

#defineERROR0

#defineTRUE1

#defineFALSE0

#defineOK1

#defineEQUAL1

#defineOVERFLOW-1

#defineSTACK_INIT_SIZE100

#defineSTACKINCREMENT10

typedefintStatus;

structSTU{

charname[20];

charstuno[10];

intage;

intscore;

};

typedefstructSTUSElemType;

structSTACK

{

SElemType*base;

SElemType*top;

intstacksize;

};

typedefstructSTACKSqStack;

typedefstructSTACK*pSqstack;

StatusInitStack(SqStack**S);

StatusDestroyStack(SqStack*S);

StatusClearStack(SqStack*S);

StatusStackEmpty(SqStackS);

intStackLength(SqStackS);

StatusGetTop(SqStackS,SElemType*e);

StatusPush(SqStack*S,SElemTypee);

StatusPop(SqStack*S,SElemType*e);

StatusStackTraverse(SqStackS,Status(*visit)());

StatusInitStack(SqStack**S)

{

(*S)=(SqStack*)malloc(sizeof(SqStack));

(*S)->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!

(*S)->base)exit(OVERFLOW);

(*S)->top=(*S)->base;

(*S)->stacksize=STACK_INIT_SIZE;

returnOK;

}

StatusDestroyStack(SqStack*S)

{

free(S->base);

free(S);

}

StatusClearStack(SqStack*S)

{

S->top=S->base;

}

StatusStackEmpty(SqStackS)

{

if(S.top==S.base)returnTRUE;

else

returnFALSE;

}

intStackLength(SqStackS)

{

inti;

SElemType*p;

i=0;

p=S.top;

while(p!

=S.base)

{p++;

i++;

}

}

StatusGetTop(SqStackS,SElemType*e)

{

if(S.top==S.base)returnERROR;

*e=*(S.top-1);

returnOK;

}

StatusPush(SqStack*S,SElemTypee)

{

/*

if(S->top-S->base>=S->stacksize)

{

S->base=(SElemType*)realloc(S->base,

(S->stacksize+STACKINCREMENT)*sizeof(SElemType));

if(!

S->base)exit(OVERFLOW);

S->top=S->base+S->stacksize;

S->stacksize+=STACKINCREMENT;

}

*/

*(S->top++)=e;

returnOK;

}

StatusPop(SqStack*S,SElemType*e)

{

if(S->top==S->base)returnERROR;

*e=*--S->top;

returnOK;

}

StatusStackPrintElem(SElemType*e)

{

printf("%s%s%d%d\n",e->name,e->stuno,e->age,e->score);

}

StatusStackTraverse(SqStackS,Status(*visit)())

{

while(S.top!

=S.base)

visit(--S.top);

}

main()

{

SElemTypee;

SqStack*Sa;

clrscr();

printf("\n\n-------------------SqStackDemoisrunning...----------------\n\n");

printf("FirstisPushfunction.\n");

InitStack(&Sa);

strcpy(e.name,"stu1");

strcpy(e.stuno,"100001");

e.age=80;

e.score=1000;

printf("NowStackisEmpty.\n");

StackTraverse(*Sa,StackPrintElem);

Push(Sa,e);

printf("NowStackhasoneelement.\n");

StackTraverse(*Sa,StackPrintElem);

strcpy(e.name,"stu3");

strcpy(e.stuno,"100002");

e.age=80;

e.score=1000;

Push(Sa,e);

printf("NowStackhasanotherelement.\n");

StackTraverse(*Sa,StackPrintElem);

printf("NowPopStack,thetopelemputintovariablee.\n");

Pop(Sa,&e);

printf("%s\n%s\n%d\n%d\n",e.name,e.stuno,e.age,e.score);

printf("Let'sseetheleftofStack's

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

当前位置:首页 > 高中教育 > 初中教育

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

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