資料結構 帶頭節點的單鏈表的表示和實現

2021-09-25 07:51:52 字數 2614 閱讀 6523

#includeusing namespace std;

typedef struct lnode lnode, *linklist;

// 初始化鍊錶,頭節點資料域存放鍊錶長度

bool initlist(linklist &l)

// 建立鍊錶(尾插法)

void createlist(linklist &l)

r->next = null;

}// 列印鍊錶

void printlist(linklist l)

coutdata)

// 根據節點資料域返回節點位址

lnode *locateelem(linklist l, int data)

return p;

}// 插入節點到第pos個位置

void insertelem(linklist &l, int pos, int data)

lnode *n = new lnode;

n->next = p->next;

p->next = n;

n->data = data;

l->data++;

}// 刪除第pos個節點

void deleteelem(linklist &l, int pos)

lnode *q = p->next;

p->next = q->next;

delete q;

l->data--;

}int main() {

linklist l;

initlist(l);

createlist(l);

printlist(l);

cout<<"將19插入到第5個節點的位置"next = 0x6f7fd8

p2= 0x6f7fd8 p->data = 1 p->next = 0x6f7f28

p3= 0x6f7f28 p->data = 2 p->next = 0x6f7f38

p4= 0x6f7f38 p->data = 3 p->next = 0x6f7f48

p5= 0x6f7f48 p->data = 4 p->next = 0x6f7f58

p6= 0x6f7f58 p->data = 5 p->next = 0x6f7ae0

p7= 0x6f7ae0 p->data = 6 p->next = 0x6f7af0

p8= 0x6f7af0 p->data = 7 p->next = 0x6f7b00

p9= 0x6f7b00 p->data = 8 p->next = 0x6f7b10

p10= 0x6f7b10 p->data = 9 p->next = 0

將19插入到第5個節點的位置

head node = 0x6f7fb8

link length = 11

p1= 0x6f7fc8 p->data = 0 p->next = 0x6f7fd8

p2= 0x6f7fd8 p->data = 1 p->next = 0x6f7f28

p3= 0x6f7f28 p->data = 2 p->next = 0x6f7f38

p4= 0x6f7f38 p->data = 3 p->next = 0x6f7b20

p5= 0x6f7b20 p->data = 19 p->next = 0x6f7f48

p6= 0x6f7f48 p->data = 4 p->next = 0x6f7f58

p7= 0x6f7f58 p->data = 5 p->next = 0x6f7ae0

p8= 0x6f7ae0 p->data = 6 p->next = 0x6f7af0

p9= 0x6f7af0 p->data = 7 p->next = 0x6f7b00

p10= 0x6f7b00 p->data = 8 p->next = 0x6f7b10

p11= 0x6f7b10 p->data = 9 p->next = 0

資料域為19的節點位址為0x6f7b20

刪除第6個節點

head node = 0x6f7fb8

link length = 10

p1= 0x6f7fc8 p->data = 0 p->next = 0x6f7fd8

p2= 0x6f7fd8 p->data = 1 p->next = 0x6f7f28

p3= 0x6f7f28 p->data = 2 p->next = 0x6f7f38

p4= 0x6f7f38 p->data = 3 p->next = 0x6f7b20

p5= 0x6f7b20 p->data = 19 p->next = 0x6f7f58

p6= 0x6f7f58 p->data = 5 p->next = 0x6f7ae0

p7= 0x6f7ae0 p->data = 6 p->next = 0x6f7af0

p8= 0x6f7af0 p->data = 7 p->next = 0x6f7b00

p9= 0x6f7b00 p->data = 8 p->next = 0x6f7b10

p10= 0x6f7b10 p->data = 9 p->next = 0

第6個節點位址為0x6f7f58

資料結構 C 實現帶頭節點單鏈表

線性表的鏈式儲存又稱單鏈表。特點 不需要使用位址連續的儲存單元 插入和刪除操作不需要移動元素 非隨機訪問 附加指標域,存在浪費儲存空間的缺點 linklist.h pragma once include using namespace std 節點結構體 template class datatyp...

資料結構之單鏈表(帶頭節點)

今天來聊聊單鏈表。單鏈表是以節點存的鏈式結構,分為帶頭節點和不帶頭節點的單鏈表。單鏈表中的節點包括data域和next域,data域儲存元素的值,next域則儲存下乙個節點的位址。單鏈表中的各個節點在記憶體中不一定是連續儲存的,儲存形式如下圖所示 從圖中可以看出,單鏈表中的各個節點並不是連續儲存的。...

帶頭節點的單鏈表

需要注意 include define maxlen 20 define elementtype char using namespace std typedef struct slnode node 函式宣告 void initiallist node l int listlength node ...