c語言線性表之順序表實現

2021-10-10 19:03:36 字數 4638 閱讀 4479

//避免重複編譯

#ifndef __seqlist_h__

#define __seqlist_h__

#include

#include

#include

#define seqlist_init_size 8

#define inc_size 3

typedef

int elmetype;

typedef

struct seqlist

seqlist;

bool inc

(seqlist *list)

;void

initseqlist

(seqlist *list)

;void

push_back

(seqlist *list,elmetype x)

;void

push_front

(seqlist *list,elmetype x)

;void

show_list

(seqlist*list)

;void

pop_back

(seqlist *list)

;void

pop_front

(seqlist *list)

;void

insert_pos

(seqlist *list,elmetype x,

int pos)

;int

find

(seqlist *list,elmetype key)

;int

length

(seqlist *list)

;void

delete_pos

(seqlist *list,

int pos)

;void

delete_val

(seqlist *list,elmetype key)

;void

sort

(seqlist *list)

;void

resver

(seqlist *list)

;void

clear

(seqlist *list)

;void

destroy

(seqlist *list)

;void

merge

(seqlist *lt,seqlist *la,seqlist *lb)

;#endif

#include

"seqlist.h"

void

merge

(seqlist *lt,seqlist *la,seqlist *lb)

while

(iasize)

while

(ibsize)

lt->size=la->size+lb->size;

}bool inc

(seqlist *list)

list->base=newbase;

list->capacity+

=inc_size;

return true;

}void

initseqlist

(seqlist *list)

void

push_back

(seqlist *list,elmetype x)

list->base[list->size]

=x; list->size++;}

void

push_front

(seqlist *list,elmetype x)

for(

int i = list->size; i >0;

--i)

list->base[0]

=x; list->size++;}

void

show_list

(seqlist*list)

printf

("\n");

}void

pop_back

(seqlist *list)

list->size--;}

void

pop_front

(seqlist *list)

for(

int i =

0; i < list->size-

1; i++

) list->size--;}

void

insert_pos

(seqlist *list,elmetype x,

int pos)

if(list->size>=list->capacity&&

!inc

(list)

)for

(int i=list->size;i>pos;

--i)

list->base[pos]

=x; list->size++;}

intfind

(seqlist *list,elmetype key)

}return-1

;}intlength

(seqlist *list)

void

delete_pos

(seqlist *list,

int pos)

for(

int i = pos; i size-1;

++i)

list->size--;}

void

delete_val

(seqlist *list,elmetype key)

delete_pos

(list,pos);}

//氣泡排序

void

sort

(seqlist *list)}}

}void

resver

(seqlist *list)

}void

clear

(seqlist *list)

void

destroy

(seqlist *list)

#include

"seqlist.h"

void

main()

/*int main()

show_list(&mylist);

break;

case 2:

printf("請輸入要插入的資料:(-1結束)>");

while (scanf("%d",&item),item!=-1)

show_list(&mylist);

break;

case 3:

show_list(&mylist);

break;

case 4:

pop_back(&mylist);

show_list(&mylist);

break;

case 5:

pop_front(&mylist);

show_list(&mylist);

break;

case 6:

printf("請輸入要插入的資料:>");

scanf("%d",&item);

printf("請輸入要插入的位置:>");

scanf("%d",&pos);

insert_pos(&mylist,item,pos);

break;

case 7:

printf("要查詢的資料:>");

scanf("%d",&item);

pos=find(&mylist,item);

if(pos==-1)

printf("查詢資料%d在順序表不存在,\n",item);

else

printf("查詢的資料%d在順序表存在下標%d\n",item,pos);

break;

case 8:

printf("順序表的長度為》 %d\n",length(&mylist));

break;

case 9:

printf("請輸入要刪除資料的位置:>\n");

scanf("%d",&pos);

delete_pos(&mylist,pos-1);

break;

case 10:

printf("請輸入要刪除資料的值:>\n");

scanf("%d",&item);

delete_val(&mylist,item);

case 11:

sort(&mylist);

break;

case 12:

resver(&mylist);

break;

case 13:

clear(&mylist);

break;

case 14:

destroy(&mylist);

break;

default:

printf("輸入的資料有誤,請重新輸入\n");

break;

} }return 0;

}*/

線性表之順序表C 實現

線性表之順序表 一 標頭檔案 seqlist.h 1 順序線性表的標頭檔案 2 include3 4const int maxsize 100 5 定義順序表seqlist的模板類 6 template 7class seqlist 11 順序表有參構造器 建立乙個長度為n的順序表 12 seqli...

C 實現順序表(線性表)

基本思想是使用陣列作為盛放元素的容器,陣列一開始的大小要實現確定,並使用乙個pointer指向順序表中最後的元素。順序表中的元素是陣列中元素的子集。順序表在記憶體中是連續的,優勢是查詢,弱勢是插入元素和刪除元素。為避免裝箱拆箱,這裡使用泛型,代替object。使用object的例子可以參照這個鏈結中...

線性表之順序表

資料結構草草學過,不過沒有認真運用過。雖然知道一些最為基本的抽象型別及一些常用操作,不過叫我把這些基本的演算法寫出來我也 是寫不出來的。因為常說資料結構 演算法是乙個程式設計師最基本的素質,所以這次認真加以複習。在複習的同時我盡量將自己學習的其他的 一些基本知識比如c 中的物件導向思想也引入進來,同...