浅谈线性表的基本操作与应用
2016-09-26 12:18
302 查看
线性表作为一种线性数据结构,常应用于信息检索,存储管理等诸多领域,因此了解线性表的基本操作与应用对于我们学习数据结构有着十分重要的意义。
[b]一,线性表的基本操作[/b]
首先,我们定义一个线性表的基类linearlist,并以此定义了它的派生类顺序表类seqlist和链表类singlelist.在基类中,我们以抽象函数的形式定义了线性表常用的几种操作,如插入删除等。
View Code
运行结果如图:
[b]一,线性表的基本操作[/b]
首先,我们定义一个线性表的基类linearlist,并以此定义了它的派生类顺序表类seqlist和链表类singlelist.在基类中,我们以抽象函数的形式定义了线性表常用的几种操作,如插入删除等。
#include<iostream> using namespace std; class Term { public: Term(int c,int e); Term(int c,int e,Term *nxt); Term *InsertAfter(int c,int e); private: int coef; int exp; Term *link; friend ostream &operator<<(ostream &,const Term &); friend class Polynominal; }; Term::Term(int c,int e):coef(c),exp(e) { link=0; } Term::Term(int c,int e,Term *nxt):coef(c),exp(e) { link=nxt; } Term* Term::InsertAfter(int c,int e) { link=new Term(c,e,link); return link; } ostream &operator<<(ostream &out,const Term &val) { if(val.coef==0) return out; out<<val.coef; switch(val.exp) { case 0:break; case 1:out<<"X";break; default:out<<"X^"<<val.exp; } return out; } class Polynominal { public: Polynominal(); ~Polynominal(); void AddTerms(istream &in); void Output(ostream &out)const; void PolyAdd(Polynominal& r); void PolyMul(Polynominal& r); private: Term *theList; friend ostream&operator<<(ostream &,const Polynominal &); friend istream &operator>>(istream &,Polynominal &); friend Polynominal& operator+(Polynominal &,Polynominal&); friend Polynominal & operator * (Polynominal &a, Polynominal &b); }; Polynominal::Polynominal() { theList=new Term(0,-1); theList->link=theList; } Polynominal::~Polynominal() { Term *p=theList->link; while(p!=theList) { theList->link=p->link; delete p; p=theList->link; } delete theList; } void Polynominal::AddTerms(istream &in) { Term *q=theList; int c,e; for(;;) { cout<<"input the term(coef,exp):\n"; cin>>c>>e; if(e<0) break; q=q->InsertAfter(c,e); } } void Polynominal::Output(ostream &out)const { int first=1; Term *p=theList->link; cout<<"The polynominal is:\n"<<endl; for(;p!=theList;p=p->link) { if(!first&&(p->coef>0)) cout<<"+"; first=0; out<<*p; } cout<<"\n"<<endl; } void Polynominal::PolyAdd(Polynominal &r) { Term *q,*q1=theList,*p; p=r.theList->link; q=q1->link; while(p->exp>=0) { while(p->exp<q->exp) { q1=q; q=q->link; } if(p->exp==q->exp) { q->coef=q->coef+p->coef; if(q->coef==0) { q1->link=q->link; delete(q); q=q1->link; } else { q1=q; q=q->link; } } else q1=q1->InsertAfter(p->coef,p->exp); p=p->link; } } void Polynominal::PolyMul(Polynominal& r) { Polynominal result; //定义相乘后的数据 Term *n = result.theList; //n指向result的头结点 n = n->InsertAfter(0, 0); //在result的头结点后插入新结点,系数指数均为0 Term *p = r.theList->link; //p指向第一个要处理的结点 while(p->exp >= 0) //对r的单循环链表遍历 { Polynominal tmp; //存储某段相乘后的数据 Term *m = tmp.theList; //m指向tmp的头结点 Term *q = theList->link; //q指向表头结点的后继结点 while(q->exp >= 0) //对当前对象的单循环环链表遍历 { m = m->InsertAfter((p->coef)*(q->coef), (p->exp) + (q->exp)); //生成新结点插入n后 q = q->link; } result.PolyAdd(tmp); //将temp加到result上 p = p->link; } Term *q = theList->link; //q指向表头结点的后继结点 while(q != NULL) //删除原对象的所有数据 { theList->link = q->link; delete q; q = theList->link; } q = theList; q = q->InsertAfter(0, 0); PolyAdd(result); //将result加到当前对象上 } ostream&operator<<(ostream &out,const Polynominal &x) { x.Output(out); return out; } istream &operator>>(istream &in,Polynominal &x) { x.AddTerms(in); return in; } Polynominal &operator+(Polynominal &a,Polynominal&b) { a.PolyAdd(b); return a; } Polynominal & operator * (Polynominal &a, Polynominal &b) { a.PolyMul(b); return a; } int main() { Polynominal p,q; cin>>p; cout<<p; cin>>q; cout<<q; q=q+p; cout<<q; q=p*q; cout<<q; return 0; }
View Code
运行结果如图:
![](https://images2015.cnblogs.com/blog/904122/201609/904122-20160926125508875-1127920910.png)
相关文章推荐
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一 线性表的基本操作实现及其应用
- 实验一线性表的基本操作及其应用
- 实验一:线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 爹地的实验:实验一: 数据结构实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一:线性表的基本操作实现及其应用(C++)
- 实验一线性表的基本操作实现及其应用
- 实验一 线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用(JavaScript实现)
- 线性表的基本操作及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 数据结构实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用
- 实验一线性表的基本操作实现及其应用