順序表 陣列

2022-09-04 02:18:10 字數 2085 閱讀 9034

第i個資料元素的儲存位置:loc(ai)=loc(ai)+(i-1)*l;1≤i≤n(l為每個元素需佔l個儲存單元)

第(i+1)個資料元素的儲存位置loc(ai+1)

和第i個資料元素的儲存位置loc(ai)的關係:loc(ai+1)=loc(ai)+l;

陣列:

#define  ok   1

#define error -1

#define max_size 100typedef

intstatus ;

typedef

intelemtype ;

typedef

struct

sqlist

sqlist ;

1. 順序線性表初始化

status init_sqlist( sqlist *l ) 

2. 順序線性表的插入---實現步驟(einsert=n/2,平均時間複雜度為o(n))

/*(1) 將線性表l中的第i個至第n個結點後移乙個位置。

(2) 將結點e插入到結點ai-1之後。

(3) 線性表長度加1

*/

status insert_sqlist(sqlist *l,int

i ,elemtype e)

for( j=l->length–1; j>=i-1; --j )

l->elem_array[j+1]=l->elem_array[j];/*

i-1位置以後的所有結點後移

*/l->elem_array[i-1]=e; /*

在i-1位置插入結點

*/l->length++;

return

ok ;

}

3 順序線性表的刪除(einsert=(n-1)/2,平均時間複雜度為o(n))

/*(1) 將線性表l中的第i+1個至第n個結點依此向前移動乙個位置。

(2) 線性表長度減1。

*/

elemtype  delete_sqlist(sqlist *l,inti) 

if(i<1||i>l->length)

x=l->elem_array[i-1] ; /*

儲存結點的值

*/for(k=i; klength; k++)

l->elem_array[k-1]=l->elem_array[k];/*

i位置以後的所有結點前移

*/l->length--;

return

(x);

}

status  locate_delete_sqlist(sqlist *l,elemtype x)/*

刪除線性表l中值為x的第乙個結點

*/ }

if (i>l->length)

return

ok;

}

動態陣列:

typedef struct

sqlist;

建立順序表(陣列):

status init(sqlist &l)

插入:

status insertlist(sqlist *l,int i,elemtype e)   //

第i的位置插入元素

q=&(l.elem[i-1]); //

p插入的位置

for(p=&(l.elem[length-1]);p>=q;--p)

*(p+1)=*p; //

指標移動

*q=e; //

插入元素

++l.length; //

表長加一

return

ok;}

刪除:

status deletlist(sqlist *l,int

i,elemtype e)

順序表 陣列實現

description 順序表 version 1.0 author meify 2013 7 30 上午9 43 45 public class arraylist 獲取當前線性表的大小 public int getsize 往線性表中index位置插入一條資料 public void inser...

順序表應用5 有序順序表歸併(陣列做法)

順序表應用5 有序順序表歸併 time limit 100 ms memory limit 880 kib submit statistic discuss problem description 已知順序表a與b是兩個有序的順序表,其中存放的資料元素皆為普通整型,將a與b表歸併為c表,要求c表包含...

線性表之陣列順序表

ifndef seqlist h define seqlist h include include include define seqlist init size 8 define inc size 3 typedef int elemtype typedef struct seqlist seq...