zoukankan      html  css  js  c++  java
  • List-----Array

    1、Definition

    Arry数组是一种连续储存的List

    储存方式:将线性表中的元素一次储存在连续的储存空间中。

    Computer's logical structure:
    逻辑位置上相邻的元素在物理位置上也相邻
    

    2、Implementation

    template<class List_entry>
    class List
    {
      public:
         List();
         int size();
         bool full();
         bool empty();
         ...
     protected:
         int last;
         List_entry entry[max_list];
    };
    

    3、Operations

    (1)Insert element e at i

    InsertElem(L, e, i)
       input:     i:location, e: ElementType;
       Pre-condition: 1<=i<=L.last+1;
       Pos-condition: insert element e at i and add 1 to last;
    -----------------------------------------------------------------------------------------
    
    int InsertElem(List *L, DataType e, int i)
    {
       if(((*L).last)>=(maxsize-1))
         {
            cout<<"overflow"<<endl;
            return ERROR;     //溢出
          }
      else if((i<0)||(i>((*L).last+1)))
         {
            cout<<"ERROR"<<endl;
            return ERROR;    //非法位置
          }
     else
         {
           for(int j=((*L).last);j>=i;j--)
                 (*L).data[j+1]=(*j).data[j];
           (*L).data[i]=e;
           (*L).last+=1; 
          return(1);
         }
    }
    

    (2)Delete element e at location i

    DeleteElem(i)
        input:    i: location
        Pre-condition: 0<=i<=last;
        Pos-condition: delete the element at location i and last-1;
    ---------------------------------------------------------------------------------------------
    int Delete(List *L, int i)
    {
      if(((*L).last)>=(maxsize-1))
       {
          cout<<"overflow"<<endl;
          return ERROR;    //溢出
       }
     else if((i<0)||(i>((*L).last+1)))
      {
          cout<<"ERROR"<<endl;
          return ERROR;    //非法位置
       }
     else
       {
          for(int j=i;j<((*L).last);j++)
            (*L).data[j]=(*L).data[j+1];
         (*L).last-=1;
         return (1);
        }
    }
    

    Insert and delete

    delete:

      最好情况为删除最后一个元素,其他元素不用移动,时间复杂度为O(1)

      最坏情况为删除第一个元素,要移动所有元素,时间复杂度为O(N)

    insert:

      最好情况为在最后一个位置插入,其他元素不用移动,时间复杂度为O(1)

     最坏情况为在第一个位置插入,要移动所有元素,时间复杂度为O(N) 

    (3)Search

     按值查找

    Search(e, i)
       input: e:ElementType;
       output: i:location;
       Pre-condition: L is not empty;
       Pos-condition: output the location of e;
    -----------------------------------------------------------------------
    int Search(List *L, DataType e)
    {
      for(int i=0;i<=((*L).last);i++)
        if(((*L).data[i])==e)
            return i;
      return 0;
    } 
    

    按位查找

    Search(i)
      input: i:location;
      output: e:ElementType;
      Pre-condition: 0<=i<=last;
      Pos-condition: output the element in the location i;
    ----------------------------------------------------------------------------------------
    DataType Search(List *L, int i)
    {
       if((i<0)||(i>(*L).last+1))
            { 
                  cout<<"ERROR"<<endl;
                  return NULL;
            }
       return (*L).data[i];
    }
    

    4、Analysis of Array

    Advantages:

       顺序表的结构比较简单,储存效率高,无需储存元素之间的关系

    Disadvantages:

     在进行插入和删除操作时时间复杂度高

    对长度较大的线性表,须预先分配较大的的空间或经常扩充线性表,不方便。

  • 相关阅读:
    HashMap底层实现原理(JDK1.8)源码分析
    JVM总结
    初识Nosql
    线程池总结
    Linux中常用操作命令
    JAVA—集合框架
    TeKtronix TDS210数字示波器使用简介
    硬盘的物理结构
    Unicode 与 UTF
    I2C串行总线
  • 原文地址:https://www.cnblogs.com/KennyRom/p/5879276.html
Copyright © 2011-2022 走看看