七个程序Word下载.docx

上传人:b****5 文档编号:19750695 上传时间:2023-01-09 格式:DOCX 页数:35 大小:22.60KB
下载 相关 举报
七个程序Word下载.docx_第1页
第1页 / 共35页
七个程序Word下载.docx_第2页
第2页 / 共35页
七个程序Word下载.docx_第3页
第3页 / 共35页
七个程序Word下载.docx_第4页
第4页 / 共35页
七个程序Word下载.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

七个程序Word下载.docx

《七个程序Word下载.docx》由会员分享,可在线阅读,更多相关《七个程序Word下载.docx(35页珍藏版)》请在冰豆网上搜索。

七个程序Word下载.docx

front=tail;

front->

Exponent=-1;

file=fopen(filename,"

r"

if(file==NULL)

Cant'

topen%s!

filename);

while(!

feof(file))

val=fscanf(file,"

%d,%d"

&

coefficient,&

exponent);

val=%d\n"

val);

if(exponent<

-1)

InFile%s:

theexponentofpolynomialisnotlessthan0!

temp=(PtrToNode)malloc(sizeof(structNode));

if(temp==NULL)

temp->

Coefficient=coefficient;

Exponent=exponent;

Next=NULL;

tail->

Next=temp;

tail=temp;

fclose(file);

returnfront;

}

voidOutput(Polynomialpoly,char*filename)

w"

PtrToNodecurNode=poly->

Next;

while(curNode!

=NULL)

fprintf(file,"

%d,%d\n"

curNode->

Coefficient,curNode->

Exponent);

curNode=curNode->

voidAttach(intcoefficient,intexponent,PtrToNode*ptr)

PtrToNodetemp;

(*ptr)->

*ptr=temp;

PolynomialAdd(Polynomialpolya,Polynomialpolyb)

PtrToNodefront,tail;

intsum;

polya=polya->

polyb=polyb->

while(polya&

&

polyb)

switch(COMPARE(polya->

Exponent,polyb->

Exponent))

case-1:

Attach(polyb->

Coefficient,polyb->

Exponent,&

tail);

break;

case0:

sum=polya->

Coefficient+polyb->

Coefficient;

if(sum)

Attach(sum,polya->

case1:

Attach(polya->

Coefficient,polya->

for(;

polya;

Next)

Coefficient,polya->

Exponent,&

polyb;

Coefficient,polyb->

voidDelete(Polynomialpoly)

PtrToNodenode,tmp;

node=poly;

while(node!

tmp=node->

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"

翻转链表

fatal.h"

/*Placeintheinterfacefile*/

ElementTypeElement;

PositionNext;

ListReverse(ListL)

PositionOld_head,New_head,Temp;

New_head=NULL;

Old_head=L->

while(Old_head)

Temp=Old_head->

Old_head->

Next=New_head;

New_head=Old_head;

Old_head=Temp;

};

L->

returnL;

voidPrintList(ListL,FILE*fp)

if(fp==NULL)

Error("

Can'

topentheoutputfile!

Positionp=L->

while(p!

fprintf(fp,"

%d"

p->

Element);

p=p->

if(p!

"

List

MakeEmpty(ListL)

if(L!

=NULL)

DeleteList(L);

L=malloc(sizeof(structNode));

if(L==NULL)

FatalError("

Outofmemory!

"

);

/*START:

fig3_8.txt*/

/*ReturntrueifLisempty*/

int

IsEmpty(ListL)

returnL->

Next==NULL;

/*END*/

fig3_9.txt*/

/*ReturntrueifPisthelastpositioninlistL*/

/*ParameterLisunusedinthisimplementation*/

intIsLast(PositionP,ListL)

returnP->

fig3_10.txt*/

/*ReturnPositionofXinL;

NULLifnotfound*/

Position

Find(ElementTypeX,ListL)

PositionP;

/*1*/

P=L->

/*2*/

while(P!

=NULL&

P->

Element!

=X)

/*3*/P=P->

/*4*/

returnP;

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=TmpCell->

/*Bypassdeletedcell*/

free(TmpCell);

fig3_12.txt*/

/*IfXisnotfound,thenNextfieldofreturnedvalueisNULL*/

/*Assumesaheader*/

FindPrevious(ElementTypeX,ListL)

P=L;

while(P->

Next!

Next->

fig3_13.txt*/

/*Insert(afterlegalpositionP)*/

/*Headerimplementationassumed*/

Insert(ElementTypeX,ListL,PositionP)

PositionTmpCell;

TmpCell=malloc(sizeof(structNode));

if(TmpCell==NULL)

/*3*/FatalError("

Outofspace!

!

TmpCell->

Element=X;

/*5*/

Next=P->

/*6*/

Next=TmpCell;

#if0

fig3_14.txt*/

/*IncorrectDeleteListalgorithm*/

DeleteList(ListL)

/*Headerassumed*/

/*3*/

/*4*/free(P);

P=P->

#endif

fig3_15.txt*/

/*CorrectDeleteListalgorithm*/

PositionP,Tmp;

/*4*/Tmp=P->

free(P);

P=Tmp;

Header(ListL)

First(ListL)

Advance(PositionP)

ElementType

Retrieve(PositionP)

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

试写一高效算法,删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值都不相同),同时释放被删除结点空间,并分析你算法的时间复杂度。

0,1,1,1,2,4,5,8,8,10

0,1,2,4,5,8,10

typedefintElementType;

typedefPtrToNodeList;

typedefPtrToNodePosition;

voidDeleteList(ListL)

Tmp=P->

ListCreateList(int*mink,int*maxk)

FILE*inFile;

charc;

intnumber;

Listlist=NULL;

Positionend;

Positionnode;

inFile=fopen("

data.txt"

"

if(inFile==NULL)

cannotopentheinputfile!

list=(List)malloc(sizeof(structNode));

end=list;

do

fscanf(inFile,"

number);

node=(Position)malloc(sizeof(structNode));

node->

Element=number;

end->

Next=node;

end=node;

c=fgetc(inFile);

while(c=='

'

%d,%d"

mink,maxk);

fclose(inFile);

returnlist;

voidOutPut(Listlist)

FILE*outFile;

outFile=fopen("

result.txt"

Positionp=list->

if(outFile==NULL)

cannotopentheoutputfile!

while(p->

fprintf(outFile,"

%d,"

fclose(outFile);

voidDeleteRange(Listlist,intmink,intmaxk)

PositionpCur=list->

PositionpPre=list;

PositionpDel=NULL;

intn=0;

while(pCur!

pCur->

Element<

=mink)/*重要*/

pPre=pCur;

pCur=pCur->

if(pCur==NULL)

Nooutput!

else

maxk)

pDel=pCur;

n++;

free(pDel);

pPre->

Next=pCur;

delete%dnumbers!

n);

voidDeleteDup(Listlist)

PositionpCur,pNext,pTmp;

intcount=0;

pCur=list->

pNext=pCur->

while(pNext!

=NULL&

Element==pNext->

Element)

pTmp=pNext;

pNext=pNext->

free(pTmp);

count++;

Next=pNext;

if(pNext==NULL)

pCur=pNext;

Deleted%ditems!

count);

intmain(intargc,char*argv[])

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

当前位置:首页 > 医药卫生 > 基础医学

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

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