数据结构算法c++实现0130100438.docx

上传人:b****1 文档编号:15112036 上传时间:2023-06-30 格式:DOCX 页数:89 大小:62.54KB
下载 相关 举报
数据结构算法c++实现0130100438.docx_第1页
第1页 / 共89页
数据结构算法c++实现0130100438.docx_第2页
第2页 / 共89页
数据结构算法c++实现0130100438.docx_第3页
第3页 / 共89页
数据结构算法c++实现0130100438.docx_第4页
第4页 / 共89页
数据结构算法c++实现0130100438.docx_第5页
第5页 / 共89页
数据结构算法c++实现0130100438.docx_第6页
第6页 / 共89页
数据结构算法c++实现0130100438.docx_第7页
第7页 / 共89页
数据结构算法c++实现0130100438.docx_第8页
第8页 / 共89页
数据结构算法c++实现0130100438.docx_第9页
第9页 / 共89页
数据结构算法c++实现0130100438.docx_第10页
第10页 / 共89页
数据结构算法c++实现0130100438.docx_第11页
第11页 / 共89页
数据结构算法c++实现0130100438.docx_第12页
第12页 / 共89页
数据结构算法c++实现0130100438.docx_第13页
第13页 / 共89页
数据结构算法c++实现0130100438.docx_第14页
第14页 / 共89页
数据结构算法c++实现0130100438.docx_第15页
第15页 / 共89页
数据结构算法c++实现0130100438.docx_第16页
第16页 / 共89页
数据结构算法c++实现0130100438.docx_第17页
第17页 / 共89页
数据结构算法c++实现0130100438.docx_第18页
第18页 / 共89页
数据结构算法c++实现0130100438.docx_第19页
第19页 / 共89页
数据结构算法c++实现0130100438.docx_第20页
第20页 / 共89页
亲,该文档总共89页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

数据结构算法c++实现0130100438.docx

《数据结构算法c++实现0130100438.docx》由会员分享,可在线阅读,更多相关《数据结构算法c++实现0130100438.docx(89页珍藏版)》请在冰点文库上搜索。

数据结构算法c++实现0130100438.docx

数据结构算法c++实现0130100438

数据结构算法实现

计算机科学与技术系

算法一学会简单开发与程序调式1

算法二线性表操作3

算法三单链表操作7

算法四栈基本操作13

算法五表达式求值18

算法六队列操作24

算法七稀疏矩阵运算27

算法八广义表操作30

算法九二叉树操作33

算法十二叉排序树的操作39

算法十一图的操作45

算法十二排序操作63

算法十三查找操作67

算法十四哈希表操作69

算法一学会简单开发与程序调式

1、目的

熟悉C或C++集成开发环境的基本命令及功能键,熟悉常用功能菜单命令理解C或C++程序结构理解函数声明、定义和调用方法理解标准库函数、自定义函数掌握参数的不同传送方式及作用

2、要求学习如何根据编译信息,定位语法错误将警告与错误一律看作错误学习C或C++程序书写风格写出上机调试后的体会

3、内容

(1)编程实现输出一组数的最大值(或最小值)参考程序如下:

#includeconstintn=10;

voidmain(){inti,x,a[n];

cout<<"inputin10num:

";for(i=0;i

cin>>a[i];x=a[0];i=1;

while(i

{if(a[i]>x)

x=a[i];

i++;

}cout<<"10nummaxis:

"<

}

(2)阅读下列程序,体会参数传递的变化,并上机调试。

#include

#include

voidfun1(inta,intb);

voidfun2(int&a,int&b);

voidfun3(int*a,int*b);

voidmain()

{

intx=5,y=10;

cout<<"anzhichuansong:

"<

"<

cout<<"main:

"<

cout<<"yyong:

"<

"<

cout<<"main:

"<

cout<<"zhizhen:

"<

cout<<"main:

"<

cout<<"main:

"<

}

voidfun1(inta,intb)

{

a=a+b;

b=2*a+3*b;cout<<"fun1:

"<

}

voidfun2(int&a,int&b)

a=a+b;

b=2*a+3*b;

cout<<"fun2:

"<

}

voidfun3(int*pa,int*pb)

{

*pa+=*pb;

*pb-=1;

cout<<"fun3:

"<

}

算法二顺性表操作

1、目的会定义线性表的顺序存储类型掌握线性表的基本运算和具体函数的定义

2、要求编写对线性表的建立、插入、删除、查找等算法,并判断插入、删除的位置是否合法。

认真编写源程序,并进行调试,写出输入、输出和溢出判断结果写出上机调试后的体会

3、内容编写线性表的顺序存储结构上的:

初始化线性表、清空线性表、求线性表的长度、判空、判满、查找、插入、删除、线性表的有序输出等算法。

参考程序如下:

#include

#include

typedefintelemtype;

structlist{

elemtype*list;

intlen;

intmaxsize;

};

voidinitlist(list&l,intms)

{l.list=newelemtype[ms];

if(!

l.list)

{cerr<<"memoryallocationfailure!

"<

(1);}

l.len=0;

l.maxsize=ms;

}

voidclearlist(list&l)

{l.len=0;}

intlength(list&l)

{returnl.len;}

intlistempty(list&l)

{returnl.len==0;}

intlistfull(list&l)

{returnl.len==l.maxsize;}

voidlooklist(list&l)

{for(inti=0;i

cout<

cout<

}

intfindlist(list&l,elemtypex)

{for(inti=0;i

if(l.list[i]==x)

{x=l.list[i];

return1;}

return0;

}

intinsertlist(list&l,elemtypex,intk)

{if(listfull(l))return0;

if(k>0)

{for(inti=l.len-1;i>=0;i--)l.list[i+1]=l.list[i];

l.list[0]=x;

}

elseif(k<0)l.list[l.len]=x;

else

{for(inti=0;i=i;j--)l.list[j+1]=l.list[j];

l.list[i]=x;

}

l.len++;

return1;

}

intdeletelist(list&l,elemtype&x,intk)

{if(listempty(l))return0;

if(k>0)

{x=l.list[0];

for(inti=1;i

}

elseif(k<0)x=l.list[l.len-1];

else

{for(inti=0;i=l.len)return0;elsex=l.list[i];for(intj=i+1;j

}

l.len--;

return1;

voidoutputlist(list&l,intm)

{int*b=newint[l.len];

inti,k;

for(i=0;i

for(i=1;i

{k=i-1;

for(intj=i;j

{if(m==1&&l.list[b[j]]

=1&&l.list[b[j]]>l.list[b[k]])k=j;

}

if(k!

=i-1)

{intx=b[i-1];

b[i-1]=b[k];

b[k]=x;

}

}

for(i=0;i

cout<

}

voidmain()

{constintml=20;

lista;initlist(a,ml);inti;elemtypex;cout<<"input5intnum:

";for(i=0;i<5;i++)

{cin>>x;

insertlist(a,x,-1);

}cout<<"input2intnum:

";cin>>x;insertlist(a,x,1);cin>>x;insertlist(a,x,1);

looklist(a);

cout<<"upordersort:

";outputlist(a,1);

cout<<"downordersort:

";outputlist(a,0);

listb;

initlist(b,ml);

for(i=0;i

insertlist(b,a.list[i],0);

looklist(b);

if(deletelist(a,x,1))cout<<"deletefront!

"<

else

cout<<"deletefale!

"<

if(deletelist(a,x,-1))cout<<"deleterear!

"<

else

cout<<"deletefale!

"<

cout<<"inputadelnum:

";cin>>x;

if(deletelist(a,x,0))cout<<"deletesuccess!

"<

else

cout<<"deletefale!

"<

}

算法三单链表操作

1、目的会定义单链表的结点类型掌握对单链表的一些基本操作和具体函数的定义

充分利用C或C++语言的特点,提高程序的可移植性

2、要求

编写对单链表的初始化、插入、删除、查找等算法。

认真编写源程序,并进行调试,写出输入、输出结果写出上机调试运行分析及功能。

3、内容编写单链表:

初始化单链表、清空单链表、求单链表的长度、判空、查找、插入、删除、线性表的有序输出等算法。

参考程序如下:

#include

#include

typedefintelemtype;

structlnode{

elemtypedata;

lnode*next;

};

voidinitlist(lnode*&hl)

{lnode*p=newlnode;

p->next=NULL;

hl=p;

hl->next=NULL;

}

voidclearlist(lnode*&hl)

{

lnode*p,*q;

p=hl->next;

while(p!

=NULL)

{q=p->next;

deletep;

p=q;

}

hl->next=NULL;

}

intlissize(lnode*hl)

{inti=0;

lnode*p=hl->next;while(p!

=NULL){i++;

p=p->next;

}returni;

}

intlistempty(lnode*hl)

{returnhl->next==NULL;

}elemtypegetelem(lnode*hl,inti){

if(i<1)

{cerr<<"posisoutrange!

"<

(1);

}

lnode*p=hl->next;

intj=0;while(p!

=NULL)

{j++;if(j==i)break;

p=p->next;}

if(p==NULL)

{cerr<<"posisoutrange!

"<

(1);

}return(p->data);

}

voidinsertrear(lnode*hl,elemtypex){lnode*q=newlnode;

q->data=x;q->next=NULL;

lnode*p=hl;

while(p->next!

=NULL)p=p->next;

p->next=q;

}

voidlooklist(lnode*hl)

{

lnode*p=hl->next;

while(p!

=NULL)

{cout<data<<"";p=p->next;

}

cout<

}

intfindlist(lnode*hl,elemtype&x)

{lnode*p=hl->next;

while(p!

=NULL)if(p->data==x)

{x=p->data;

return1;

}

else

p=p->next;return0;

}

voidinsertlist(lnode*hl,elemtypex,inti){if(i<1)

{cerr<<"posisoutrange!

"<

(1);

}lnode*p,*q;intj=0;p=hl;

while(p->next!

=NULL)

{j++;

if(j==i)

break;

p=p->next;

}

if(j==i)

{q=newlnode;q->data=x;q->next=p->next;p->next=q;

}

else

{cerr<<"posisoutrange!

"<

(1);

}

}

voiddeletelist(lnode*hl,inti)

{lnode*p=hl;

intj=0;

while(p->next!

=NULL&&j

{p=p->next;

j++;

}if(j>i-1||p->next==NULL)

{cerr<<"error!

"<

(1);

}

lnode*q=p->next;

p->next=q->next;

deleteq;

}

voidsortlist(lnode*hl,intk)

{lnode*head=newlnode;head->next=NULL;head->data=hl->next->data;for(lnode*p=hl->next->next;p;p=p->next)

{lnode*q=newlnode;

q->data=p->data;

lnode*cp=head;

lnode*ap=NULL;

while(cp)

{if(k==1)

{if(q->datadata)

break;

else

{ap=cp;cp=cp->next;}

}

else

{if(q->data>cp->data)break;

else

{ap=cp;cp=cp->next;}

}

}

if(ap==NULL)

{q->next=head;head=q;}else

{q->next=cp;ap->next=q;}}

lnode*r=newlnode;r->next=head;

head=r;

looklist(head);

clearlist(head);

}

voidmain()

{

lnode*hl;

initlist(hl);

inti;

elemtypex;

cout<<"inputin5num:

";

for(i=0;i<5;i++)

{cin>>x;

insertrear(hl,x);

}cout<<"input2num:

";cin>>x;insertlist(hl,x,1);cin>>x;insertlist(hl,x,1);looklist(hl);sortlist(hl,1);sortlist(hl,0);

}

栈基本操作

算法四

1、目的:

掌握栈的存储表示方式和栈基本操作的实现方法

2、要求:

栈的基本操作实现方法,栈的应用

3、内容:

栈的实现栈的顺序存储。

#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\nSqStackDemoisrunning...\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("

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

当前位置:首页 > 求职职场 > 笔试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2