鍊錶基礎知識整理

2021-08-04 10:16:55 字數 1416 閱讀 8912

關於鍊錶最重要的是搞清楚指標的指向,next域存放的是下乙個結點的位址。

1:順序建煉表要定義頭指標,游動指標,尾部指標;

逆序建立鍊錶定義頭指標和游動指標即可,每次將新的結點插在頭指標後面。搞清楚指標的指向即可。

2:刪除結點。定義乙個游動指標,還要定義乙個指標,為了記後乙個結點的位址。

//順序建表:

#include #include struct node

;int main()

p= head->next; //因為鍊錶只能從第乙個指標開始查詢,只有頭指標位置可知,所以讓游動指標開始為h->next的;

while(p!=null) //只要p不為空,就輸出。

else

printf(" %d",p->data);

p = p->next;//讓p 不斷移動、

}return 0;

}#include #include struct node

;struct node *name(int n)

return head; //返回頭指標。

};void print(struct node *h)

else

printf(" %d",p->data);

p = p->next;//讓p 不斷移動、

}};int main()

//逆序建鍊錶:

#include #include struct node

;struct node *name(int n)

return head;

}void print(struct node *head)

}int main()

//鍊錶結點的查詢

struct node *search(struct node *h,key)

return null; //即未找到;

}鍊錶的插入函式;

void insert(struct node *p,int key)

//鍊錶逆置函式:實質是逆序建表的過程:

struct node *reverse(struct node *head)

return head;

};//鍊錶的歸併:e題,實質是順序建表的過程

struct node *merge(struct node *h1,struct node *h2)

else

}if(p1) //進行完上述迴圈後,p1或p2有乙個有剩餘。將剩餘的直接連在尾部即可。

else

tail->next = p2;

return h1;

};

鍊錶基礎知識

1.為什麼用linkedlist array是乙個非常有用的資料結構,但是有兩個限制 1 當改變原有array的size的時候需要將原有array的所有元素copy到新array中去 2 由於array的資料儲存在記憶體中是連續空間,導致插入和刪除都會帶來其他資料的移動。鍊錶是由一系列的節點組成,這...

鍊錶基礎知識

鍊錶之前要有一定的c語言基礎,如指標,結構體,分配記憶體malloc等,切勿操之過急。第一次接觸可能有點難度,我是把書上的 反覆敲,反覆看注釋,搞了好幾天,才漸漸懂了。閒話少說,上 include include 要用到malloc struct llist int main printf 聯絡人 ...

鍊錶的基礎知識

鍊錶基礎知識 typedef int datatype typedef struct listnode listnode,pnode 鍊錶的初始化 void slistinit pnode phead 建立新結點 pnode buynewnode datatype data 尾插 void slis...