C++算法与数据结构学习笔记------单链表实现多项式
2016-05-02 23:01
701 查看
//使用单链表实现了多项式的加减和乘法。
#include<iostream.h> template<class T> class List; template<class T> class Node{ friend class List<T>; private: T coef,exp; Node<T> *next; }; template<class T> class List{ private: Node<T> *first; public: List(){first=0;} ~List(); bool Empty() const{return first==0;} int Length()const; int Locate(const T& c,const T& x)const; bool Retrieve(int k,T& c, T& x)const; List<T>& Insert(int k,const T& c, const T& x); List<T>& Delete(int k,T& c,T& x); void PrintList(); List<T>& polyAdd(List<T>& poly2); List<T>& polyMul(List<T>& poly2,List<T>& poly3); List<T>& mergerPoly(); }; template<class T> List<T>::~List() { Node<T> *next; while(first){ next=first->next; delete first; first=next; } } template<class T> int List<T>::Length()const { Node<T> *current=first; int len=0; while(current){ len++; current=current->next; } return len; } template<class T> int List<T>::Locate(const T& c,const T& x)const { Node<T> *current=first; int index=1; while(current&¤t->coef!=c&¤t->exp!=x){ current=current->next; index++; } if(current)return index; return 0; } template<class T> bool List<T>::Retrieve(int k,T& c,T& x)const { if(k<1)return false; Node<T> *current=first; int index=1; while(index<k&¤t){ current=current->next; index++; } if(current){ c=current->coef; x=current->exp; return true; } return false; } template<class T> List<T>& List<T>::Insert(int k,const T& c,const T& x) { Node<T> *p=first; for(int index=1;index<k&&p;index++) p=p->next; Node<T> *y=new Node<T>; y->coef=c; y->exp=x; if(k){ y->next=p->next; p->next=y; } else{ y->next=first; first=y; } return *this; } template<class T> List<T>& List<T>::Delete(int k,T& c,T& x) { Node<T> *p=first; if(k==1) first=first->next; else{ Node<T> *q=first; for(int index=1;index<k-1&&q;index++) q=q->next; if(!q||!q->next) return *this; p=q->next; q->next=p->next; } x=p->exp; c=p->coef; delete p; return *this; } template<class T> void List<T>::PrintList( ) { Node<T> *current; for(current=first;current;current=current->next) { if (current->coef>=0&¤t!=first) { if (0==current->exp) cout<<"+"<<current->coef; else if (1==current->exp) cout<<"+"<<current->coef<<"x"; else cout<<"+"<<current->coef<<"x^"<<current->exp; } else { if (0==current->exp) cout<<current->coef; else if (1==current->exp) cout<<current->coef<<"x"; else cout<<current->coef<<"x^"<<current->exp; } } cout<<endl; } template<class T> List<T>& List<T>::polyAdd(List<T>& poly2) { Node<T> *p=first,*q=poly2.first,*before=first; while(q!=0) { if (p!=0) { if (p->exp<q->exp) { before=p; p=p->next; } else if (p->exp>q->exp) { Insert(Locate(p->coef,p->exp),q->coef,q->exp); q=q->next; } else if (p->exp==q->exp) { p->coef+=q->coef; before=p; p=p->next; q=q->next; } } else { Insert(Length(),q->coef,q->exp); q=q->next; } } return *this; } template <class T> List<T>& List<T>::polyMul(List<T>& poly2,List<T>& poly3) { Node<T> *p=first,*q=poly2.first; int i=0; T c,x; while(p!=0) { while(q!=0) { c=p->coef*q->coef; x=p->exp+q->exp; q=q->next; poly3.Insert(i,c,x); i++; } p=p->next; q=poly2.first; } return *this; } template <class T> List<T>& List<T>::mergerPoly() { Node<T> *p=first,*q=p->next,*beforeQ=first,*temp; while(p!=0&&p->next!=0) { while(q!=0) { if (p->exp==q->exp) { p->coef+=q->coef; temp=q->next; delete q; q=temp; beforeQ->next=q; } else { beforeQ=q; q=q->next; } } p=p->next; beforeQ=p; if (beforeQ!=0) q=p->next; } Node<T> *beforeP=0; p=first; while(p!=0) { if (0==p->coef) { temp=p->next; delete p; p=temp; if (beforeP!=0) beforeP->next=p; else first=p; } else { beforeP=p; p=p->next; } } return *this; } int main() { List<int> poly1,poly2,poly3; int c,x; cout <<"输入第一个多项式,提示:输入0 0,结束多项式输入。"<<endl; while(0!=c||0!=x) { cout <<"请输入多项式的系数:" ; cin >>c; cout <<"请输入多项式的指数:" ; cin >>x; if (c!=0||x!=0) poly1.Insert(poly1.Length(),c,x); } c=1; cout <<"输入第二个多项式,提示:输入0 0,结束多项式输入。"<<endl; while(0!=c||0!=x) { cout <<"请输入多项式的系数:" ; cin >>c; cout <<"请输入多项式的指数:" ; cin >>x; if (c!=0||x!=0) poly2.Insert(poly2.Length(),c,x); } cout<<"A(x)="; poly1.PrintList(); cout<<"B(x)="; poly2.PrintList(); cout <<"请输入要做的运算,1加法,2乘法:"; cin >>c; if (1==c) { poly1.polyAdd(poly2); poly1.mergerPoly(); cout <<"A(x)+B(x)="; poly1.PrintList(); } else if(2==c) { poly1.polyMul(poly2,poly3); poly3.mergerPoly(); cout <<"A(x)*B(x)="; poly3.PrintList(); } else cout<<"输入错误"; return 0; }
相关文章推荐
- 数据结构初学过程中对算法效率度量的理解
- 【数据结构】二叉树相关操作
- Java千百问_06数据结构(006)_java基本数据类型如何转换
- 二叉树主要知识点总结
- HDU 2874 LCA转RMQ+并查集
- HDU 2586 LCA转RMQ
- ZOJ 3195 LCA转RMQ
- 数据结构 并查集
- Java千百问_06数据结构(005)_数值中为什么会出现下划线
- 数据结构之排序:归并排序
- 数据结构之排序:堆排序
- 数据结构之排序:直接选择排序
- nginx的高级数据结构
- LINUX 数据结构 &算法 网络协议 & 网络编程 多任务编程
- 02-线性结构1 一元多项式的乘法与加法运算[网易云课堂-数据结构]
- 数据结构之排序:快速排序
- 数据结构与算法分析 c++描述 读书笔记(2)
- 数据结构之排序:冒泡排序
- 数据结构进阶(二)矩阵(稀疏矩阵)的压缩存储
- 数据结构进阶(二)矩阵(稀疏矩阵)的压缩存储