链表

单链表

使用数组存储当前位置的数字以及指向的下一个位置 e[N],ne[N]
共有三种操作:

插入数字

void add(int x,int y){
    e[idx]=y;
    ne[idx]=ne[x];
    ne[x]=idx;
    idx++;
}

删去数字

void remove(int x){
    if(x==0){
        head=ne[head];
    }
    else{
        ne[x]=ne[ne[x]];
    }
}

在头部插入数字

void ahead(int x){
    e[idx]=x;
    ne[idx]=head;//原来的头节点变为现在头节点的下一位
    head=idx;//idx变为头节点
    idx++;
}

输出链表

for(int i=head;i!=-1;i=ne[i]){
        cout<<e[i]<<" ";
    }
posted @ 2022-07-20 11:47  zyzzzzlh  阅读(39)  评论(1)    收藏  举报