鍊錶操作 完

2021-09-12 23:30:50 字數 2503 閱讀 6013

c++實現鍊錶操作

使用節點類構造節點

包括 建立鍊錶,顯示鍊錶,插入節點,刪除節點,查詢節點,鍊錶長度和反轉鍊錶

#ifndef __list_h__

#define __list_h__

#include#include//size_t

using namespace std;

//用結構體型別表示乙個節點

class node

void set_data(const value_type& value)

void set_link(node* link)

value_type data()

node* link()

private:

value_type data_field;

node* link_field;

};void display_list(node* head_ptr);//輸出鍊錶

node* create_list(int num);//建立鍊錶

void remove_head(node*& head_ptr);//刪除頭節點

void remove_node(node* previous_ptr);//刪除節點

void insert_head(node*& head_ptr, node::value_type entry);//在頭節點插入

void insert_node(node* previous_ptr, node::value_type entry);//插入節點

node* search_node(node* head_ptr, node::value_type target);//查詢幾點

size_t size_list(node* head_ptr);//鍊錶長度

node* reverse_list(node* head_ptr);//反轉鍊錶

#endif

#include"list.h"

//輸出鍊錶

void display_list(node* head_ptr)

cout << endl;

}//建立鍊錶

node* create_list(int num)

return head_ptr;

}//刪除頭節點

void remove_head(node*& head_ptr)

//刪除節點

void remove_node(node* previous_ptr)

//在頭節點插入

void insert_head(node*& head_ptr, node::value_type entry)

//插入節點

void insert_node(node* previous_ptr, node::value_type entry)

//查詢節點

node* search_node(node* head_ptr, node::value_type target) }}

//節點長度

size_t size_list(node* head_ptr)

}//反轉鍊錶

node* reverse_list(node* head_ptr)

return cur_previous;

}

#include#include#include"list.h"

using namespace std;

int main() {

int n = 5;

node* head_ptr = create_list(n);//建立鍊錶

display_list(head_ptr);//1 2 3 4 5

remove_head(head_ptr);//在頭節點刪除

display_list(head_ptr);//2 3 4 5

insert_head(head_ptr, 100);//在頭節點插入

display_list(head_ptr);//100 2 3 4 5

insert_node(head_ptr,90);//插入乙個節點

display_list(head_ptr);//100 90 2 3 4 5

remove_node(head_ptr);//刪除乙個節點

C 鍊錶操作總結和常見鍊錶操作

一 鍊錶的定義 鍊錶是一種動態資料結構,他的特點是用一組任意的儲存單元 可以是連續的,也可以是不連續的 存放資料元素。鍊錶中每乙個元素成為 結點 每乙個結點都是由資料域和指標域組成的,每個結點中的指標域指向下乙個結點。head是 頭指標 表示鍊錶的開始,用來指向第乙個結點,而最後乙個指標的指標域為n...

LeetCode 鍊錶Tag(0828肝完簡單題)

中等題 困難題 基於以前的一些基礎 leetcode 刪除鍊錶中的節點 單雙指標實現 leetcode 反轉單鏈表 頭插法 雙指標 輔助棧 leetcode 兩個鍊錶的第乙個公共節點 簡單易懂雙指標 leetcode 從尾到頭列印單鏈表 老韓資料結構 josephu問題 單鏈表實現 自定義類實現單鏈...

LinkList鍊錶操作

以下的c 類linklist實現了線性鍊錶的一般操作。可以直接在其他的程式中直接建立它的物件,其中線性表中的資料在此為整型,具體應用的時候可以適當的修改,並可以在此基礎上繼續封裝特定的功能。標頭檔案 linklist.h typedef struct lnode lnode,plinklist cl...