线性表的链式存储

上次分享了顺序存储的主要代码,这次的链式存储因为使用到了指针就比较难了,一不小心就会出现错误。

主类:

struct Node
{
    T data;
    Node<T> *next;
};
template<class T>
class Linklist
{
    Node<T> *first;
public:
    Linklist();
    Linklist(T a[],int n);
    Linklist(int n,T a[]);
    ~Linklist();
    int outlength();
    T getdata(int i);
    int location(T x);
    void insertnew(T x, int i);
    void Deletes(int i);
    int emptys();
    void bianli();
};

功能实现:

构造函数

无参构造函数:

Linklist<T>::Linklist()
{
    first=new Node<T>;
    first->next=NULL;
}

尾插构造函数:

template<class T>
Linklist<T>::Linklist(int n,T a[])
{
    first=new Node<T>;
    first->next=NULL;
    Node<T> *s,*r=first;
    for(int i=0;i<n;i++)
    {
        s=new Node<T>;
        s->data=a[i];
        r->next=s;
        r=s;
    }
    r->next=NULL;
}

头插构造函数:

template<class T>
Linklist<T>::Linklist(T a[],int n)
{
    first=new Node<T>;
    first->next=NULL;
    Node<T> *s;
    for(int i=0;i<n;i++)
    {
        s=new Node<T>;
        s->data=a[i];
        s->next=first->next;
        first->next=s;
    }
}
析构函数
template<class T>
Linklist<T>::~Linklist()
{
    Node<T> *p=first;
    while(first)
    {
        first=first->next;
        delete p;
        p=first;
    }
}
数据数量
template<class T>
int Linklist<T>::outlength()
{
    Node<T> *p=first->next;
    int all=0;
    while(p)
    {
        p=p->next;
        all++;
    }
    return all;
}
按位置查询数据
template<class T>
T Linklist<T>::getdata(int i)
{
    Node<T> *p=first->next;
    int all=1;
    while(p&&all<i)
    {
        p=p->next;
        all++;
    }
    if(p==NULL)
    {
        cout<<"error";
    }
    else
    {
        return p->data;
    }
}
查询数据在链表中的位置
template<class T>
int Linklist<T>::location(T x)
{
    Node<T> *p=first->next;
    int all=1;
    while(p)
    {
        if(p->data==x)
        {
            return all;
        }
        else
        {
            p=p->next;
            all++;
        }
    }
    return 0;
}
插入新数据
template<class T>
void Linklist<T>::insertnew(T x, int i)
{
    Node<T> *p=first,*s=NULL;
    int all=0;
    while(p&&all<i-1)
    {
        p=p->next;
        all++;
    }
    if(p==NULL)
    {
        cout<<"error";
    }
    else
    {
        s=new Node<T>;
        s->data=x;
        s->next=p->next;
        p->next=s;
    }
}
删除数据
template<class T>
void Linklist<T>::Deletes(int i)
{
    Node<T> *p=first,*q=NULL;
    int all=0;
    while(p&&all<i-1)
    {
        p=p->next;
        all++;
    }
    if(p==NULL||p->next==NULL)
    {
        cout<<"error";
    }
    else
    {
        q=p->next;
        p->next=q->next;
        delete q;
    }
}
判断是否为空
template<class T>
int Linklist<T>::emptys()
{
    Node<T> *p=first->next;
    if(p)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
遍历链表
template<class T>
void Linklist<T>::bianli()
{
    Node<T> *p=first->next;
    while(p)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值