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

2021-10-08 04:46:24 字數 2175 閱讀 7918

#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

int status;

typedef

int elemtype;

typedef

struct

sqlist;

//構造空線性表

status initlist_sq

(sqlist& l)

//寫入資料

void

inputdata

(sqlist& l)

l.length = n;

}//遍歷

void

listprint

(sqlist l)

printf

("\n");

}//判空

status listempty

(sqlist l)

//輸出線性表元素個數

intlistlength

(sqlist l)

//清空線性表

status clearlist

(sqlist& l)

//銷毀線性表

void

destroylist

(sqlist& l)

//從線性表中獲取第i個元素

status getelem

(sqlist l,

int i, elemtype* e)

intlocateelem_sq

(sqlist l, elemtype e)

if(i <= l.length)

return i;

else

return0;

}//刪除線性表中的資料

status listdelete_sq

(sqlist& l,

int i, elemtype& e)

//線性表中插入資料

status listinsert_sq

(sqlist &l,

int i, elemtype e)

// 注意:c語言中陣列的下標從0開始,線性表中第i個元素是l.elem[i-1]

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

);//q為插入位置

for(p =

&(l.elem[l.length -1]

); p >= q;

--p)

*(p +1)

=*p;

//插入位置之後元素後移

*q = e;

++l.length;

return ok;

}status mergelist

(sqlist la, sqlist lb, sqlist& lc)

}while

(pa <= pa_last)

//表la非空且表lb空

*pc++

=*pa++

;//插入la的剩餘元素

while

(pb <= pb_last)

//表lb非空且表la空

*pc++

=*pb++

;//插入lb的剩餘元素

return ok;

}void

main()

case5:

if(clearlist

(l)== ok)

printf

("clear!\n");

break

;case6:

destroylist

(l);

break

;case7:

case8:

case9:

case10:

case11:

else

printf

("lb init fail!\n");

break;}

}system

("pause");

}destroylist

(l);

}

資料結構之順序表的基本操作(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 define maxn 100 typedef int status using namespace std typedef structsqlist 初始化順序表 status initlist sqlist l 順序表的插入 將要插入的新元素e放到第i個位置 status lis...

資料結構(C語言版) 順序表的實現

完整 include includeusing namespace std define maxsize 100 define stepsize 10 define success 1 define error 1 typedef int elemtype int flag 0 用於判斷順序表是否初...