链表—应用实例
链表基本类型结构
基本操作:在指定位置插入节点,删除节点,添加节点
链表实现
template <class T> class list
{
public:
list();
~list();
void insert(list_node<T> *,const T &);
void deletenode(list_node<T> *);
void addnode(const T &);
void print();
private:
list_node<T> *head;
list_node<T> *rear;
};
template <class T> list<T>::list()
{
head=new list_node<T>();
if (head==NULL)
cerr<<"new error"<<endl;
head->next=NULL;
rear=head;
}
template<class T> void list<T>::addnode(const T &value)
{
list_node<T>* p=new list_node<T>();
p->value=value;
p->next=NULL;
rear->next=p;
rear=p;
}
template<class T>void list<T>::insert(list_node<T> *pos,const T &value)
{
list_node<T>* p=new list_node<T>;
list_node<T>*q=p->next
p->value=value;
p->next=pos->next;
pos->next=p;
}
template<class T> void list<T>::deletenode(list_node<T> *pos)
{
list_node<T> *p=head->next;
while (p->next!=pos&&p->next!=NULL)
{
p=p->next;
}
if (p->next==NULL)
{
cout<<"count find postion"<<endl;
}
p->next=pos->next;
delete pos;
}
template <class T> list<T>::~list()
{
list_node<T> *p=head,*q=NULL;
while (p)
{
q=p->next;
delete p;
p=q;
}
}
template<class T> void list<T>::print()
{
list_node<T> *p=head->next;
while (p)
{
cout<<p->value<<endl;
p=p->next;
}
}
1.链表实现多项式加法
如:a=3x^14+2x^8+1 b=8x^14-3x^10+10x^6
多项式链式存储:
struct poly_node{
int coef; //系数
int expen; //指数
poly_node *next;
};
多项式加法
poly_node * poly_add(poly_node *a,poly_node *b)
{
poly_node *ap=a,*bp=b;
poly_node *cp=NULL;
poly_node *c=NULL;
poly_node *p=NULL;
poly_node *left=NULL;
while (ap!=NULL&&bp!=NULL)
{
p=(poly_node *)malloc(sizeof(poly_node));
if(ap->expen==bp->expen)
{
p->coef=ap->coef+bp->coef;
p->expen=ap->expen;
p->next=NULL;
ap=ap->next;
bp=bp->next;
}
else if (ap->expen>bp->expen)
{
p->coef=ap->coef;
p->expen=ap->expen;
p->next=NULL;
ap=ap->next;
}
else
{
p->coef=bp->coef;
p->expen=bp->expen;
p->next=NULL;
bp=bp->next;
}
if(cp==NULL)
{
cp=p;
c=cp;
}
else
{
cp->next=p;
cp=p;
}
}
if(ap==NULL&&bp!=NULL)
{
left=bp;
}
else if (ap!=NULL&&bp==NULL)
{
left=ap;
}
else
left=NULL;
while (left)
{
p=new poly_node;
p->coef=left->coef;
p->expen=left->expen;
p->next=NULL;
cp->next=p;
cp=cp->next;
left=left->next;
}
return c;
}
//将一个字符串表示的多项式转化链表形式
poly_node * str2node(const char * str)
{
poly_node * start=NULL;
const char * p=str,*q=NULL;
poly_node *newnode;
poly_node *rear=NULL;;
int value=0;
int exp=0;
int flag=1;
while (*p)
{
value=0;
exp=0;
flag=1;
if (isdigit(*p))
{
q=p;
flag=1;
}
else if(*p=='-')
{
flag=-1;
q=p+1;
value=0;
}
else if (*p=='+')
{
flag=1;
q=p+1;
value=0;
}else
{
value=1;
flag=1;
}
while (*q!='x'&&*q!='X')
{
value=value*10+*q-48;
q++;
}
value=value*flag;
q++;
if(*q=='-')
{
flag=-1;
q++;
}else
{
flag=1;
}
while (*q!='\0'&&*q!='+'&&*q!='-')
{
exp=exp*10+*q-48;
q++;
}
exp=exp*flag;
newnode=new poly_node;
newnode->coef=value;
newnode->expen=exp;
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
p=q;
}
return start;
}
//将链表转化为字符串形式
void node2str(poly_node *start,char *str)
{
poly_node *p=start;
char temp[64];
while (p)
{
sprintf(temp,"%+dx%d",p->coef,p->expen);
strcat(str,temp);
p=p->next;
}
}

浙公网安备 33010602011771号