线性表(数据结构)

线性表就是n个具有相同类型的数据元素的有限序列。线性表中的元素个数成为线性表的长度,长度等于零的时候称为空表。当我们初始化的时候一般初始化为空表直接把表的长度赋为0;

 

顺序存储(数组存储) 

#include <bits/stdc++.h>
using namespace std;
const int Maxx=1000;
template <class t>
class seqlist
{
private:
    t data[Maxx];
    int length;
public:
    seqlist();
    seqlist(t *a,int n);
    ~seqlist();
    int Lenth();
    int locate(t i);
    t getint(int i);
    void inesrt(int i,t x);
    void deletee(int i);
    int emptyy();
    void printft();
};
template <class t>
seqlist<t>::seqlist()
{
    length=0;
}
template <class t>
seqlist<t>::seqlist(t *a,int n)
{
    length=0;
    if(n<=Maxx)
        for(int i=0; i<n; i++)
        {
            data[i]=a[i];
            length++;
        }
}
template <class t>
seqlist<t>::~seqlist() {}

template <class t>
int seqlist<t>::Lenth()
{
    return length;
}

template <class t>
int seqlist<t>::locate(t i)
{
    for(int j=0; j<length; j++)
    {
        if(data[j]==i)
            return j;
    }
    return 0;
}

template <class t>
t seqlist<t>::getint(int i)
{
    if(i>=0&&i<=length)
        return data[i-1];
}

template <class t>
void seqlist<t>::inesrt(int i,t x)
{
    if(length!=Maxx&&i>0&&i<=length+1)
    {

        for(int j=length; j>=i; j--)
            data[j]=data[j-1];
        data[i-1]=x;
        length++;
    }
}

template <class t>
void seqlist<t>::deletee(int i)
{
    if(i>0&&i<=length)
    {
        for(int j=i-1; j<length-1; j++)
            data[j]=data[j+1];
        length--;
    }
}

template <class t>
int seqlist<t>::emptyy()
{
    if(length==0)
        return 0;
    return 1;
}

template <class t>
void seqlist<t>::printft()
{
    for(int i=0; i<length; i++)
        cout<<data[i]<<' ';
    cout<<endl;
}
int main()
{
    int n,b[100];
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>b[i];
    seqlist<int> s(b,n);
    s.printft();
  //  cout<<s.getint(3);
    s.inesrt(4,13);

    s.printft();
    s.deletee(4);
    s.printft();
}

链式存储

由于是动态存储内存也是动态申请的 所以需要一个指针来指向下一个位置并且需要一个头指针记录整个链表的开始位置,创建的方法有带头结点和不带头结点的方法。因为不带头结点的操作方法有些情况需要特殊处理,所以我们常用带头结点的,链式存储和顺序存储的差别链式存储的插入是O(1)的而顺序表是O(n)的,查找位置的复杂度正好相反,但是这两种存储方式再增删数据的时候都是O(n)的但这并不代表这两种存储方式的增删方式就是一样的,链式存储的数据再增删的时候找数据需要遍历,而顺序存储的时候找很快但是在插入删除的时候需要把在这个位置之后所有的数据全部后移这就导致O(n)。

#include<bits/stdc++.h>
using namespace std;
int  a[30000000];
struct Boe
{
    int x;
    Boe *next;
};
struct danlink
{
    Boe *first;
    danlink(){
        first=new Boe;
        first->next=NULL;
    }
    danlink(int *a,int n){
        first=new Boe;
        first->next=NULL;
        Boe *last=first;
        for(int i=1;i<=n;i++){
            Boe *y=new Boe;
            y->x=a[i];
            last->next=y;
            last=y;
        }
        last->next=NULL;
    }
    danlink(int *a,int n,int o){
        first=new Boe;
        first->next=NULL;
        for(int i=1;i<=n;i++){
            Boe *x=new Boe;
            x->x=a[i];
            x->next=first->next;
            first->next=x;
        }
    }
    ~danlink(){
        for(Boe *i=first->next;i;)
        {
            Boe *x=i;
            i=i->next;
            delete x;
        }
        delete first;
    }
    void print()
    {
        for(Boe *i=first->next;i;i=i->next)
            cout<<i->x<<' ';
        cout<<endl;
        return;
    }
    bool place(int w){
        int flag=0;
        Boe *x=first;
        for(Boe *i=first->next;i;x=i,i=i->next)
        {
            flag++;
            if(flag==w)
            {
                x->next=i->next;
                delete i;
                return 1;
            }
        }
        return 0;
    }
    bool valuee(int v){
        Boe *x=first;
        for(Boe *i=first->next;i;x=i,i=i->next)
        {
            if(i->x==v)
            {
                x->next=i->next;
                delete i;
                return 1;
            }
        }
        return 0;
    }
};

 循环链表

循环链表就是把头部和尾部连接起来其他的操作和正常链表无异。数组的静态实现也是同理。

双向链表

双向链表就是在单链表的基础上又加了一个指针来表示前继是什么需要注意的就是再插入的时候的指针更改的顺序不能把原来的值覆盖(覆盖过早导致后面的指针赋值出现错误)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值