順序表和煉表基本操作

2021-10-06 12:17:31 字數 3525 閱讀 3457

1.列印

public

void

display()

} result+=

"]";

system.out.

println

(result)

;}

2.插入元素

public

void

add(

int pos,

int data)

//容量不夠就擴容

if(size>=datas.length)

datas=newdatas;

}//在尾部插入

if(pos==size)

//在其他位置插入

for(

int i=size-

1;i>=pos;i--

) datas[pos]

=data;

size++

;}

3.判度是否包含某個元素

public

boolean

contains

(int tofind)

}return

false

;}

4.查詢指定元素

public

intsearch

(int tofind)

}return-1

;}

5 根據下標查詢(設定)元素

public integer getpos

(int pos)

return datas[pos];}

public

void

setpos

(int pos,

int data)

6.刪除指定元素

public

void

remove

(int toremove)

//如果在末尾就直接刪除

if(pos==size-1)

//如果在中間,先刪除在搬運

for(

int i=pos;i1;i++

) size--

;}

7.清空元素

public

void

clear()

1.頭插

public

void

addfirst

(int data)

//3.如果不是空鍊錶

node.next = head;

head = node;

}

2.尾插

public

void

addlast

(int data)

//3.如果不是空鍊錶,就先要找到最後乙個節點

node tail = head;

while

(tail.next != null)

//迴圈結束之後,就找到對應的節點了

tail.next = node;

}

3.列印

public

void

display()

system.out.

println()

;}

4.求長度

public

intgetsize()

return size;

}

5.指定位置插入

public

boolean

addindex

(int index,

int data)

//2.如果index=0,相當於頭插

if(index ==0)

//3.如果index=size,相當於尾插

if(index == size)

node node=

newnode

(data)

;//4.如果index處於中間位置

//1)先找到index的前乙個元素

node prev =

getpos

(index-1)

; node.next=prev.next;

prev.next=node;

return

true

;}

6.得到指定下標的值

public node getpos

(int index)

return cur;

}

7.判斷是否包含

public

boolean

contains

(int tofind)

}return

false

;}

8.刪除指定元

public

void

remove

(int toremove)

//2.不是頭節點,首先要找到刪除元素的前乙個元素

node prev=

searchprev

(toremove)

;//3.改變引用

// prev.next=prev.next.next;

node todelete=prev.next;

prev.next=todelete.next;

}

9.查詢元素

public node searchprev

(int tofind)

}return null;

}

10.刪除指定元素

public

void

removeall

(int toremove)

else

}//2.如果要刪除的是頭節點

if(head.data==toremove)

}

11.清空鍊錶

public

void

clear()

順序鍊錶的基本操作

標頭檔案 ifndef commonheader h define commonheader h include include include include include include include include include include include include inclu...

順序表和煉表的基本操作(Java實現)

線性表 頭插 尾插 中間插入 頭刪 尾刪 1.順序表 想細節,寫偽 難點 迴圈邊界 2.鍊錶 結點和結點之間的邏輯關係 邏輯上有前後關係,物理儲存也是前後相連的。頭插 思考 偽 1.需要後移size個元素 2.為避免資料被覆蓋,從後往前移 3.空間 size,1 資料 size 1,0 4.arra...

資料結構 順序表 鍊錶 基本操作

ifndef slist h define slist h include include include typedef int sltdatatype typedef struct slistnode slistnode typedef struct slist slist void slist...