資料結構 鍊錶(C )

2021-08-10 01:37:10 字數 2299 閱讀 6593

typedef

int rank

#define listnodeposi(t) listnode*

template

class listnode

listnode(t e,listnodeposi(t) p=null,listnodeposi(t) s=null)

:data(e),prenode(p),backnode(s){}

~listnode(){}

listnodeposi(t) insertaspred(t const & e); //在當前節點之前插入新的節點

listnodeposi(t) insertasback(t const & e); //在當前結點之後插入新的結點

};template

listnode::insertaspred(t const & e)

template

listnode::insertasback(t const & e)

template

class list : public listnode //返回規模大小

listnodeposi(t) first() //返回頭結點

listnodeposi(t) last() //返回尾結點

bool empty() //判斷是否為空

listnodeposi(t) insertasfirst(t const & e); //在首元結點前插入新結點

listnodeposi(t) insertaslast(t const & e); //在尾指標前插入新結點

listnodeposi(t) insertasbefore(listnodeposi(t) p,t const & e);

listnodeposi(t) insertasafter(listnodeposi(t) p,t const & e);

t remove(listnodeposi(t) p); //刪除p結點

void clear(); //清除鍊錶所有結點

void sort(listnodeposi(t) p,int n); //歸併排序

void merge(listnodeposi(t) p,int n,list& l,listnodeposi(t) q,int m); //歸併操作

listnodeposi(t) search(t const & e) const; //查詢

};//建構函式,對成員變數的初始化

template

list::list()

//析構函式,將成員變數的記憶體釋放

template

list::~list()

//在首元結點之前插入新結點

template

listnodeposi(t) list::insertasfirst(t const & e)

//在尾指標之前插入結點

template

listnodeposi(t) list::insertaslast(t const & e)

//在p結點之前插入新結點

template

listnodeposi(t) list::insertasbefore(listnodeposi(t) p,t const & e)

//在p點之後插入新結點

template

listnodeposi(t) list::insertasafter(listnodeposi(t) p,t const & e)

//刪除節點

template

t list:: remove(listnodeposi(t) p)

//清除所有結點

template

void list::clear()

}//歸併排序

template

void list::merge(listnodposi(t) p,int n,listl,listnodeposi(t) q,int m) else

p=pp->backnode;

}//歸併排序

template

void list::sort(listnodeposi(t) p,int n)

//查詢

template

listnodeposi(t) list::search(t const & e)

return p;

}

以上**實現的是雙向鍊錶,基本實現了鍊錶的基本增、刪、查、改的功能,在此基礎上,還實現了鍊錶的歸併操作,和歸併排序演算法。

C 資料結構 鍊錶

理論基礎 鍊錶是用一組任意的儲存單元來儲存線性表中的資料元素。如果結點的引用域只儲存該結點直接後繼結點的儲存位址,則該鍊錶叫單鏈表 singly linked list 單鏈表由頭引用h唯一確定。頭引用指向單鏈表的第乙個結點,也就是把單鏈表第乙個結點的位址放在h中。c 實現 1介面 引用線性表的介面...

C 資料結構 鍊錶

資料結構2 鍊錶.cpp 此檔案包含 main 函式。程式執行將在此處開始並結束。include pch.h include using namespace std typedef struct lnode 定義乙個節點 list void createla list l,int n,int len...

資料結構(C 鍊錶

鍊錶結構體的定義 struct ndoe typedef struct node node,pnode 頭插法插入結點 1.建立頭結點,並分配空間 2.建立新結點,新節點 next指向上乙個節點,頭結點 next指向新結點 pnode list headinsert pnode head pnode...