1.第一部分定义链表结构和相关操作。文件名称:comm.h
#ifndef _COMM_H__
#define _COMM_H__
#include
#include
#include
using namespace std;
string trim(string &strIn);
typedef struct Element
{
float index;
int exp;
}Element;
typedef int Status;
#define ERROR 0
#define OK 1
#define OVERFLOW -1
typedef struct LNode
{
Element data;
struct LNode *next;
}*Link,*Position;
typedef struct
{
Link head;
Link tail;
int len;
}LinkList;
namespace linkListName{
Status InitList(LinkList &L)
{
Link p = (Link)malloc(sizeof(LNode));
if( !p )
return ERROR;
p->next = NULL;
L.head = p;
L.tail = p;
L.len = 1;
return OK;
}
void Destroy(LinkList &L)
{
Link p = L.head;
Link q;
while(p!=L.tail)
{
q=p;
++p;
if(q) free(q);
}
}
Status MakeNode(Link &p,Element e)
{
Link ptr = (Link)malloc(sizeof(LNode));
if(!ptr) return ERROR;
ptr->data = e;
ptr->next = NULL;
p=ptr;
return OK;
}
void FreeNode( Link p )
{
if(p)
free(p);
}
Link GetHead(LinkList &L)
{
return L.head;
}
Status LinkEmpty(LinkList &L)
{
return L.len == 0;
}
int LengthLink(LinkList &L)
{
return L.len;
}
void ClearLink(LinkList &L)
{
}
Status LocateElem(LinkList &L,Link &p,int i)
{
if( i > L.len)
return OVERFLOW;
Link ha = GetHead(L);
int j=1;
while( j < i + 1 )
{
j++;
ha = ha->next;
}
p = ha;
return OK;
}
Status InsAfterElem(LinkList &L,Link &p,Element e)
{
Link ptr;
if( !MakeNode(ptr,e) )
return OVERFLOW;
if( !p->next )
{
L.tail = ptr;
}
ptr->next = p->next;
p->next=ptr;
++L.len;
return OK;
}
Status DelAfterElem(LinkList &L,Link p)
{
if(!p->next)
return OVERFLOW;
if( !(p->next)->next )
L.tail = p;
Link q = p->next;
p->next = q->next;
FreeNode(q);
return OK;
}
void SetElem(Link p,Element e)
{
p->data = e;
}
}
#endif
2.第二部分添加main函数以及多项式相加函数等。文件名称:LinkListTest.cpp.代码如下:
1 #include
2 #include "..\..\include\comm.h"
3 #include "..\..\include\MainFunc.h"
4 using namespacestd;5 using namespacelinkListName;6
7 intcomp(Element a,Element b)8 {9 if(a.exp ==b.exp)10 return 0;11 if(a.exp next;19 while(la)20 {21 Element a = la->data;22 cout<next;24 }25 cout<next;33 Link lb = hb->next;34
35 Element a = la->data;36 Element b = lb->data;37 Link ptmp;38 Link pcur;39
40 while( la &&lb )41 {42 a = la->data;43 b = lb->data;44
45 int ret =compare(a,b);46 switch(ret)47 {48 case -1:49 LocateElem(pc,pcur,pc.len-1);50 InsAfterElem(pc,pcur,a);51 la = la->next;52 break;53 case 0:54 if( !( (a.index + b.index) > -0.005 && (a.index + b.index) < 0.005) )55 {56 Element c;57 c.index = a.index +b.index;58 c.exp =a.exp;59 LocateElem(pc,pcur,pc.len-1);60 InsAfterElem(pc,pcur,c);61 }62 la = la->next;63 lb = lb->next;64 break;65 case 1:66 LocateElem(pc,pcur,pc.len-1);67 InsAfterElem(pc,pcur,b);68 lb = lb->next;69 break;70 default:71 break;72 }73 }74
75 while( la != (pa.tail)->next )76 {77 a = la->data;78 LocateElem(pc,pcur,pc.len-1);79 InsAfterElem(pc,pcur,a);80 la = la->next;81 }82 while( lb != (pb.tail)->next )83 {84
85 a = lb->data;86 LocateElem(pc,pcur,pc.len-1);87 InsAfterElem(pc,pcur,a);88 lb = lb->next;89 }90 }91
92
93 voidfunc1()94 {95 Element a;96 a.index=1;97 a.exp = 2;98 Element b;99 b.index=3;100 b.exp = 4;101 Element c;102 c.index=2;103 c.exp = 6;104 Element array1[3] ={a,b,c};105
106 Element aa;107 aa.index=1;108 aa.exp = 2;109 Element ab;110 ab.index=3;111 ab.exp = 3;112 Element ac;113 ac.index=1;114 ac.exp = 4;115 Element array2[3] ={aa,ab,ac};116
117 LinkList pa;118 LinkList pb;119 InitList(pa);120 InitList(pb);121 Link pcur;122 for(int i = 0 ; i < 3 ; i++)123 {124
125 LocateElem(pa,pcur,pa.len-1);126
127 InsAfterElem(pa,pcur,array1[i]);128
129 LocateElem(pb,pcur,pb.len-1);130 InsAfterElem(pb,pcur,array2[i]);131 }132
133 LinkList pc;134 InitList(pc);135 addPolynomial(pa,pb,pc,comp );136 cout<
143
144 }145
146 intmain()147 {148 cout<
View Code
3.编译文件,本人使用的是mingw32-make
LinkList.exe:LinkListTest.cpp
g++ -m64 -I../../include -L../../../lib LinkListTest.cpp -lcomm -o LinkList.exe
#move ./test.exe ../../../bin/
本人文件目录说明:
H:\myProject\src\include\comm.h
H:\myProject\src\module\LinkListTest\LinkListTest.cpp
4.运行结果:
H:\myProject\src\module\LinkListTest>LinkList
before func1 输入的第一个多项式:
1*X^2+3*X^4+2*X^6+
输入的第二个多项式:
1*X^2+3*X^3+1*X^4+
相加后的结果: 2*X^2+3*X^3+4*X^4+2*X^6+
after func1
H:\myProject\src\module\LinkListTest>