数据结构实验报告.docx
- 文档编号:26681041
- 上传时间:2023-06-21
- 格式:DOCX
- 页数:61
- 大小:217.22KB
数据结构实验报告.docx
《数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告.docx(61页珍藏版)》请在冰豆网上搜索。
数据结构实验报告
数据结构实验报告
实验一:
线性表的应用
实验名称:
线性表的应用
实验目的:
掌握线性表的逻辑结构定义、线性表的两种存储结构(顺序和链式);
掌握顺序表和链表的定义及基本操作,运用其解决一些实际问题。
实验题目:
1、请编写26个字母按特定的字母值插入或者删除的完整程序,可自行选用顺序存储或者链表存储。
程序代码:
#include
#include
typedefstructliuyu{chardata;structliuyu*link;}test;
test*p,*q,*r,*head;
intL;
intm=sizeof(test);
voidbuild();
voiddisplay();
intinsert_char(char,char);
intdelet_char(char);
voidbuild()
{
inti;
head=(test*)malloc(m);
p=head;
for(i=1;i { p->data=i+'a'-1; p->link=(test*)malloc(m); p=p->link; } p->data=i+'a'-1; p->link=NULL; } voiddisplay() { p=head; while(p->link! =NULL) { printf("%c",p->data); p=p->link;} printf("%c\n",p->data); } intinsert_char(charX,charY) { p=head; r=(test*)malloc(m); r->data=X; if(head->data==Y) { head=r; r->link=p; } else { while((p->data! =Y)&&(p->link! =NULL)){q=p;p=p->link;} if(p->data==Y){q->link=r;r->link=p;} else{p->link=r,r->link=NULL;} } L++; return0; } intdelet_char(charX) { p=head; if(head->data==X){head=head->link;free(p);} else{while((p->data! =X)&&(p->link! =NULL)) {q=p;p=p->link;} if(p->data==X) {q->link=p->link;free(p);} elsereturn(-1); L--; return(0); } voidmain(void) { L=26; build(); display(); printf("insertreturnvalue=%d\n",insert_char('L','W')); display(); printf("deletereturnvalue=%d\n",delet_char('z')); display(); } 运行结果: 2、创建一个链表。 /*creatalist*/ #include"stdlib.h" #include"stdio.h" #include structlist { intdata; structlist*next; }; typedefstructlistnode; typedefnode*link; voidmain() { linkptr,head; intnum,i; head=(link)malloc(sizeof(node)); ptr=head; printf("pleaseinput5numbers==>\n"); for(i=0;i<=4;i++) { scanf("%d",&num); ptr->data=num; ptr->next=(link)malloc(sizeof(node)); if(i==4)ptr->next=NULL; elseptr=ptr->next; } ptr=head; while(ptr! =NULL) { printf("Thevalueis==>%d\n",ptr->data); ptr=ptr->next; } } 运行结果: 3、反向输出一个链表。 /*reverseoutputalist*/ #include"stdlib.h" #include"stdio.h" #include structlist { intdata; structlist*next; }; typedefstructlistnode; typedefnode*link; voidmain() { linkptr,head,tail; intnum,i; tail=(link)malloc(sizeof(node)); tail->next=NULL; ptr=tail; printf("npleaseinput5data==>\n"); for(i=0;i<=4;i++) { scanf("%d",&num); ptr->data=num; head=(link)malloc(sizeof(node)); head->next=ptr; ptr=head; } ptr=ptr->next; while(ptr! =NULL) { printf("Thevalueis==>%d\n",ptr->data); ptr=ptr->next; } } 运行结果: 实验二 1、连接两个链表。 #include"stdlib.h" #include"stdio.h" structlist { intdata; structlist*next; }; typedefstructlistnode; typedefnode*link; linkdelete_node(linkpointer,linktmp) { if(tmp==NULL) returnpointer->next; else {if(tmp->next->next==NULL) tmp->next=NULL; else tmp->next=tmp->next->next; returnpointer; } } voidselection_sort(linkpointer,intnum) { linktmp,btmp; inti,min; for(i=0;i { tmp=pointer; min=tmp->data; btmp=NULL; while(tmp->next) {if(min>tmp->next->data) {min=tmp->next->data; btmp=tmp; } tmp=tmp->next; } printf("\40: %d\n",min); pointer=delete_node(pointer,btmp); } } linkcreate_list(intarray[],intnum) { linktmp1,tmp2,pointer; inti; pointer=(link)malloc(sizeof(node)); pointer->data=array[0]; tmp1=pointer; for(i=1;i {tmp2=(link)malloc(sizeof(node)); tmp2->next=NULL; tmp2->data=array[i]; tmp1->next=tmp2; tmp1=tmp1->next; } returnpointer; } linkconcatenate(linkpointer1,linkpointer2) { linktmp; tmp=pointer1; while(tmp->next) tmp=tmp->next; tmp->next=pointer2; returnpointer1; } voidmain(void) { intarr1[]={3,12,8,9,11}; linkptr; ptr=create_list(arr1,5); selection_sort(ptr,5); } 运行结果: 2、改进版: #include"stdlib.h" #include"stdio.h" structlist { intdata; structlist*next; }; typedefstructlistnode; typedefnode*link; linkdelete_node(linkpointer,linktmp) { if(tmp==NULL) returnpointer->next; else {if(tmp->next->next==NULL) tmp->next=NULL; else tmp->next=tmp->next->next; returnpointer; } } voidselection_sort(linkpointer,intnum) { linktmp,btmp; inti,min; for(i=0;i { tmp=pointer; min=tmp->data; btmp=NULL; while(tmp->next) {if(min>tmp->next->data) {min=tmp->next->data; btmp=tmp; } tmp=tmp->next; } printf("\40: %d\n",min); pointer=delete_node(pointer,btmp); } } linkcreate_list(intarray[],intnum) { linktmp1,tmp2,pointer; inti; pointer=(link)malloc(sizeof(node)); pointer->data=array[0]; tmp1=pointer; for(i=1;i {tmp2=(link)malloc(sizeof(node)); tmp2->next=NULL; tmp2->data=array[i]; tmp1->next=tmp2; tmp1=tmp1->next; } returnpointer; } linkconcatenate(linkpointer1,linkpointer2) {linktmp; tmp=pointer1; while(tmp->next) tmp=tmp->next; tmp->next=pointer2; returnpointer1; } voidmain(void) { intarr1[]={3,12,13,14,15}; linkpointer1; pointer1=create_list(arr1,5); selection_sort(pointer1,5); intarr2[]={1,2,4,5,6}; linkpointer2; pointer2=create_list(arr2,5); selection_sort(pointer2,5); concatenate(pointer1,pointer2); linkp; p=pointer1; while(p->next! =NULL) {printf("%d\n",p->data); p=p->next;} printf("%d\n",p->data); } 运行结果: 实验三: 栈和队列的应用 实验目的: 掌握栈和队列的结构定义和特性;掌握栈和队列的基本操作以及栈和队列在程序设计中的应用。 实验题目: 1、栈实现数制转换 #include"stdlib.h" #include"stdio.h" #include"stack.h" typedefintSElemType; Statusvisit(SElemType*e) { printf("%d",*e); return0; } voidconversion() { pSqStackS; SElemTypee; intn; InitStack(&S); printf("InputanumbertoconverttoOCT: \n"); scanf("%d",&n); if(n<0) { printf("\nThenumbermustbeover0."); return; } if(! n)Push(S,0); while(n){ Push(S,n%8); n=n/8; } printf("theresultis: "); while(! StackEmpty(*S)){ Pop(S,&e); printf("%d",e); } } main() { printf("\n\n\n\n"); conversion(); getchar(); printf("\n\n"); } stack.h代码如下: #defineOVERFLOW-1 #defineOK2 #defineERROR-1 #defineTRUE1 #defineFALSE0 #defineSTACK_INIT_SIZE100 #defineSTACKINCREMENT10 typedefintStatus; structSTACK { SElemType*base; SElemType*top; intstacksize; }; typedefstructSTACKSqStack; typedefstructSTACK*pSqStack; StatusInitStack(SqStack**S); StatusDestroyStack(SqStack*S); StatusClearStack(SqStack*S); StatusStackEmpty(SqStackS); StatusStackLength(SqStackS); SElemTypeGetTop(SqStackS); StatusPush(SqStack*S,SElemType*e); 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); return0; } StatusClearStack(SqStack*S) { S->top=S->base; return0; } 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++; } returni; } SElemTypeGetTop(SqStackS) { if(S.top==S.base)returnERROR; return*(S.top-1); } 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; } StatusStackTraverse(SqStackS,Status(*visit)(SElemType*e)) { while(S.top>S.base) visit(--S.top); return0; } 运行结果: 2、栈实现单行编辑 程序代码: #include #include #include #include #include #defineEOFILE'`' typedefcharSElemType; #include"stack.h" Statusvisit(SElemType*e) { printf("%c",*e); returnOK; } charOP[10]={'+','-','*','/','(',')','#'}; intprecede[7][7]={ 1,1,2,2,2,1,1, 1,1,2,2,2,1,1, 1,1,1,1,2,1,1, 1,1,1,1,2,1,1, 2,2,2,2,2,3,0, 1,1,1,1,0,1,1, 2,2,2,2,2,0,3}; intIn(charc,char*op) { inti=0; while(i<7) if(c==op[i++]) return1; return0; } charPrecede(charop,charc) { intpos_op; intpos_c; inti; for(i=0;i<7;i++) { if(op==OP[i])pos_op=i; if(c==OP[i])pos_c=i; } switch(precede[pos_op][pos_c]) { case1: return'>'; case2: return'<'; case3: return'='; } } charOperate(inta,chartheta,intb) { switch(theta) { case'+': returna+b-'0'; case'-': returna-b+'0'; case'*': return(a-'0')*(b-'0')+'0'; case'/': return(a-'0')/(b-'0')+'0'; } } charEvaluateExpression() { SqStack*OPND,*OPTR; charc,x,theta; chara,b; InitStack(&OPTR);Push(OPTR,'#'); InitStack(&OPND); c=getchar(); while(c! ='#'||GetTop(*OPTR)! ='#') { if(! In(c,OP)) {Push(OPND,c); c=getchar();} else switch(Precede(GetTop(*OPTR),c)) { case'<': Push(OPTR,c); c=getchar(); break; case'=': Pop(OPTR,&x); c=getchar(); break; case'>': Pop(OPTR,&theta); Pop(OPND,&b); Pop(OPND,&a); Push(OPND,Operate(a,theta,b)); break; } } c=GetTop(*OPND); DestroyStack(OPTR); DestroyStack(OPND); returnc; } voidmain() { chari; printf("\n\n\n\nOnlywithin0..9evaluation,inputaexpressionendwithsymbol#: \n"); i=EvaluateExpression(); printf("\nThisexpression'sresultis: "); printf("%d\n\n\n\n",i-'0'); printf("\n\nWelcometovisit! "); } stack.h代码如下: #defineOVERFLOW-1 #defineOK2 #defineERROR-1 #defineTRUE1 #defineFALSE0 #defineSTACK_INIT_SIZE100 #defineSTACKINCREMENT10 typedefintStatus; structSTACK { SElemType*base; SElemType*top; intstacksize; }; typedefstructSTACKSqStack; typedefstructSTACK*pSqStack; StatusInitStack(SqStack**S); StatusDestroyStack(SqStack*S); StatusClearStack(SqStack*S); Stat
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告