線性表是一種可以在任意位置插入和刪除元素,由n個同型別元素組成的線性結構。主要包括順序表,單鏈表,迴圈單鏈表,雙向鍊錶和**鍊錶。應用比較廣泛的是順序表和單鏈表。
下面是線性表的介面,主要操作包括插入元素,刪除元素,取得元素,得到線性表元素個數,判斷線性表是否為空。
package com.nishizhen.list;
public inte***ce list
順序表:
順序表插入乙個元素需要移動元素的平均次數為n/2次,刪除乙個元素需要移動元素次數為(n-1)/2,所以順序表的時間複雜度為o(n)。
順序表的實現如下:
package com.nishizhen.list;
public class seqlist implements list
public seqlist()
public void initiate(int sz)
public void insert(int i,object obj)throws exception
if(i<0 || i>maxsize)
else
listarray[i] = obj;
size++;}}
public void delete(int i)throws exception
if(i<0 || i>=size)
else
listarray[listarray.length-1] = "";
size--;}}
public object getdata(int i)throws exception
if(1<0 || i>=size)
else
}public int size()
public boolean isempty()
return flag;
}}
單鏈表:
指標是指乙個資料元素邏輯意義上的儲存位置,鏈式儲存機構是基於指標實現的,每乙個節點由乙個資料元素和乙個指標構成。鏈式儲存結構是用指標把相互關聯的元素鏈結起來。
在單鏈表中,每個節點只有乙個直接只想後繼元素的指標,而雙向鍊錶中每個節點有兩個指標,乙個只想後繼節點乙個只想前驅節點。
單鏈表的實現
節點類:
package com.nishizhen.list;
public class node
node(object obj,node nextval)
public node getnext()
public void setnext(node nextval)
public object getelement()
public void setelement(object obj)
public string tostring()
}單鏈錶類:
package com.nishizhen.list;
public class linlist implements list
public void index(int i) throws exception
if(i==-1)
current = head.next;
int j = 0;
while((current !=null)&&j=size)
index(i-1);
current.setnext(new node(obj,current.next));
size++;
}public void delete(int i)throws exception
if(1<0 || i>=size)
index(i-1);
object obj = current.next.getelement();
current.setnext(current.next.next);
size--;
}public object getdata(int i)throws exception
index(i);
return current.getelement();
}public int size()
public boolean isempty()
}
JAVA實現 線性表
如果乙個資料元素序列滿足 除第乙個和最後乙個資料元素外,每個資料元素都只有乙個前驅資料元素和乙個後繼資料元素 第乙個資料元素沒有前驅資料元素 最後乙個資料元素沒有後繼資料元素 則稱這樣的資料結構為線性結構。線性表 堆疊 佇列 串和陣列都屬於線性結構。線性表是一種可以在任意位置進行插入和刪除資料元素操...
線性表實現(JAVA)
package nodelist public class liststructs 判斷線性表是否為空 return 0為空,1為非空 public int listempty return 0 把陣列中的資料清空 return 0為清空 public int clearlist return 0 ...
ArrayList線性表Java實現
自定義list介面 classname list description 列表的介面 author xiaomu date 2018年1月14日 下午4 25 05 param public inte ce listarraylist classname arraylist description ...