ImageVerifierCode 换一换
格式:DOCX , 页数:35 ,大小:22.60KB ,
资源ID:6733589      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6733589.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(七个程序.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

七个程序.docx

1、七个程序多项式相加 HW1p.79 3.6Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation. If the polynomials have M and N terms, respectively, what is the time complexity of your program?#include #include #define COMPARE(numa,numb) (numanumb?-1:(numaExponent = -1; fil

2、e = fopen(filename, r); if (file = NULL) printf(Cantt open %s!n, filename); exit(1); while (!feof(file) val = fscanf(file, %d, %d, &coefficient, &exponent); printf(val = %dn, val); if (exponent Coefficient = coefficient; temp-Exponent = exponent; temp-Next = NULL; tail-Next = temp; tail = temp; fclo

3、se(file); return front;void Output(Polynomial poly, char * filename) FILE * file; file = fopen(filename, w); PtrToNode curNode = poly-Next; if (file = NULL) printf(Cantt open %s!n, filename); exit(1); while (curNode != NULL) fprintf(file, %d,%dn,curNode-Coefficient,curNode-Exponent); curNode = curNo

4、de-Next; fclose(file);void Attach(int coefficient, int exponent, PtrToNode * ptr) PtrToNode temp; temp = (PtrToNode)malloc(sizeof(struct Node); if (temp = NULL) printf(The memory is full!n); exit(1); temp-Coefficient = coefficient; temp-Exponent = exponent; temp-Next = NULL; (*ptr)-Next = temp; *ptr

5、 = temp;Polynomial Add(Polynomial polya, Polynomial polyb) PtrToNode front, tail; int sum; tail = (PtrToNode)malloc(sizeof(struct Node); if (tail = NULL) printf(The memory is full!n); exit(1); front = tail; front-Exponent = -1; polya = polya-Next; polyb = polyb-Next; while (polya & polyb) switch (CO

6、MPARE(polya-Exponent, polyb-Exponent) case -1: Attach(polyb-Coefficient, polyb-Exponent, &tail); polyb = polyb-Next; break; case 0: sum = polya-Coefficient + polyb-Coefficient; if (sum) Attach(sum, polya-Exponent, &tail); polya = polya-Next; polyb = polyb-Next; break; case 1: Attach(polya-Coefficien

7、t, 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; return front;void Delete(Polynomial poly) PtrToNod

8、e node, tmp; node = poly; while (node != NULL) tmp = node-Next; free(node); node = tmp; int main(int argc, char *argv) Polynomial polya; Polynomial polyb; Polynomial polyc; polya = Create(argv1); polyb = Create(argv2); polyc = Add(polya, polyb); Output(polyc, argv3); Delete(polya); Delete(polyb); De

9、lete(polyc); return 0;翻转链表p.80 3.12 a. Write a nonrecursive procedure to reverse a singly linked list in O(N) time.*b. Write a procedure to reverse a singly linked list in O(N) time using constant extra space.#include list.h 翻转链表#include #include fatal.h/* Place in the interface file */struct Node E

10、lementType Element; Position Next;List Reverse( List L ) Position Old_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; return L;void PrintList(List L, FILE * fp) if

11、(fp = NULL) Error(Cant open the output file!n); Position p = L-Next; while (p != NULL) fprintf(fp, %d, p-Element); p = p-Next; if (p != NULL) fprintf(fp, ,); fprintf(fp, n);ListMakeEmpty( List L ) if( L != NULL ) DeleteList( L ); L = malloc( sizeof( struct Node ) ); if( L = NULL ) FatalError( Out of

12、 memory! ); L-Next = NULL; return L;/* START: fig3_8.txt */* Return true if L is empty */intIsEmpty( List L ) return L-Next = NULL;/* END */* START: fig3_9.txt */* Return true if P is the last position in list L */* Parameter L is unused in this implementation */int IsLast( Position P, List L ) retu

13、rn P-Next = NULL;/* END */* START: fig3_10.txt */* Return Position of X in L; NULL if not found */PositionFind( ElementType X, List L ) Position P; /* 1*/ P = L-Next; /* 2*/ while( P != NULL & P-Element != X ) /* 3*/ P = P-Next; /* 4*/ return P;/* END */* START: fig3_11.txt */* Delete from a list */

14、* Cell pointed to by P-Next is wiped out */* Assume that the position is legal */* Assume use of a header node */voidDelete( ElementType X, List L ) Position P, TmpCell; P = FindPrevious( X, L ); if( !IsLast( P, L ) ) /* Assumption of header use */ /* X is found; delete it */ TmpCell = P-Next; P-Nex

15、t = TmpCell-Next; /* Bypass deleted cell */ free( TmpCell ); /* END */* START: fig3_12.txt */* If X is not found, then Next field of returned value is NULL */* Assumes a header */PositionFindPrevious( ElementType X, List L ) Position P; /* 1*/ P = L; /* 2*/ while( P-Next != NULL & P-Next-Element !=

16、X ) /* 3*/ P = P-Next; /* 4*/ return P;/* END */* START: fig3_13.txt */* Insert (after legal position P) */* Header implementation assumed */* Parameter L is unused in this implementation */voidInsert( ElementType X, List L, Position P ) Position TmpCell; /* 1*/ TmpCell = malloc( sizeof( struct Node

17、 ) ); /* 2*/ if( TmpCell = NULL ) /* 3*/ FatalError( Out of space! ); /* 4*/ TmpCell-Element = X; /* 5*/ TmpCell-Next = P-Next; /* 6*/ P-Next = TmpCell;/* END */#if 0/* START: fig3_14.txt */* Incorrect DeleteList algorithm */voidDeleteList( List L ) Position P; /* 1*/ P = L-Next; /* Header assumed *

18、/ /* 2*/ L-Next = NULL; /* 3*/ while( P != NULL ) /* 4*/ free( P ); /* 5*/ P = P-Next; /* END */#endif/* START: fig3_15.txt */* Correct DeleteList algorithm */voidDeleteList( List L ) Position P, Tmp; /* 1*/ P = L-Next; /* Header assumed */ /* 2*/ L-Next = NULL; /* 3*/ while( P != NULL ) /* 4*/ Tmp

19、= P-Next; /* 5*/ free( P ); /* 6*/ P = Tmp; /* END */PositionHeader( List L ) return L;PositionFirst( List L ) return L-Next;PositionAdvance( Position P ) return P-Next;ElementTypeRetrieve( Position P ) return P-Element;一定范围删除数据问题:已知线性表中的元素以值递增有序排列,并以单链表作为存储结构。试写一高效算法,删除表中所有值大于mink且小于maxk的元素(若表中存在这样

20、的元素),同时释放被删除结点空间,并分析你算法的时间复杂度。示例:“data.txt”文件中的第一行为递增排列的有序整数,第二行即为mink和maxk。 输入(data.txt) 0,1,1,2,4,5,8,103,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

21、,8,10#include #include #include fatal.htypedef int ElementType;typedef struct Node *PtrToNode;typedef PtrToNode List;typedef PtrToNode Position;struct Node ElementType Element; Position Next;void DeleteList( List L ) Position P, Tmp; P = L; while( P != NULL ) Tmp = P-Next; free( P ); P = Tmp; List C

22、reateList(int * mink, int * maxk) FILE * inFile; char c; int number; List list = NULL; Position end; Position node; inFile = fopen(data.txt, r); if (inFile = NULL) printf(can not open the input file!n); exit(1); list = (List)malloc(sizeof(struct Node); end = list; do fscanf(inFile, %d, &number); nod

23、e = (Position)malloc(sizeof(struct Node); node-Next = NULL; node-Element = number; end-Next = node; end = node; c = fgetc(inFile); while(c = ,); fscanf(inFile, %d,%d, mink, maxk); fclose(inFile); return list;void OutPut(List list) FILE * outFile; outFile = fopen(result.txt, w); Position p = list-Nex

24、t; if (outFile = NULL) printf(can not open the output file!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);void DeleteRange(List list, int mink, int maxk) Position pCur = list-Next; Position pPre = list; Po

25、sition pDel = NULL; int n = 0; while(pCur != NULL & pCur-Element Next; if (pCur = NULL) printf(No output!n); else while(pCur != NULL & pCur-Element Next; n+; free(pDel); if (pCur = NULL) pPre-Next = NULL; else pPre-Next = pCur; printf(delete %d numbers!n, n); void DeleteDup(List list) Position pCur,

26、 pNext, pTmp; int count = 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 %d items!n, count);int main(int argc, char * argv)

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

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