線性表中順序表的實現和單鏈表的實現

2021-10-06 07:23:31 字數 3944 閱讀 6559

線性表

線性表儲存結構

// 基類介面

public

inte***ce

list

// 簡單實現 順序表

public

class

arraylist

implements

list

// 陣列初始長度

public

arraylist

(int initialcapacity)

@override

public

intsize()

@override

public object get

(int i)

return elementdata[i];}

@override

public

boolean

isempty()

@override

public

boolean

contains

(object e)

@override

public

intindexof

(object e)}}

return-1

;}@override

public

void

add(

int i, object e)

if(size == elementdata.length)

for(

int j = size; j > i; j--

) elementdata[i]

= e;

size++;}

@override

public

void

add(object e)

private

void

grow()

@override

public

boolean

addbefore

(object obj, object e)

@override

public

boolean

addafter

(object obj, object e)

@override

public object remove

(int i)

@override

public

boolean

remove

(object e)

@override

public object replace

(int i, object e)

@override

public string tostring()

stringbuilder bw =

newstringbuilder

("[");

for(

int i =

0; i < size; i++

)else

} bw.

("]");

return bw.

tostring()

;}}

// 自定義異常

public

class

myarrayindexoutofbound***ception

extends

runtimeexception

public

myarrayindexoutofbound***ception

(string msg)

}

// 測試

public

static

void

main

(string[

] args)

單鏈表

public

class

node

public

node()

public

node

(object data, node next)

public object getdata()

public

void

setdata

(object data)

public node getnext()

public

void

setnext

(node next)

}

public

class

singlelinkedlist

implements

list

@override

public object get

(int i)

return p.data;

}@override

public

boolean

isempty()

@override

public

boolean

contains

(object e)

@override

public

intindexof

(object e)

@override

public

void

add(

int i, object e)

//找到前乙個結點

node p = head;

for(

int j =

0; j < i; j++

)// 新建立乙個結點

node newnode =

newnode

(e);

// 指明性結點直接後繼結點

newnode.next = p.next;

p.next = newnode;

size++;}

@override

public

void

add(object e)

@override

public

boolean

addbefore

(object obj, object e)

@override

public

boolean

addafter

(object obj, object e)

@override

public object remove

(int i)

@override

public

boolean

remove

(object e)

@override

public object replace

(int i, object e)

@override

public string tostring()

stringbuilder bw =

newstringbuilder

("[");

node p = head.next;

for(

int i =

0; i < size; i++

)else

p = p.next;

} bw.

("]");

return bw.

tostring()

;}}

public

static

void

main

(string[

] args)

轉 線性表(順序表和單鏈表)

大神 權侵刪 一 定義 是n個型別相同的資料元素的有限序列。線性表元素的個數n n 0 定義為線性表的長度,當n 0時,稱為空表。在較複雜的線性表中,乙個資料元素可以由若干個資料項組成。三 線性表的順序儲存結構 1 順序儲存定義 線性表的順序儲存結構,指的是用一段位址連續的儲存單元依次儲存線性表的資...

線性表逆置(順序表和單鏈表)

這幾天練習了下線性表的逆置,順序表比較簡單,就是陣列的位置交換。順序表的逆置 單鏈表的逆置 頭插法就地逆置 遞迴順序表的逆置 中間變數temp,交換首位兩個元素的位置。void listreverse sqlist l sqlist status listinit sqlist l status l...

線性表 順序表與單鏈表(模板)

順序表 線性表的順序儲存結構,靜態儲存分配,通常用一位陣列來實現 include using namespace std const int maxsize 100 template typename t class seqlist template typename t seqlist seqli...