鍊錶題目整理 C語言實現

2021-07-11 12:00:19 字數 2045 閱讀 1861

struct node node_t;

list_display(node_t *head)

list_len(node_t *head)

int main(int argc, const char *argv)

, c = , b = , a = ;

printf("%d\n", list_len(&a));//4

return 0;

}

reverse(node_t *head)

, 0}, c = , b = , a = ;

list_display(&a);

reverse(&a);

list_display(&d);

, 0}, c = , b = , a = ;

printf("_0 :%s _1: %s _2:%s _3:%s\n"

, _kth(&a, 0)->data, _kth(&a, 1)->data, _kth(&a, 2)->data, _kth(&a, 3)->data);

_0 :d _1: c _2:b _3:a

return q;

}

node_t e = , d = , c = , b = , a = ; 

printf("%s\n", middle(&a)->data);

r_display(node_t *t)}

any_ring(node_t *head)

return

0; //fail find

}

node_t e = , d = , c = , b = , a = ; 

e.next = &a;

printf("%d\n", any_ring(&a));

初始化三個指標p, q, r全部指向head. 然後p以2的速度行進, q以1的速度行進.當p和q相遇的時候, 發出r指標並以1的速度行進, 當p和r相遇返回這個結點.複雜度o(n)

if (!p) return

0; //no ring in list

for (r = head, q = q->next; q != r; r = r->next, q = q->next);

return r; }

, 0}, d = , c = , b = , a = ;

e.next = &d;

printf("

%s\n

", find_entry(&a)->data);

is_intersect(node_t *a, node_t *b)

, 0}, d = , c = , b = , a = ;

node_t z = , y = , x = ;

printf("

%d\n

", is_intersect(&a, &x));

p,q分別遍歷鍊錶a,b,假設q先到達null,此時從a的頭髮出乙個指標t,當p到達null時,從b的頭髮出s,當s==t的時候即交點.

, 0}, d = , c = , b = , a = ;

node_t z = , y = , x = ;

printf("

%s\n

", intersect_point(&a, &x)->data);

把d的下乙個結點e的資料拷貝到d中,然後刪除e

1 2 3 4 5 

6 7 8

p和q兩個指標分別遍歷鍊錶a和b,假如q先到達0(即a比b長),此時由a頭髮出t,列印完鍊錶a.

p繼續移動到0,並列印空格.

從b頭髮出指標s列印鍊錶b

foo(node_t *a, node_t *b)

node_t o = , n = , m = ;

foo(&a, &m);

鍊錶的C語言實現

編輯 c巨集例項 以下 摘自linux核心2.6.21.5原始碼 部分 展示了鍊錶的另一種實現思路,未採用ansi c標準,採用gnu c標準,遵從gpl版權許可。struct list head define list head init name define list head name st...

雙向鍊錶C語言實現

ifndef stdlist h define stdlist h typedef struct tagstdnode stdnode,lpstdnode typedef struct tagstdlist stdlist,lpstdlist 鍊錶資料結構 struct tagstdnode 鍊錶節...

線性鍊錶 C語言實現

include include define error 0 define ok 1 define equal 1 define overflow 1 define list init size 100 define listincrement 10 struct stustu 50 typedef...