南邮数据结构实验一
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告
(2014 / 2015 学年第二学期)
课程名称数据结构
实验名称线性表的基本运算及多项式的算术运算
实验时间2015 年9 月28 日
指导单位计算机科学与技术系
指导教师黄海平
学生姓名陈明阳班级学号Q
学院(系) 贝尔英才专业信息科技强化班
实验报告
~SeqList() { delete[] elements; }
bool IsEmpty() const;
int Length() const;
bool Find(int i, T& x) const;
int Search(T x) const;
bool Insert(int i, T x);
bool Delete(int i);
bool Update(int i, T x);
void Output(ostream& out)const;
private:
int maxLength;
T *elements;
};
template<class T>
SeqList<T>::SeqList(int mSize)
{
maxLength = mSize;
elements = new T[maxLength];
n = 0;
}
template<class T>
bool SeqList<T>::IsEmpty() const
{
return n == 0;
}
template<class T>
int SeqList<T>::Length()const
{
return n;
}
template<class T>
bool SeqList<T>::Find(int i, T& x)const
{
if (i<0 || i>n - 1)
{
cout <<"out of bounds"<< endl; return false;
}
x = elements[i];
return true;
}
template<class T>
int SeqList<T>::Search(T x)const
{
for (int j = 0; j < n; j++)
if (elements[j] == x)return j;
return -1;
}
template<class T>
bool SeqList<T>::Insert(int i, T x)
{
if (i<-1 || i>n - 1)
{
cout <<"out of bounds"<< endl;
return false;
}
if (n == maxLength)
{
cout <<"over flow"<< endl;
return false;
}
for (int j = n - 1; j > i; j--)elements[j + 1] = elements[j];
elements[i + 1] = x;
n++;
return true;
}
template<class T>
bool SeqList<T>::Delete(int i)
{
if (i<0 || i>n - 1)
{
cout <<"out of bounds"<< endl;
return false;
}
if (!n)
{
cout <<"over flow"<< endl;
return false;
}
for (int j = i+1; j <n; j--)elements[j -1] = elements[j];
n--;
return true;
}
template<class T>
bool SeqList<T>::Update(int i, T x)
{
if (i<0 || i>n - 1)
{
cout <<"out of bounds"<< endl;
return false;
}
elements[i] = x;
return true;
}
template<class T>
void SeqList<T>::Output(ostream& out)const
{
for (int i = 0; i < n; i++)out << elements[i] << " ";
out<< endl;
}
源.cpp:
#include"seqlist.h"
const int SIZE = 20;
void main()
{
SeqList<int> LA(SIZE);
int i = 0;
for (i = 0; i<5; i++) LA.Insert(i - 1, i);
LA.Insert(-1, 10);
LA.Output(cout);
}
实现在线性表LA中插入0-4然后在一开始插入10 运行截图如下:
多项式实验:
定义类如下
重构函数如下:
源码:
#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 (0 == val.coef)
return out;
if (1!= val.coef)
out<<val.coef;
switch (val.exp)
{
case 0:break;
case 1:out<<"X"; break;
default:out<<"X^"<<val.exp; break;
}
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 &, Polynominal &); };
Polynominal::Polynominal()
{
theList = new Term(0, -1); //头结点
theList->link = NULL; //单链表尾结点指针域为空
}
Polynominal::~Polynominal()
{
Term* p = theList->link;
while (p != NULL)
{
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 a term(coef,exp):\n"<< endl;
cin >> c >> e;
q = q->InsertAfter(c, e);
if (0 >= e) break;
}
}
void Polynominal::Output(ostream& out)const
{
int first = 1;
Term *p = theList->link;
for (; p != NULL && p->exp >= 0; p = p->link)
{
if (!first && (p->coef>0)) out<<"+";
first = 0;
out<< *p;
}
cout << endl;
}
void Polynominal::PolyAdd(Polynominal& r)
{
Term *q, *q1 = theList, *p; //q1指向表头结点
p = r.theList->link; //p指向第一个要处理的结点
q = q1->link; //q1是q的前驱,p和q就指向两个当前进行比较的项while (p != NULL && p->exp >= 0)//对r的单循环链表遍历,知道全部结点都处理完
{
while (p->exp < q->exp) //跳过q->exp大的项
{
q1 = q;
q = q->link;
}
if (p->exp == q->exp) //当指数相等时,系数相加
{
q->coef = q->coef + p->coef;
if (q->coef == 0) //若相加后系数为0,则删除q
{
q1->link = q->link;
delete(q);
q = q1->link; //重置q指针
}
else
{
q1 = q; //若相加后系数不为0,则移动q1和q
q = q->link;
}
}
else//p>exp>q->exp的情况
q1 = q1->InsertAfter(p->coef, p->exp); //以p的系数和指数生成新结点,插入q1后 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()
实验报告
文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.。