北大青鳥北京學校講解:在線性表接口的實現

      在線性表接口的實現_Java中,我們實現了線性表的接口,今天北大青鳥北京學校老師講解實現線性表的順序存儲結構——順序表類。

      北大青鳥北京學校老師首先說明了順序表的定義:

      線性表的順序存儲是用一組連續的內存單元依次存放線性表的數據元素,元素在內存的物理存儲次序與它們在線性表中的邏輯次序相同,即元素ai與其直接前驅ai-1及直接后繼ai+1的存儲位置相鄰。順序存儲的線性表也成為順序表(sequential list)。

      順序表類SeqList提供線性表基于順序存儲結構的一種實現,它有兩個私有成員變量table和n,table是一個存放元素的對象數組;n為線性表長度,n≤table.length。SeqList聲明如下,它實現了線性表的接口LList。

      1.       package dataStructure.linearList;  

      2.       import dataStructure.linearList.LList;  

      3.         

      4.       public class SeqList<E> implements LList<E>                 //順序表類,實現線性表接口   

      5.       {  

      6.           private Object[] table;                                 //對象數組,私有成員   

      7.           private int n;                                          //順序表長度   

      8.             

      9.           public SeqList(int capacity)                            //構造方法,創建置頂容量的空表   

      10.    {  

      11.        this.table = new Object[Math.abs(capacity)];  

      12.        this.n = 0;  

      13.    }  

      14.      

      15.    public SeqList()                                        //指定空表的默認容量   

      16.    {  

      17.        this(16);  (北大青鳥北京)

      18.    }  

      19.      

      20.    public boolean isEmpty()                                //判斷順序表是否為空,若空返回true   

      21.    {  

      22.        return this.n == 0;  

      23.    }  

      24.      

      25.    public int length()                                     //返回順序表長度   

      26.    {  

      27.        return this.n;  

      28.    }  

      29.      

      30.    public E get(int index)                                 //返回index(初值為0)位置的對象,若序號無效,返回null   

      31.    {  

      32.        if(index>=0 && index < this.n)   (北大青鳥北京)

      33.        {  

      34.            return (E)this.table[index];  

      35.        }  

      36.        return null;  

      37.    }  

      38.      

      39.    public E set(int index,E element)                       //設置index位置的對象為element,若操作成功,放回原對象,否則返回null    (北大青鳥北京

      40.    {  

      41.        if(index >= 0 && index < this.n && element != null)  

      42.        {  

      43.            E old =(E)this.table[index];  

      44.            this.table[index] = element;  

      45.            return old;  

      46.        }  

      47.        return null;  

      48.    }  

      49.      

      50.    public boolean add(int index,E element)                 //在index位置插入element對象,若操作成功返回true,不能插入null   

      51.    {  

      52.        if(element == null)                                 //不能插入null   

      53.        {  

      54.            return false;  

      55.        }  

      56.        if(this.n == table.length)                          //若數組滿,則需要擴充順序表容量   

      57.        {  

      58.            Object[] temp = this.table;  

      59.            this.table = new Object[temp.length*2];         //重新申請一個容量更大的數組   

      60.            for(int i = 0;i < temp.length;i++)  

      61.            {  

      62.                this.table[i] = temp[i];  

      63.            }   (北大青鳥北京)

      64.        }  

      65.          

      66.        if(index < 0)                                        //下標容錯   

      67.        {  

      68.            index = 0;  

      69.        }  

      70.        if(index > this.n)  

      71.        {  

      72.            index =this.n;  

      73.        }  

      74.        for(int j = this.n-1;j >= index;j--)             //元素后移,平均移動n/2   

      75.        {  

      76.            this.table[j+1] = this.table[j];  

      77.        }  

      78.        this.table[index] = element;  

      79.        this.n++;   (北大青鳥北京

      80.        return true;  

      81.    }  

      82.      

      83.    public boolean add(E element)                           //在順序表最后插入element對象   

      84.    {  

      85.        return add(this.n,element);  

      86.    }  

      87.      

      88.    public E remove(int index)                              //移去index位置的對象,若操作成功,則返回被移去的對象,否者返回null   

      89.    {  

      90.        if(this.n != 0 && index >= 0 && index < this.n)  

      91.        {  

      92.            E old = (E)this.table[index];  

      93.            for(int j = index;j < this.n-1;j++)              //元素前移,平均移動n/2   

      94.            {  

      95.                this.table[j] = this.table[j + 1];  

      96.            }  

      97.            this.table[this.n - 1] = null;  

      98.            this.n--;  

      99.            return old;  

      100.        }  

      101.        return null;  

      102.    }   (北大青鳥北京)

      103.      

      104.    public void clear()                                     //清空順序表   

      105.    {  

      106.        if(this.n != 0)  

      107.        {  

      108.            for(int i = 0;i < this.n;i++)  

      109.            {  

      110.                this.table[i] = null;  

      111.            }  

      112.            this.n=0;  

      113.        }  

      114.    }  

      115.    public String toString()                                //返回顯示所有元素的字符串,形式為(,)   

      116.    {  

      117.        String str = "(";  

      118.        if(this.n != 0)  

      119.        {  

      120.            for(int i = 0;i < this.n - 1;i++)  

      121.            {  

      122.                str += this.table[i].toString()+",";  

      123.            }  

      124.            str += this.table[this.n - 1].toString();  

      125.        }  

      126.        return str + ")";  

      127.    }  

      128.}  

      package dataStructure.linearList;

      import dataStructure.linearList.LList;

       

      public class SeqList<E> implements LList<E>                                   //順序表類,實現線性表接口

      {

             private Object[] table;                                                               //對象數組,私有成員

             private int n;                                                                             //順序表長度

            

             public SeqList(int capacity)                                                 //構造方法,創建置頂容量的空表

             {

                    this.table = new Object[Math.abs(capacity)];

                    this.n = 0;

             }

            

             public SeqList()                                                                      //指定空表的默認容量

             {

                    this(16);

             }

            

             public boolean isEmpty()                                                        //判斷順序表是否為空,若空返回true

             {

                    return this.n == 0;

             }

            

             public int length()                                                                      //返回順序表長度

             {

                    return this.n;

             }

            

             public E get(int index)                                                               //返回index(初值為0)位置的對象,若序號無效,返回null

             {

                    if(index>=0 && index < this.n)

                    {

                           return (E)this.table[index];

                    }

                    return null;

             }

            

             public E set(int index,E element)                                          //設置index位置的對象為element,若操作成功,放回原對象,否則返回null

             {

                    if(index >= 0 && index < this.n && element != null)

                    {

                           E old =(E)this.table[index];

                           this.table[index] = element;

                           return old;

                    }

                    return null;

             }

            

             public boolean add(int index,E element)                                   //在index位置插入element對象,若操作成功返回true,不能插入null

             {

                    if(element == null)                                                               //不能插入null

                    {

                           return false;

                    }

                    if(this.n == table.length)                                                 //若數組滿,則需要擴充順序表容量

                    {

                           Object[] temp = this.table;

                           this.table = new Object[temp.length*2];                     //重新申請一個容量更大的數組

                           for(int i = 0;i < temp.length;i++)

                           {

                                  this.table[i] = temp[i];

                           }

                    }

                   

                    if(index < 0)                                                                      //下標容錯

                    {

                           index = 0;

                    }

                    if(index > this.n)

                    {

                           index =this.n;

                    }

                    for(int j = this.n-1;j >= index;j--)                            //元素后移,平均移動n/2

                    {

                           this.table[j+1] = this.table[j];

                    }

                    this.table[index] = element;

                    this.n++;

                    return true;

             }

            

             public boolean add(E element)                                                 //在順序表最后插入element對象

             {

                    return add(this.n,element);

             }

            

             public E remove(int index)                                                        //移去index位置的對象,若操作成功,則返回被移去的對象,否者返回null

             {

                    if(this.n != 0 && index >= 0 && index < this.n)

                    {

                           E old = (E)this.table[index];

                           for(int j = index;j < this.n-1;j++)                            //元素前移,平均移動n/2

                           {

                                  this.table[j] = this.table[j + 1];

                           }

                           this.table[this.n - 1] = null;

                           this.n--;

                           return old;

                    }

                    return null;

             }

            

             public void clear()                                                                      //清空順序表

             {

                    if(this.n != 0)

                    {

                           for(int i = 0;i < this.n;i++)

                           {

                                  this.table[i] = null;

                           }

                           this.n=0;

                    }

             }

             public String toString()                                                        //返回顯示所有元素的字符串,形式為(,)

             {

                    String str = "(";

                    if(this.n != 0)

                    {

                           for(int i = 0;i < this.n - 1;i++)

                           {

                                  str += this.table[i].toString()+",";

                           }

                           str += this.table[this.n - 1].toString();

                    }

                    return str + ")";(北大青鳥北京)

             }

      }


      順序表是一種隨即存取結構,存取任何一個元素的get()、set()方法的時間復雜度是O(1)。 (北大青鳥北京)

       

      對順序表進行插入或刪除操作是,算法所花費的時間主要用于移動元素。在等概率情況下,插入一個元素平均需要移動一半的元素,時間復雜度為O(n)。同里,刪除一個元素的時間復雜度亦為O(n)。(北大青鳥北京)

      綜上所述,順序表具有下列特點:

      ①:元素的物理存儲順序直接反映表中元素的邏輯順序,可以隨機存取元素。

      ②:插入和刪除的操作效率很低。每插入或刪除一個元素,可能需要移動大量元素,其平均移動次數是順序表長度的一半。再者,數組容量不可更改,存在因容量小造成數據溢出,或因容量過大造成內存資源浪費的問題。解決數據溢出的方法是,申請另一個更大容量的數組,并進行數組元素復制,但插入操作效率很低。

      北大青鳥北京

      北大青鳥網上報名
      北大青鳥招生簡章
      主站蜘蛛池模板: 亚洲国产日韩在线一区| 一区二区三区免费在线观看| 国产福利微拍精品一区二区 | 国产精品一区二区久久沈樵| 亚洲一区精彩视频| 人妻精品无码一区二区三区| 天堂成人一区二区三区| 无码人妻一区二区三区在线水卜樱| 视频一区在线播放| 一区二区三区人妻无码| 亚洲视频一区网站| 无码人妻精一区二区三区| 国产在线不卡一区| 日本大香伊一区二区三区| 熟妇人妻AV无码一区二区三区| 最新欧美精品一区二区三区| 日韩美女在线观看一区| 国产av天堂一区二区三区 | 中文字幕AV一区中文字幕天堂| 亚洲AV无码一区二区二三区软件| 亚洲一区二区三区日本久久九| 无码中文字幕人妻在线一区二区三区 | 97一区二区三区四区久久| 国产精品亚洲一区二区三区在线观看| 一区二区三区在线播放| 色久综合网精品一区二区| 在线观看日韩一区| 无码国产精品一区二区免费模式 | 日韩在线一区二区| 一区二区三区视频在线播放| 色婷婷一区二区三区四区成人网 | 亚洲无线码在线一区观看| 中日韩精品无码一区二区三区| 亚洲人成网站18禁止一区| 色偷偷一区二区无码视频| 一区二区三区四区电影视频在线观看 | 精品理论片一区二区三区| 国产a∨精品一区二区三区不卡| 日韩精品中文字幕视频一区| 无码一区二区三区免费| 亚洲第一区在线观看|