zoukankan      html  css  js  c++  java
  • Static List

    Static List
    Static List is the smart implementation of list data structure for those languages that have no pointer or similar function, such as Pascal, Basic. It uses an auxilary array to store the locations of data in the main array, which is known as cursor,the supplicant of pointer.

    Static List data structure
    #define LIST_INIT_SIZE 100

    typedef struct
    {
    ElemType data;
    int cur; /*cursor,mean no pointing if 0.*/
    }Component,StaticLinkList[LIST_INIT_SIZE];
    How static list is organized:
    1.Index 0 is reserved and stores no data.
    2.Cursor 0 points to first empty index.
    3.Last Cursor points to index first data element.
    4.Each cursor points to index of its next element.

    Operations:
    /*space[0].cur is head pointer,
    "0" indicates null pointer.*/
    Status InitList(StaticLinkList space)
    {
    int i;
    for(i=0;i<LIST_INIT_SIZE-1;i++)
    {
    space[i].cur=i+1;
    }
    /*current static linked list is empty,
    cur of last element is 0.*/
    space[LIST_INIT_SIZE-1].cur=0;
    return OK;
    }

    /*return the cur of allocated node if the linked list is not
    empty, otherwise return 0.*/
    int AllocSLL(StaticLinkList space)
    {
    int i=space[0].cur;
    if(space[0].cur)
    {
    space[0].cur=space[i].cur;
    }
    return i;
    }

    /*insert a new element e before i element.*/
    Status ListInsert(StaticLinkList L,int i,ElemType e)
    {
    int j,k,l;
    /*k set as cur of last element.*/
    k=LIST_INIT_SIZE-1;
    if(i<1||i>ListLength(L)+1)
    {
    return ERROR;
    }

    /*obtain cur of the allocated node.*/
    j=AllocSSL(L);
    if(j)
    {
    L[j].data=e;
    for(l=1;l<=i-1;l++)
    {
    L[j].cur=L[k].cur;
    }
    L[k].cur=j;
    return OK;
    }
    return ERROR;
    }

    void Free_SSL(StaticLinkList space,int k)
    {
    space[k].cur=space[0].cur;
    space[0].cur=k;
    }

    Status ListDelete(StaticLinkList L,int i)
    {
    int j,k;
    if(i<1||i>ListLength(L))
    {
    return ERROR;
    }
    k=MAXSIZE-1;
    for(j=1;j<=i-1;j++)
    {
    k=L[k].cur;
    }
    j=L[k].cur;
    L[k].cur=L[j].cur;
    Free_SSL(L,j);
    return OK;
    }

    int ListLength(StaticLinkList L)
    {
    int j=0;
    int i=L[MAXSIZE-1].cur;
    while(i)
    {
    i=L[i].cur;
    j++;
    }
    return j;
    }

  • 相关阅读:
    android 属性动画
    android EventBus
    android gson使用
    Date、String、Calendar类型之间的转化
    2020-08-26:裸写算法:树的非递归先序遍历。
    2020-08-25:BloomFilter的原理以及Zset的实现原理。
    2020-08-24:什么是小文件?很多小文件会有什么问题?很多小文件怎么解决?(大数据)
    2020-08-23:描述HTTPS和HTTP的区别。
    2020-08-22:I/O多路复用中select/poll/epoll的区别?
    2020-08-21:网络IO模型有哪些?
  • 原文地址:https://www.cnblogs.com/askDing/p/5978401.html
Copyright © 2011-2022 走看看