資料結構 順序表的基本操作

2021-10-01 05:08:22 字數 2733 閱讀 7797

在了解順序表之前先了解線性表,順序表是線性表的一種。

線性表

線性表(linear list):是n個具有相同特性的資料元素的有限序列。

線性表是一種在實際中廣泛使用的資料結構,常見的線性表:順序表、鍊錶、棧、佇列、字串等

順序表靜態順序表:使用定長陣列儲存。

動態順序表:使用動態開闢的陣列儲存。

基本操作:

**實現

// 順序表的元素型別 int

public

class

myarraylist

//1.增(重點)

//頭插

public

void

pushfront

(int element)

array[0]

= element;

size++;}

尾插 時間複雜度平均 o(1)

public

void

pushback

(int element)

在指定下標位置插入資料

public

void

insert

(int index,

int element)

ensurecapacity()

;for

(int i = size -

1; i >= index; i--

) array[index]

= element;

size++;}

//2.刪(重點)

頭刪public

void

popfront()

for(

int i =

0; i < size -

1; i++)

array[

--size]=0

;}尾刪

public

void

popback()

array[

--size]=0

;}刪除指定下標位置的元素

public

void

earse

(int index)

if(index <

0|| index >= size)

for(

int i = index +

1; i < size; i++)

array[

--size]=0

;}.1刪除指定元素,如果出現多次,刪除第一次出現的

public

void

remove

(int element)

}.2刪除順序表中所有重複的元素

public

void

removeall

(int element)

*//* int newarray = new int[array.length];

int j = 0;

for (int i = 0; i < size; i++)

}array = newarray;

size = j;

*///思路:跳過要刪除的元素,不是要刪除的元素往前移,

//最後的據為不是重複的元素,j記錄順序表的已有資料個數返回即可;

int j =0;

for(

int i =

0; i < size; i++)}

size = j;

}//3.查

查詢指定元素的下標

//返回 element 在順序表中的下標,如果出現多次,返回第一次出現的下標

public

intindexof

(int element)

}return-1

;}查詢指定下標的元素

public

intget

(int index)

return array[index];}

//4.改

//更新元素的下標

public

void

set(

int index,

int element)

array[index]

= element;

}//5.列印

public

void

print()

system.out.

println()

;}//6.獲得現有順序表的個數

public

intsize()

//7.判斷順序表是否為空

public

boolean

isempty()

//8.保證容量夠用,否則進行擴容

private

void

ensurecapacity()

int newcapacity = array.length *2;

int[

] newarray =

newint

[newcapacity]

;for

(int i =

0; i < size; i++

) array = newarray;

}public

static

void

main

(string[

] args)

}

資料結構 順序表的基本操作

計算機中線性表的存放結構主要有兩種 順序儲存結構和鏈式儲存結構。採用前者存放方式的線性表是順序表,採用後者的就是我們平時所說的鍊錶 線性鍊錶 這裡先對順序表的一些基本操作進行歸納和總結,鍊錶的將在後面的文章中歸納總結。順序表的表示,一般都是借助一維陣列。c 語言定義其結構如下 const int m...

資料結構 順序表的基本操作

main include include define true 1 define error 0 define ok 1 define false 0 define overflow 2 typedef int status typedef int elemtype define list ini...

資料結構 順序表的基本操作

老規矩先來看一下題目 本題要求實現順序表元素的增 刪 查詢以及順序表輸出共4個基本操作函式。l是乙個順序表,函式status listinsert sq sqlist l,int pos,elemtype e 是在順序表的pos位置插入乙個元素e pos應該從1開始 函式status listdel...