資料結構 單向鍊錶

2021-07-15 21:15:31 字數 3261 閱讀 9530

//鍊錶結構的資料格式儲存

#include "stdafx.h"//把這行放在最開始。

#include#includeusing namespace std;

typedef struct

data;

typedef struct node//這裡與上面的不同是多了node,注意如果沒有這個node,下面的struct node*nextnode;就會報錯說找不到這個node。

cltype;//這裡的資料結構型別是cltype與上面的data一樣,而不該是node!

cltype* claddend(cltype*head,data nodedata);

void clallnode(cltype*head);

cltype*claddfirst(cltype*head,data nodedata);

cltype*cldeletenode(cltype*head,char*key);

int cllength(cltype*head);

cltype*clinsertnode(cltype*head,char *findkey,data nodedata);

cltype*clfindnode(cltype*head,char*key);

int main()

else

}while(1);

clallnode(head);//鍊錶都是從頭結點開始乙個個去尋找的。

printf("\n演示插入結點,輸入插入位置的關鍵字:");

cin>>findkey;

printf("\n輸入插入點的資料(關鍵字 姓名 年齡):");

cin>>nodedata.key>>nodedata.name>>nodedata.age;

head=clinsertnode(head,findkey,nodedata);

clallnode(head);

printf("\n演示刪除結點,輸入要刪除的關鍵字:");

fflush(stdin);

cin>>key;

head=cldeletenode(head,key);//注意不是cltype*head

clallnode(head);

printf("\n演示插入頭結點,輸入插入點的資料(關鍵字 姓名 年齡):");

fflush(stdin);

cin>>nodedata.key>>nodedata.name>>nodedata.age;

head=claddfirst(head,nodedata);

clallnode(head);

printf("\n演示計算結點長度:");

len=cllength(head);

coutif(head==null)//說明這個head來的時候,指向的是null 也就是它是頭指標

//重要的**,演示如何查詢響應的資訊。

htemp=head;//讓htemp來取代head,其實這裡是等同於b=3;a=b;作用當head是頭結點但不尾結點,也就是這不是空鍊錶的時候。那麼就該找到最後乙個結點,標誌就是nextnode=null

while(htemp->nextnode!=null)//查詢尾結點,當nextnode指向的不是null的時候,就不是尾結點,為了要繼續查詢,所以讓遍歷一般

htemp->nextnode=node;//node是現在存放著尾結點的新新增結點,要把node所代表的位址傳給原來鍊錶的尾結點並取代掉所指向的位址null,

//切注意這裡不能用htemp=node,如此會是把原來的最後乙個結點丟失,變成把node的位址傳給原來的倒數第二個結點的nextnode

return head;//還是要返回頭結點,鍊錶的特性就是要知道任何乙個結點的位置都要從頭結點開始,如此就需要返回頭結點。 }}

void clallnode(cltype*head)

while(htemp) }

//插入結點

cltype*clinsertnode(cltype*head,char *findkey,data nodedata)//插入結點,但要先分配記憶體,和查詢是不是有這個結點,有這個結點時候再移動。

node->nodedata=nodedata;//把新新增的資料放到新增加的記憶體的資料域裡面(能執行到這裡說明已經分配好記憶體了),

nodetemp=clfindnode(head,findkey);//上面步驟已經完成了申請記憶體和把資料存入新增加的記憶體中。接下來是要把這個記憶體放到鍊錶中。

if(nodetemp)//說明不為null 的情況下,那麼就是找到了該節點,接下來就是要把node的位址放到nodetemp的nextnode中。注意node的位址域還是空的

else//如果說沒能找到這個結點

return head;//返回頭指標, }

//查詢結點

cltype*clfindnode(cltype*head,char*key)//從頭開始查詢,查詢無法是找到了,那麼返回該結點的位址,要麼沒有找到返回乙個null 或者是0

//能到這裡說明strcmp(htemp->nodedata.key,key)已經搜過了,但不對。

htemp=htemp->nextnode;//常用的迴圈方法,可以把htemp->nextnode去覆蓋掉htemp裡面的nextnode位址值,這樣就能保證一直往下搜尋

} return null;//如果不能從if裡面返回,那麼到這裡就說明沒找到,要返回null

}//刪除結點

cltype*cldeletenode(cltype*head,char*key)//要刪除結點,首先得確定有沒有這個結點,有這個結點的情況下,

//把它刪除後,那麼它儲存的nextnode要給到他前面的結點,如果可以直接覆蓋那麼就不需要再申請指標。但是要迴圈就得暫時指標

node=htemp;//不斷更新htemp前結點,這裡是用node來存放要刪除結點前面的乙個結點的。

htemp=htemp->nextnode; }

return head;

}//計算鍊錶長度

int cllength(cltype*head)//拿乙個來計算,然後遍歷一遍就好了

return i;

}//插入頭結點

cltype*claddfirst(cltype*head,data nodedata)//插入頭結點,那麼就需要先分配一塊記憶體,然後再把頭結點的記憶體給到它就好了。

node->nodedata=nodedata;

node->nextnode=head;

return node;

}

資料結構(單向鍊錶)

ifndef linklist h define linklist h 鍊錶節點 template class linklistdata linklistdata linklistdata 獲取資料 t getdata public t data 資料 template class linklist...

資料結構 單向鍊錶

錯誤 h next null 或其他 讓指標指向空 的語句執行報錯 null undeclared identifier cannot convert from int to struct node 原因 未引入標頭檔案 include stdio.h 把null定義為乙個空位址是在 stdio標頭...

資料結構 單向鍊錶

include using namespace std template class t class node node const t element element element node const t element,node next element element template c...