鍊錶的相關操作及簡單通訊錄系統的實現

2021-06-27 00:27:34 字數 4399 閱讀 7249

鍊錶基礎:

對於需要經常插入資料的線性表,一般採用連式儲存的鍊錶,鍊錶是採用動態儲存分配的一種結構,可以根據需要申請記憶體單元。

鍊錶的每乙個結點都應該包括兩個部分,一部分是實際資料,一部分是下乙個結點的位址,操作鍊錶時,需要定義乙個「頭指標「變數(head),該指標指向第乙個結點,第乙個結點又指向第二個結點。。。一直到最後乙個結點,最後乙個結點不再指向其他結點,稱為」表尾「,在表尾位址放入」null「,表示空位址,鍊錶至此結束。

在鍊錶中,邏輯上相鄰的結點在記憶體中不一定相鄰,邏輯相鄰關係通過指標實現,因此對於鍊錶的訪問只能從表頭逐個查詢,一直找到需要的結點為止,而不是像順序表那樣隨機訪問。

可以用malloc函式動態分配結點的儲存空間,刪除某個結點時,再用free函式釋放結點所占用的記憶體空間。

鍊錶的基本操作:

鍊錶的基本操作有:新增結點(加到表頭,加到表尾),查詢,插入,刪除,獲取鍊錶結點數量等。

首先定義操作鍊錶的標頭檔案 」chainlist.h「

#include
#includetypedef struct node     //定義結構體作為鍊錶的資料型別

chainlisttype;

chainlisttype *chainlistaddend(chainlisttype *head,data data);

chainlisttype *chainlistaddfirst(chainlisttype *head,data data);

chainlisttype *chainlistfind(chainlisttype *head,char *key);

chainlisttype *chainlistinsert(chainlisttype *head,char *findkey,data data);

int chainlistdelete(chainlisttype *head, char *key);

int chainlistlength(chainlisttype *head);

因為需要動態分配記憶體的函式malloc,需要標頭檔案stdlib.h。。如果需要做雙向鍊錶,需要在結構體中再增加乙個指標,指向上乙個結點。

鍊錶操作函式儲存在檔案"chainlist.c"中:

1.新增結點到尾部:

若要在尾部新增結點,需要從頭(head)開始逐個往後,直到找到最後乙個結點,即表尾,然後將表尾結點的位址部分由null更改為新增的結點位址(原來表尾的結點指向新增的結點),然後將新增結點位址部分設定為null。

#include//新增結點到尾部 

chainlisttype *chainlistaddend(chainlisttype *head,data data) //新增節點到鍊錶尾部

node->data=data; //儲存資料

node->next=null; //設定結點指標為空 ,即表尾

if(head==null) //判斷頭指標是否為空,若為空則指向當前節點

//若頭指標不為 空

h=head; //儲存頭指標

while(h->next!=null) //迴圈,使指標一直指到最後,即null

h=h->next; //往後一直查詢

h->next=node; //原來表尾結點位址指向新增結點位址

return head;

}

對於新增結點到尾部時,如果只知道頭指標head,則要從頭部一直查詢到鍊錶的尾部,如果經常需要這樣做,可以定義乙個尾指標,指向鍊錶尾部,這樣在多次給尾部新增結點時,可省去查詢過程。

2.新增結點到首部

在首部新增結點,使新增結點指向頭指標所指的結點,head指向新增結點即可。

//新增結點到首部

chainlisttype *chainlistaddfirst(chainlisttype *head,data data) //head為煉表頭指標,data為結點儲存資料

node->data=data;

node->next=head; //指向頭指標 所指向的結點

head=node; //頭指標指向新增結點

return head;

}

3.查詢結點

對於鍊錶中儲存的資料,可通過關鍵字查詢

//查詢結點

chainlisttype *chainlistfind(chainlisttype *head,char *key)

return null;  

} 從頭指標開始,如果指標h的值不為null,就對結點關鍵字和查詢關鍵字進行比較,是否相等,一直到表尾,若還沒周到返回空指標。

4.插入結點

插入結點即在鍊錶中間部分新增結點,首先找到插入的位置a(邏輯位置),使插入位置a原來指向位置b的結點指向新增結點,而新增結點指向位置b。

//插入結點

chainlisttype *chainlistinsert(chainlisttype *head,char *findkey,data data)

node->data=data;

node1=chainlistfind(head,findkey); //findkey為在鍊錶中查詢的結點(a)關鍵字,找到a後在該結點後面新增新增結點

if(node1) //找到要插入的節點

else

return head;

}

6.刪除結點

首先查詢到需要刪除的結點(delete),然後還需要定義乙個指向需要刪除結點的前乙個結點的指標(a),修改a,使a原來指向需要刪除結點(delete)修改為指向刪除結點後的那個結點(b),這樣完成了邏輯上的刪除,然後再釋放掉需要刪除結點(delete)的記憶體,這樣才刪除成功。

刪除前 a

deleteb

刪除後 ab

注:若是查詢到結點後,直接釋放記憶體,而沒有完成邏輯上的刪除,則a結點以後無法完成後面鏈結,b結點也無法被前面的指標鏈結到,這樣的刪除是不行的。

//刪除節點

int chainlistdelete(chainlisttype *head,char *key) //key為需要刪除的關鍵字

else

} return 0; //未刪除

}

7.鍊錶長度

//鍊錶的長度

int chainlistlength(chainlisttype *head)

return i;

}

以上是鍊錶的基本操作,下面用鍊錶製作乙個簡單的通訊錄

最簡單的通訊錄包括:新增聯絡人,查詢聯絡人,刪除聯絡人,顯示聯絡人資訊。下面用上面介紹的鍊錶來實現這些功能。

通訊錄的標頭檔案:

typedef struct

data; //資料結點型別

#include #include #include #include "chainlist.h"

#include "chainlist.cpp"

結構體中定義 姓名(並設定姓名為關鍵字),聯絡人位址和**號。引用上面操作過的函式檔案

1.顯示聯絡人模組:

2.新增聯絡人模組

//新增聯絡人

chainlisttype *input(chainlisttype *head) //向通訊錄輸入資訊

3.查詢模組

void find(chainlisttype *head)

else

printf("沒有找到相應的聯絡人,請重新查詢\n");

printf("\n");

}

4.刪除模組

//刪除聯絡人

void delete(chainlisttype *head)

5.主函式

//主模組

int main()

} while(select!=0);

}

鍊錶通訊錄

include include include define password 208140119 define name max 20 define gender max 10 define str max 20 define tel max 20 鍊錶結構 struct address type...

鍊錶通訊錄

include include include include include struct addressbook typedef struct addressbook addressbook typedef struct addressbook link int main menu void a...

鍊錶寫的通訊錄

include include include include struct node typedef struct node node typedef struct node link void create link head void insert link head,link new nod...