七个程序.docx
- 文档编号:6733589
- 上传时间:2023-01-09
- 格式:DOCX
- 页数:35
- 大小:22.60KB
七个程序.docx
《七个程序.docx》由会员分享,可在线阅读,更多相关《七个程序.docx(35页珍藏版)》请在冰豆网上搜索。
七个程序
多项式相加HW1
p.793.6
Writeafunctiontoaddtwopolynomials.Donotdestroytheinput.Usealinkedlistimplementation.
IfthepolynomialshaveMandNterms,respectively,whatisthetimecomplexityofyourprogram?
#include
#include
#defineCOMPARE(numa,numb)(numa>numb?
-1:
(numa 1: 0)) typedefstructNode*PtrToNode; structNode { intCoefficient; intExponent; PtrToNodeNext; }; typedefPtrToNodePolynomial; PolynomialCreate(char*filename) { FILE*file; intcoefficient; intexponent; intval; PtrToNodefront,tail,temp; tail=(PtrToNode)malloc(sizeof(structNode)); if(tail==NULL) { printf("Thememoryisfull! \n"); exit (1); } front=tail; front->Exponent=-1; file=fopen(filename,"r"); if(file==NULL) { printf("Cant'topen%s! \n",filename); exit (1); } while(! feof(file)) { val=fscanf(file,"%d,%d",&coefficient,&exponent); printf("val=%d\n",val); if(exponent<-1) { printf("InFile%s: theexponentofpolynomialisnotlessthan0! \n",filename); exit (1); } temp=(PtrToNode)malloc(sizeof(structNode)); if(temp==NULL) { printf("Thememoryisfull! \n"); exit (1); } temp->Coefficient=coefficient; temp->Exponent=exponent; temp->Next=NULL; tail->Next=temp; tail=temp; } fclose(file); returnfront; } voidOutput(Polynomialpoly,char*filename) { FILE*file; file=fopen(filename,"w"); PtrToNodecurNode=poly->Next; if(file==NULL) { printf("Cant'topen%s! \n",filename); exit (1); } while(curNode! =NULL) { fprintf(file,"%d,%d\n",curNode->Coefficient,curNode->Exponent); curNode=curNode->Next; } fclose(file); } voidAttach(intcoefficient,intexponent,PtrToNode*ptr) { PtrToNodetemp; temp=(PtrToNode)malloc(sizeof(structNode)); if(temp==NULL) { printf("Thememoryisfull! \n"); exit (1); } temp->Coefficient=coefficient; temp->Exponent=exponent; temp->Next=NULL; (*ptr)->Next=temp; *ptr=temp; } PolynomialAdd(Polynomialpolya,Polynomialpolyb) { PtrToNodefront,tail; intsum; tail=(PtrToNode)malloc(sizeof(structNode)); if(tail==NULL) { printf("Thememoryisfull! \n"); exit (1); } front=tail; front->Exponent=-1; polya=polya->Next; polyb=polyb->Next; while(polya&&polyb) { switch(COMPARE(polya->Exponent,polyb->Exponent)) { case-1: Attach(polyb->Coefficient,polyb->Exponent,&tail); polyb=polyb->Next; break; case0: sum=polya->Coefficient+polyb->Coefficient; if(sum) Attach(sum,polya->Exponent,&tail); polya=polya->Next; polyb=polyb->Next; break; case1: Attach(polya->Coefficient,polya->Exponent,&tail); polya=polya->Next; break; } } for(;polya;polya=polya->Next) Attach(polya->Coefficient,polya->Exponent,&tail); for(;polyb;polyb=polyb->Next) Attach(polyb->Coefficient,polyb->Exponent,&tail); tail->Next=NULL; returnfront; } voidDelete(Polynomialpoly) { PtrToNodenode,tmp; node=poly; while(node! =NULL) { tmp=node->Next; free(node); node=tmp; } } intmain(intargc,char*argv[]) { Polynomialpolya; Polynomialpolyb; Polynomialpolyc; polya=Create(argv[1]); polyb=Create(argv[2]); polyc=Add(polya,polyb); Output(polyc,argv[3]); Delete(polya); Delete(polyb); Delete(polyc); return0; } 翻转链表 p.803.12 a.WriteanonrecursiveproceduretoreverseasinglylinkedlistinO(N)time. *b.WriteaproceduretoreverseasinglylinkedlistinO(N)timeusingconstantextraspace. #include"list.h"翻转链表 #include #include"fatal.h" /*Placeintheinterfacefile*/ structNode { ElementTypeElement; PositionNext; }; ListReverse(ListL) { PositionOld_head,New_head,Temp; New_head=NULL; Old_head=L->Next; while(Old_head) { Temp=Old_head->Next; Old_head->Next=New_head; New_head=Old_head; Old_head=Temp; }; L->Next=New_head; returnL; } voidPrintList(ListL,FILE*fp) { if(fp==NULL) Error("Can'topentheoutputfile! \n"); Positionp=L->Next; while(p! =NULL) { fprintf(fp,"%d",p->Element); p=p->Next; if(p! =NULL) fprintf(fp,","); } fprintf(fp,"\n"); } List MakeEmpty(ListL) { if(L! =NULL) DeleteList(L); L=malloc(sizeof(structNode)); if(L==NULL) FatalError("Outofmemory! "); L->Next=NULL; returnL; } /*START: fig3_8.txt*/ /*ReturntrueifLisempty*/ int IsEmpty(ListL) { returnL->Next==NULL; } /*END*/ /*START: fig3_9.txt*/ /*ReturntrueifPisthelastpositioninlistL*/ /*ParameterLisunusedinthisimplementation*/ intIsLast(PositionP,ListL) { returnP->Next==NULL; } /*END*/ /*START: fig3_10.txt*/ /*ReturnPositionofXinL;NULLifnotfound*/ Position Find(ElementTypeX,ListL) { PositionP; /*1*/ P=L->Next; /*2*/ while(P! =NULL&&P->Element! =X) /*3*/P=P->Next; /*4*/ returnP; } /*END*/ /*START: fig3_11.txt*/ /*Deletefromalist*/ /*CellpointedtobyP->Nextiswipedout*/ /*Assumethatthepositionislegal*/ /*Assumeuseofaheadernode*/ void Delete(ElementTypeX,ListL) { PositionP,TmpCell; P=FindPrevious(X,L); if(! IsLast(P,L))/*Assumptionofheaderuse*/ { /*Xisfound;deleteit*/ TmpCell=P->Next; P->Next=TmpCell->Next;/*Bypassdeletedcell*/ free(TmpCell); } } /*END*/ /*START: fig3_12.txt*/ /*IfXisnotfound,thenNextfieldofreturnedvalueisNULL*/ /*Assumesaheader*/ Position FindPrevious(ElementTypeX,ListL) { PositionP; /*1*/ P=L; /*2*/ while(P->Next! =NULL&&P->Next->Element! =X) /*3*/P=P->Next; /*4*/ returnP; } /*END*/ /*START: fig3_13.txt*/ /*Insert(afterlegalpositionP)*/ /*Headerimplementationassumed*/ /*ParameterLisunusedinthisimplementation*/ void Insert(ElementTypeX,ListL,PositionP) { PositionTmpCell; /*1*/ TmpCell=malloc(sizeof(structNode)); /*2*/ if(TmpCell==NULL) /*3*/FatalError("Outofspace! ! ! "); /*4*/ TmpCell->Element=X; /*5*/ TmpCell->Next=P->Next; /*6*/ P->Next=TmpCell; } /*END*/ #if0 /*START: fig3_14.txt*/ /*IncorrectDeleteListalgorithm*/ void DeleteList(ListL) { PositionP; /*1*/ P=L->Next;/*Headerassumed*/ /*2*/ L->Next=NULL; /*3*/ while(P! =NULL) { /*4*/free(P); /*5*/ P=P->Next; } } /*END*/ #endif /*START: fig3_15.txt*/ /*CorrectDeleteListalgorithm*/ void DeleteList(ListL) { PositionP,Tmp; /*1*/ P=L->Next;/*Headerassumed*/ /*2*/ L->Next=NULL; /*3*/ while(P! =NULL) { /*4*/Tmp=P->Next; /*5*/ free(P); /*6*/ P=Tmp; } } /*END*/ Position Header(ListL) { returnL; } Position First(ListL) { returnL->Next; } Position Advance(PositionP) { returnP->Next; } ElementType Retrieve(PositionP) { returnP->Element; } 一定范围删除数据 问题: 已知线性表中的元素以值递增有序排列,并以单链表作为存储结构。 试写一高效算法,删除表中所有值大于mink且小于maxk的元素(若表中存在这样的元素),同时释放被删除结点空间,并分析你算法的时间复杂度。 示例: “data.txt”文件中的第一行为递增排列的有序整数,第二行即为mink和maxk。 ●输入(data.txt) 0,1,1,2,4,5,8,10 3,9 ●输出(result.txt) 0,1,1,2,10 问题: 已知线性表中的元素以值递增有序排列,并以单链表作为存储结构。 试写一高效算法,删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值都不相同),同时释放被删除结点空间,并分析你算法的时间复杂度。 示例: ●输入(data.txt) 0,1,1,1,2,4,5,8,8,10 ●输出(result.txt) 0,1,2,4,5,8,10 #include #include #include"fatal.h" typedefintElementType; typedefstructNode*PtrToNode; typedefPtrToNodeList; typedefPtrToNodePosition; structNode { ElementTypeElement; PositionNext; }; voidDeleteList(ListL) { PositionP,Tmp; P=L; while(P! =NULL) { Tmp=P->Next; free(P); P=Tmp; } } ListCreateList(int*mink,int*maxk) { FILE*inFile; charc; intnumber; Listlist=NULL; Positionend; Positionnode; inFile=fopen("data.txt","r"); if(inFile==NULL) { printf("cannotopentheinputfile! \n"); exit (1); } list=(List)malloc(sizeof(structNode)); end=list; do { fscanf(inFile,"%d",&number); node=(Position)malloc(sizeof(structNode)); node->Next=NULL; node->Element=number; end->Next=node; end=node; c=fgetc(inFile); } while(c==','); fscanf(inFile,"%d,%d",mink,maxk); fclose(inFile); returnlist; } voidOutPut(Listlist) { FILE*outFile; outFile=fopen("result.txt","w"); Positionp=list->Next; if(outFile==NULL) { printf("cannotopentheoutputfile! \n"); exit (1); } if(p! =NULL) { while(p->Next! =NULL) { fprintf(outFile,"%d,",p->Element); p=p->Next; } fprintf(outFile,"%d",p->Element); } fclose(outFile); } voidDeleteRange(Listlist,intmink,intmaxk) { PositionpCur=list->Next; PositionpPre=list; PositionpDel=NULL; intn=0; while(pCur! =NULL&&pCur->Element<=mink)/*重要*/ { pPre=pCur; pCur=pCur->Next; } if(pCur==NULL) { printf("Nooutput! \n"); } else { while(pCur! =NULL&&pCur->Element { pDel=pCur; pCur=pCur->Next; n++; free(pDel); } if(pCur==NULL) pPre->Next=NULL; else { pPre->Next=pCur; } printf("delete%dnumbers! \n",n); } } voidDeleteDup(Listlist) { PositionpCur,pNext,pTmp; intcount=0; pCur=list->Next; while(pCur! =NULL) { pNext=pCur->Next; while(pNext! =NULL&&pCur->Element==pNext->Element) { pTmp=pNext; pNext=pNext->Next; free(pTmp); count++; } pCur->Next=pNext; if(pNext==NULL) break; else { pCur->Next=pNext; pCur=pNext; } } printf("Deleted%ditems! \n",count); } intmain(intargc,char*argv[]) {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 七个 程序