鍊錶基礎操作

2021-09-25 21:31:38 字數 2917 閱讀 5073

結點所在類

#pragma once

#include#include"singlelink.h"

templateclass node

t get_data()

~node() {};

};

單鏈表所在類

#pragma once

#includeusing namespace std;

templateclass node; //兩個模板類會相互包含,那麼需要在類定義之前宣告其中乙個類,並且宣告時要用模板。

templateclass singlelink

node* get_head() const

int get_size() const

void remove(int loc);

bool revise(int loc, t data);

t get_data(int loc);

void reverse();

void concat(singlelink* link1, singlelink* link2, singlelink*& link3); //合併兩個鍊錶

node* get_middle_node();

}; node* s = new node;

s->data = data;

//注意:在node建構函式中next已經初始化為null, 此處只是在鍊錶最後新增元素,所以不用再賦值

p->next = s;

size++;

}templatet singlelink::get_data(int loc)

node* p = head;

int i = 0;

//注意,此處因為有size限定loc訪問範圍,因此可以不用判斷head->next是否為空,具體自己推導

while (p != null && i <= loc)

return p->data;

}templatevoid singlelink::remove(int loc)

node* p = head;

node* q = p;

int i = 0;

while (p != null && i <= loc)

q->next = p->next;

delete p;

size--;

}templatebool singlelink::revise(int loc, t data)

node* p = head;

int i = 0;

while (p != null && i <= loc)

p->data = data;

return true;

}templatevoid singlelink::reverse(){}

templatevoid singlelink::concat(singlelink* link1, singlelink* link2, singlelink*& link3)

if (link2->get_size() == 0)

node* p = link1->get_head()->next;

node* q = link2->get_head()->next;

while (p != null && q != null)

else

}while (p != null)

while (q != null) }

templatenode* singlelink::get_middle_node()

return q;

}

迴圈鍊錶類

#pragma once

#includeusing namespace std;

templateclass node;

templateclass circularlink

;templatecircularlink::circularlink()

node* s = new node;

s->data = data;

s->next = head;

p->next= s;

size++;

}

雙向鍊錶類

#pragma once

#includeusing namespace std;

templateclass node;

templateclass doublelink

nodeget_tail()

};templatedoublelink::doublelink()

node* s = new node;

s->data = data;

s->next = null;

p->next = s;

s->next = null;

tail->next = s;

if (p != head)

s->prev = p;

}

// main.h 主函式所在檔案

#include#include"node.h"

#include"singlelink.h"

using namespace std;

int main()

for (int i = -3; i < 5; i++)

link->concat(link2, link3, link);

for (int i = 0; i < link->get_size(); i++)

node* n = link->get_middle_node();

cout system("pause");

return 0;

}

鍊錶基礎操作

1.鍊錶定義 struct listnode 2.鍊錶建立 方法一 尾插法 有頭結點 即輸出順序與插入順序一致 listnode head new listnode 0 head next null listnode p,r r head int x while cin x r next null ...

鍊錶的基礎操作實現

首先是構造鍊錶 include include define maxsize 100 typedef structsqlist 定義乙個結構體 void initlist sqlist l 構造乙個空的鍊錶 else printf 申請失敗 n int main 然後是填充這個鍊錶 插入鍊錶 inc...

靜態鍊錶基礎操作彙總

線性表的靜態鍊錶儲存結構 define maxsize 1000 typedef struct compent,staticlinklist maxsize 將一維陣列space中各分量鏈成一備用鍊錶 stactic initlist staticlinklist space space maxsi...