線性表之陣列順序表

2021-10-05 14:10:11 字數 4106 閱讀 9559

#ifndef __seqlist_h__

#define __seqlist_h__

#include#include#include#define seqlist_init_size 8

#define inc_size 3

typedef int elemtype;

typedef struct seqlist

seqlist;

bool inc(seqlist *list);

void initseqlist(seqlist *list);

void push_back(seqlist *list, elemtype x);

void push_front(seqlist *list, elemtype x);

void show_list(seqlist *list);

void pop_back(seqlist *list);

void pop_front(seqlist *list);

void insert_pos(seqlist *list, int pos, elemtype x);

int find(seqlist *list, elemtype key);

int length(seqlist *list);

void delete_pos(seqlist *list, int pos);

void delete_val(seqlist *list, elemtype 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 //__seqlist_h__

#include"seqlist.h"

void main()

/*void main()

break;

case 2:

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

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

break;

case 3:

show_list(&mylist);

break;

case 4:

pop_back(&mylist);

break;

case 5:

pop_front(&mylist);

break;

case 6:

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

scanf("%d",&item);

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

scanf("%d",&pos);

insert_pos(&mylist,pos,item);

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("請輸入要刪除資料的位置:>");

scanf("%d",&pos);

delete_pos(&mylist,pos);

break;

case 10:

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

scanf("%d",&item);

delete_val(&mylist,item);

break;

case 11:

sort(&mylist);

break;

case 12:

resver(&mylist);

break;

case 13:

clear(&mylist);

break;

//case 14:

//destroy(&mylist);

// break;

default:

printf("輸入的選擇錯誤,請重新輸入.\n");

break;

} }destroy(&mylist);

}*/

#include"seqlist.h"

bool inc(seqlist *list)

list->base = newbase;

list->capacity += inc_size;

return true;

}void initseqlist(seqlist *list)

void push_back(seqlist *list, elemtype x)

list->base[list->size] = x;

list->size++;

}void push_front(seqlist *list, elemtype 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; isize-1; ++i)

list->size--;

}void insert_pos(seqlist *list, int pos, elemtype x)

if(list->size >= list->capacity && !inc(list))

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

list->base[pos] = x;

list->size++;

}int find(seqlist *list, elemtype key)

return -1;

}int length(seqlist *list)

void delete_pos(seqlist *list, int pos)

for(int i=pos; isize-1; ++i)

list->size--;

}void delete_val(seqlist *list, elemtype key)

delete_pos(list,pos);

}void sort(seqlist *list)

} }}void resver(seqlist *list)

}void clear(seqlist *list)

void destroy(seqlist *list)

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

while(ia < la->size)

while(ib < lb->size)

lt->size = la->size + lb->size;}/*

void insert_pos(seqlist *list, int pos, elemtype x)

if(pos == 0)

push_front(list,x);

else if(pos == list->size)

push_back(list,x);

else

list->base[pos] = x;

list->size++; }}

*/

線性表之順序表

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

線性表之順序表

線性表 我們都知道是一種常用的資料結構,也是歷來各種考試的重點。今天抽了一些時間把線性表做了總結。線性表是n個資料元素的乙個有限序列。用公式表示為 l a1,a2,a3,a4,an 因為線性表是乙個有限的序列,所以也如上面公式所示,它的各個元素是相繼排放的。那麼它的每個相連的兩項之間都是有乙個邏輯關...

線性表之順序表

線性表的操作 initlist l 初始化操作,建立乙個空的線性表l。listempty l 判斷線性表是否為空表,空返回true,否則返回false。clearlist l 將線性表清空。getelem l,i,e 將線性表l中的第i個位置元素值返回給e。listdelete l,i,e 刪除線性...