順序表的基本操作 c語言版

2021-10-03 18:01:20 字數 2074 閱讀 1889

1.初始化順序表

2.擴充套件順序表的儲存空間

3.列印順序表中的資料

4.向順序表中插入資料

5.刪除順序表中的元素,並將刪除的資料儲存在變數e中

#include

#include

#define initsize 10

//預設最大長度

typedef

struct

seqlist;

//初始化順序表

void

initlist

(seqlist *l)

}//擴充套件順序表的儲存空間

void

increasesize

(seqlist *l,

int len)

l->maxsize = l->maxsize + len;

//增加順序表的最大儲存空間的標誌

free

(p);

//釋放原來空間中的指標資料

}//列印順序表中的資料

void

printflist

(seqlist *l)

printf

("\n");

}//向順序表中插入資料

void

listinsert

(seqlist *l,

int i,

int e)

//如果插入的序號大於順序表的長度,插入失敗

if(i > l->length)

int k =0;

//程式裡面第乙個元素下標為0

//因此程式裡面的序列是 位序 - 1

int real_i = i -1;

int real_length = l->length -1;

for(k = real_length +

1; k > real_i; k--

) l->data[real_i]

= e;

l->length++

;//順序表的長度+1

}//刪除順序表中的元素,並將刪除的資料儲存在變數e中

void

listdelete

(seqlist *l,

int i,

int*e)

int k =0;

//程式裡面第乙個元素下標為0

//因此程式裡面的序列是 位序 - 1

int real_i = i -1;

int real_length = l->length -1;

*e = l->data[real_i]

;for

(k = real_i; k < real_length; k++

) l->length--

;//順序表的長度+1

}int

main()

printflist

(&l)

;//列印順序表中的資料

increasesize

(&l,5)

;//擴充套件順序表的儲存空間,擴大5個儲存空間

for(i = initsize -

1; i < l.maxsize; i++

)printflist

(&l)

;//列印順序表中的資料

listinsert

(&l,3,

333)

;//向順序表中插入資料

printflist

(&l)

;//列印順序表中的資料

int e =-1

;listdelete

(&l,3,

&e);

//刪除順序表中的元素,並將刪除的資料儲存在變數e中

printf

("\n%d\n"

, e)

;//列印e的值

printflist

(&l)

;//列印順序表中的資料

return0;

}

順序表的基本操作 C語言版

標頭檔案apr 07.h define crt secure no warning 1 pragma once include define max size 10 typedef int datatype typedef struct seqlist seqlist,pseqlist typede...

資料結構之順序表的基本操作(c語言版)

include include define maxsize 50 typedef struct sqlist 插入 bool listinsert sqlist l,int i,char e 刪除 bool listdelete sqlist l,int i,char e l length ret...

《資料結構》C語言版 順序表的基本操作實現

include include define ok 1 define error 0 define true 1 define false 0 define overflow 1 define list init size 100 define listincrement 10 typedef in...