线性表的基本运算

数据结构用c++实现第一章节源码:
实现顺序表和单链表的基本运算,多项式的加法和乘法算术运算。
要求:能够正确演示线性表的查找、插入、删除运算。实现多项式的加法和乘法运算操作

实现顺序表和单链表的基本运算

Linearlist.h:

#include "iostream.h"

template <class T>
class Linearlist
{
    protected:
        int n;     //线性表长度
    public:
        virtual bool Find(int i,T& x)=0;
        virtual int Search(T x)=0;
        virtual bool Insert(int i,T x)=0;
        virtual bool Delete(int i)=0;
};

Node.h:

#include "iostream.h"

template <class T>
class Node
{
    private:
        T element;
        Node<T> *link;
};

SeqList.h:

#include "Linearlist.h"

template <class T>
class SeqList:public Linearlist<T>
{
    public:
        SeqList(int msize);
        ~SeqList();
        bool Find(int i,T& x) ;
        int Search(T x) ;
        bool Insert(int i,T x);
        bool Delete(int i);
    private:
        int maxLength;
        T *elements;
        };

template <class T>
SeqList<T>::SeqList(int msize)
{
    maxLength=msize;
    elements=new T[maxLength];
    n=0;
}

template <class T>
SeqList<T>::~SeqList()
{
    delete []elements;
}

template <class T>
bool SeqList<T>::Find(int i,T &x)
{
    if(i<0 || i>=n)
    {
        cout<<"out of bounds"<<endl;
        return false;
    }
    else
    {
        if(x==elements[i])
        {return true;}
        else
        {return false;}

    }
}

template <class T>
int SeqList<T>::Search(T x)
{
    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<0 || i>n)
    {
        cout<<"out of bounds"<<endl;
        return false;
    }
    if(n==maxLength)
    {
        cout<<"overflow"<<endl;
    }
    for(int j=n-1;j>i;j--)
    {
        elements[j]=elements[j-1];
    }
    elements[i]=x;
    n++;
    return true;
}
template <class T>
bool SeqList<T>::Delete(int i)
{
    if(i<0 || i>=n)
    {
        cout<<"out of bounds"<<endl;
        return false;
    }
    if(!n)
    {
        cout<<"overflow"<<endl;
    }
    for(int j=i;j<n-1;j++)
    {
        elements[j]=elements[j+1];
    }
    n--;
    return true;
}

SingelList:

#include "Linearlist.h"
#include "Node.h"
template <class T>
class SingelList:public Linearlist<T>
{
    private:
        Node<T> *first;
    public:
        SingelList();
        ~SingelList();
        bool Find(int i,T& x);
        int Search(T x);
        bool Insert(int i,T x);
        bool Delete(int i);

};

template <class T>
SingelList<T>::SingelList()
{
    first=NULL;
    n=0;
}

template <class T>
SingelList<T>::~SingelList()
{
    Node<T> *p;
    while(first)
    {
        p=first->link;
        delete first;
        first=p;
    }
}

template <class T>
bool SingelList<T>::Find(int i,T &x)
{
    if(i<0 || i>n-1)
    {
        cout<<"out of bounds"<<endl;
        return false;
    }
    Node<T> *p=first;
    for(int j=0;j<n-1;j++)
    {
        p=p->link;
        x=p->element;
    }
    return true;
}
template <class T>
int SingelList<T>::Search(T x)
{
    Node<T> *p=first;
    for(int j=0;p&&p->element!=x;j++)
    {
        p=p->link;
    }
    if(p)
    {
        return j;
    }
    return -1;
}


template <class T>
bool SingelList<T>::Insert(int i,T x)
{
    if(i<-1 || i>n-1)
    {
        cout<<"out of bounds"<<endl;
        return false;
    }   
    Node<T> *q=new Node;
    q->element=x;

    Node<T> *p=first;

    for(int j=0;j<i;j++)
    {
        p=p->link;
    }
    if(i!=-1)
    {
        q->link=p->link;
        p->link=q;
    }
    else
    {
        q->link=first;
        first=q;
    }
    n++;
    return true;
}
template <class T>
bool SingelList<T>::Delete(int i)
{
    if(i<0 || i>n-1)
    {
        cout<<"out of bounds"<<endl;
        return false;
    }
    if(!n)
    {
        cout<<"overflow"<<endl;
        return false;
    }

    Node<T> *p=first;
    for(int j=0;j<i-1;j++)
    {
        p=p->link;
    }
    if(i!=0)
    {
       p->link=p->link->link;
    }
    else
    {
        first=first->link;
    }

    delete p;
    n--;
    return true;
}

Main.cpp:

#include "iostream.h"
#include "SeqList.h"


void main()
{
    SeqList <int> se1(5);
    int k,x;
    for(int i=0;i<5;i++)
    {
        cout<<"please input"<<endl;
        cin>>k;
        se1.Insert(i,k);
    }
    cout<<"please input i & x"<<endl;
    cin>>i>>x;
    cout<<"se1.find(i,x): "<<se1.Find(i,x)<<endl;
    cout<<"Here end OK."<<endl;
}

多项式的实验部分给了我很大的阻碍,最大的原因在于我在写程序之前没有预演算法实现,而是拿到就写固然除了大问题以至于调试程序花了非常长的时间。 调试过程中也发现自己有很多编程细节做得不够好,野指针的问题最为明显,其他的问题列如,重载运算符要包含头文件cstring,类外实现函数形式是返回值 类名::函数(…),函数定义时有template的实例化的时候要标明数据类型,实例化指向类对象的指针时要在一开始就赋值,ostream和istream的区别,this指针的使用,const参数的匹配问题……

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值