C語言實現資料結構 順序表,單鏈表

2021-09-28 19:43:04 字數 2868 閱讀 8047

線性表分為:順序儲存結構和連儲存結構

順序儲存結構的優點:

1.空間利用率高,幾乎不需要額外的空間開銷.

2.資料的邏輯結構和物理結構完全一致.

3.結點位址計算的時間和線性表的規模大小無關.

4.可以用一維陣列實現儲存.

但是有兩個缺點:

1.順序儲存結構的儲存空間是靜態分配,必須有足夠大的連續儲存空間,如果不能則無法實現儲存.在建立順序表時,儲存空間大小有時無法確切估計,估計過大回會使空閒區段的部分空間長期閒置,估計過小則會在操作中因空間不夠而產生溢位.

2.插入操作和產出操作在大多數情況下,引起大量節點的頻繁移動,降低了演算法的時間效率.

因此:順序儲存結構 比較適合規模較小,長度變化不大且不很頻繁的線性表儲存實現.

克服線性表順序儲存結構的方法採用鍊錶儲存結構,鍊錶儲存結構的儲存分配方式靈活,有效性好.用鍊錶儲存結構儲存的線性表稱為"鍊錶".

#include

#include

#include

#define ok 0

#define overflow 1

#define error -1

#define ture 1

#define false 0

#define list_init_size 100

#define listincrement 10

typedef

int elemtype;

typedef

int status;

typedef

struct

sqlist;

status initlist_sq

(sqlist &l)

//構造乙個空的線性表l

status listinsert_sq

(sqlist &l,

int i,elemtype e)

//在順序線性表l中第i個位置之前插入新的元素e

//i的合法值為1<=i<=listlength.sq(l)+1

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

);for(p=

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

);p>=q;

--p)

*(p+1)

=*p;

*q=e;

++l.length;

return ok;

} status listdelete_sq

(sqlist &l,

int i,elemtype e)

//在順序線性表中刪除第i個元素,並用e返回其值

status input

(sqlist &l,

int n)

//輸入函式

}status output

(sqlist &l)

//輸出函式

intmain()

else}}

else

if(n==2)

else

printf

("刪除失敗\n");

}}}return0;

}

#include

#include

#include

#include

using namespace std;

#define ok 1

#define error 0

#define ture 1

#define false 0

#define overflow -1

typedef

int status;

typedef

int elemtype;

typedef

struct lnode lnode,

*linklist;

//建立單鏈表

void

createlist_l

(linklist &l,

int n)

}//獲取第i個元素的值

status getelem_l

(linklist l,

int i,elemtype &e)if(

!p || j > i)

e = p->data;

return ok;

}status listinsert_l

(linklist &l,

int i, elemtype e)if(

!p || j > i -1)

return error;

linklist s;

s =(linklist)

malloc

(sizeof

(lnode));

//生成新節點s

s->data = e;

s->next = p->next;

p->next = s;

return ok;

}status listdelete_l

(linklist &l,

int i, elemtype &e)if(

!(p->next)

|| j > i -1)

return error;

linklist q;

q = p->next;

p->next = q->next;

e = q->data;

free

(q);

return ok;

}void

showl

(linklist l)

}int

main()

資料結構 鍊錶 單鏈表 C語言實現

include includetypedef int elementtype typedef int boolean define true 1 define false 0 typedef struct linklinklist 建立乙個節點 引數可以指定節點的資料 linklist create...

資料結構 線性表 單鏈表C語言實現

單鏈表的練習 include stdlib.h include stdio.h void printlinklist struct node h 單鏈表資料結構 typedef struct node lnode,linklist 建立鍊錶 帶頭結點 linklist createlinklist ...

資料結構 單鏈表c語言實現

list.h如下 ifndef list h define list h typedef struct node node,list void initlist list list bool insert head list list,int val bool insert tail list li...