自學完C也有20來天了,最近都在學C++,今天在C++中偶然間看到了鏈表,就心血來潮想著自己寫個雙層鏈表,檢驗下C的鏈表學得怎麼樣,寫完就迫不及待傳到博客上了。要說CSDN博客真是個好東西,互相分享心得,筆記,互相學習,還方便自己以後查閱。
雙層鏈表:鏈表的每一個節點中都存放著一個鏈表。
#include"double node.h" #include#include #include #include using namespace std; //頭文件#include"double node.h"中的代碼如下: struct smallnode //內層鏈表 { int num; //編號 int data; //存放的數據 struct smallnode *pnext;//指向下一個內層鏈表節點 }; typedef struct smallnode Smallnode; struct bignode //外層鏈表 每個節點中存有一個內層鏈表 { int num; //編號 struct smallnode *pheadsmall; //指向內層鏈表的頭節點 struct bignode *pnext; //指向下一個外層鏈表節點 }; typedef struct bignode Bignode; void main() { array array = { 0,1,2,3,4,5,6,7,8,9 }; Bignode *pheadBnode; //存儲外層鏈表頭節點 Bignode *pBnode; //存儲開辟內存後的節點地址 Smallnode *pSnode; //存儲開辟內存後的節點地址 Bignode *pBtemp; //副本存儲上一節點地址 Smallnode *pStemp; //副本存儲上一節點地址 /**********************************************為外層頭節點開辟內存**********************************************/ pBnode = (Bignode *)malloc(sizeof(Bignode)); //為外層鏈表頭節點分配內存 pBtemp = pBnode; //記錄地址 用於改變下一節點指針指向 pBnode->num = 0; pBnode->pnext = NULL; pSnode = (Smallnode *)malloc(sizeof(Smallnode)); //為內層鏈表頭節點分配內存 pBnode->pheadsmall = pSnode; //存儲內層鏈表頭節點 pheadBnode = pBnode; //記錄頭節點 用於打印數據 pStemp = pSnode; //記錄地址 pSnode->num = 0; pSnode->data = array[0]; pSnode->pnext = NULL; for (int j = 1; j < 10; j++) { pSnode = (Smallnode *)malloc(sizeof(Smallnode)); //為內層鏈表節點分配內存 pStemp->pnext = pSnode; //前面已經賦值pStemp = pSnode; 這裡就是給上一節點的pnext賦值 指向當前分配的節點地址 pStemp = pSnode; //改變副本存儲的地址 pSnode->num = j; pSnode->data = array[j]; pSnode->pnext = NULL; } /**********************************************為其余外層節點開辟內存**********************************************/ for (int i = 1; i < 10; i++) { pBnode = (Bignode *)malloc(sizeof(Bignode)); //為外層鏈表節點分配內存 pBtemp->pnext = pBnode; pBtemp = pBnode; //改變副本存儲的地址 pBnode->num = i; pBnode->pnext = NULL; pSnode = (Smallnode *)malloc(sizeof(Smallnode));//為內層鏈表節點分配內存 pBnode->pheadsmall = pSnode; //存儲內層鏈表頭節點 pStemp = pSnode; //記錄地址 pSnode->num = 0; pSnode->data = array[0]; pSnode->pnext = NULL; for (int k = 1; k < 10; k++) { pSnode = (Smallnode *)malloc(sizeof(Smallnode));//為內層鏈表節點分配內存 pStemp->pnext = pSnode; pStemp = pSnode; //改變副本存儲的地址 pSnode->num = k; pSnode->data = array[k]; pSnode->pnext = NULL; } } /**********************************************打印鏈表數據 驗證正確與否**********************************************/ for (int h = 0; h < 10; h++) { pSnode = pheadBnode->pheadsmall; //指向內層鏈表頭節點 cout << "當前外層節點地址: " << pheadBnode << " " << "下一外層節點地址: " << pheadBnode->pnext << " " << "當前外層節點編號: " << pheadBnode->num << endl; cout << "\n"; for (int g = 0; g < 10; g++) { cout << "當前內層節點地址: " << pSnode << " " << "下一內層節點地址: " << pSnode->pnext << " " << "當前內層節點存儲的數據: " << pSnode->data << endl; pSnode = pSnode->pnext; } cout << "\n\n\n"; pheadBnode = pheadBnode->pnext; //指向下一外層節點 } cin.get(); //system("pause"); } //這裡解釋下為什麼要把外層頭節點和外層其余節點分開寫: //******************************頭節點中,是這樣的****************************** //pBnode = (Bignode *)malloc(sizeof(Bignode)); //為外層鏈表頭節點分配內存 //pBtemp = pBnode; //記錄地址 用於改變下一節點指針指向 1 //pBnode->num = 0; 2 //pBnode->pnext = NULL; 3 //pSnode = (Smallnode *)malloc(sizeof(Smallnode));//為內層鏈表頭節點分配內存 4 //pBnode->pheadsmall = pSnode; //存儲內層鏈表頭節點 5 //pheadBnode = pBnode; //記錄頭節點 用於打印數據 6 //pStemp = pSnode; //記錄地址 7 //pSnode->num = 0; 8 //pSnode->data = array[0]; //pSnode->pnext = NULL; //*********************************其余節點,是這樣的******************************** //for (int i = 1; i < 10; i++) //{ // pBnode = (Bignode *)malloc(sizeof(Bignode)); //為外層鏈表節點分配內存 // pBtemp->pnext = pBnode; 11 // pBtemp = pBnode; //改變副本存儲的地址 12 // pBnode->num = i; 13 // pBnode->pnext = NULL; 14 // pSnode = (Smallnode *)malloc(sizeof(Smallnode));//為內層鏈表節點分配內存 15 // pBnode->pheadsmall = pSnode; //存儲內層鏈表頭節點 16 // pStemp = pSnode; //記錄地址 17 // pSnode->num = 0; 18 // pSnode->data = array[0]; // pSnode->pnext = NULL; //細微的差別就在於外層頭節點前面沒有節點,所以標號2處 只是記錄當前外層節點的地址 //而其余外層節點之前都有節點 所以標號12,13處 先給上一外層節點的pnext賦值 再改變pBtemp的指向 //標號6處是為了後面方便打印鏈表 存儲頭節點用的 可以無視 //同理也要把內層頭節點和內層其余節點分開寫