zoukankan      html  css  js  c++  java
  • 链表(数组模拟)

    12.链表

    1)(单链表)

    #include<stdio.h>

    const int nill=-1;

    const int head=0;

    struct my{

        int next;

        int zhi;

    };

    struct mylist{

        my a[100000];

        int n;

        int getpos(int i){

            int td=head;

            for (int j=1;j<=i;j++)

                td=a[td].next;

            return td;

        }

        void insert(int pos,int val){

          int p=getpos(pos-1);

          a[++n].zhi=val;

          a[n].next=a[p].next;

          a[p].next=n;

        }

        void det(int pos){

           int p=getpos(pos-1);

           a[p].next=a[a[p].next].next;

        }

    }tu;

    int main(){

        int m;

     

        scanf("%d",&tu.n);

        for (int i=1;i<=tu.n;i++)

            {

                scanf("%d",&tu.a[i].zhi);

                tu.a[i-1].next=i;

            }

            tu.a[tu.n].next=nill;

            scanf("%d",&m);

            int help;

            int v,sum;

        for (int i=1;i<=m;i++){

            scanf("%d%d",&help,&v);

            if(help==2) {

                    scanf("%d",&sum);

            tu.insert(v,sum);

            }

            if(help==3){

                tu.det(v);

            }

            if(help==1){

                printf("%d ",tu.a[tu.getpos(v)].zhi);

            }

        }

    return 0;

    }

    2)双向链表

    const int nill=-1;

    const int head=0;

    struct my{

        int next;

        int zhi;

        int prve;

    };

    struct mylist{

        my a[100000];

        int n;

        int getpos(int i){

            int td=head;

            for (int j=1;j<=i;j++)

                td=a[td].next;

            return td;

        }

        void insert(int pos,int val){

          int p=getpos(pos-1);

          a[++n].zhi=val;

          a[n].next=a[p].next;

          a[p].next=n;

          a[a[n].next].prve=n;

          a[n].prve=p;

        }

        void det(int pos){

           int p=getpos(pos-1);

            a[p].next=a[a[p].next].next;

            a[a[p].next].prve=p;

        }

    }tu;

  • 相关阅读:
    0725------Linux基础----------信号
    0723------Linux基础----------文件 IO 之 dup、dup2 和 fcntl 函数
    0722-----C++Primer听课笔记----------句柄类和智能指针
    0721-----C++Primer听课笔记----------继承
    0718-----C++Primer听课笔记----------运算符重载
    flex 弹性盒子模型一些案例.html
    布局—column(属性)
    css3中的动画处理
    CSS3中的变形处理(transform)属性
    jquery返回顶部特效
  • 原文地址:https://www.cnblogs.com/lmjer/p/7582872.html
Copyright © 2011-2022 走看看