c 簡單鍊錶實現

2021-09-30 06:11:36 字數 3893 閱讀 1864

以下為linklist.**件**

#ifndef linklist_h_included 

#define linklist_h_included 

typedef

structlnode    

lnode, *plinklist;    

classlinklist    

;    

#endif // linklist_h_included 

以下為linklist.cpp檔案**    

#include 

#include "linklist.h" 

linklist::linklist()    

linklist::~linklist()    

}    

//初始化,分配乙個頭節點。 

boollinklist::initlist()    

plist->next = null;    

return

true;    

}    

//銷毀鍊錶。 

boollinklist::destroylist()    

deleteplist;    

return

true;    

}    

//判斷鍊錶是否為空。若為空,返回true,否則返回false。 

boollinklist::isempty()     

return

false;    

}    

//返回鍊錶的中當前節點數。 

intlinklist::getlength()    

//將鍊錶清空,釋放當前所有節點。 

boollinklist::clearlist()    

lnode *ptemp = null;    

while(plist->next != null)    

listlength = 0;    

return

true;    

}    

//將position指定的節點內的資料設定為newdata。 

//第乙個有效節點的position為1。 

boollinklist::setnodedata(intposition,intnewdata)    

ptemp->data = newdata;    

return

true;    

}    

//得到指定位置節點的資料。 

//節點索引從1到listlength。 

boollinklist::getnodedata(intposition,int&data)    

data = ptemp->data;    

return

true;    

}    

//在鍊錶中插入乙個節點。 

//插入的位置由beforewhich指定,新節點插入在beforewhich之前。 

//beforewhich的取值在1到listlength+1之間。 

boollinklist::insertnode(intbeforewhich,intdata)    

if(!(getnode(beforewhich - 1, &ptemp)))    

lnode *newnode =newlnode;    

newnode->data = data;    

newnode->next = ptemp->next;    

ptemp->next = newnode;    

listlength++;    

return

true;    

}    

//刪除乙個指定的節點。 

//節點位置由position指定。 

//positon的值從1到listlength。 

//若煉表為空或指定的節點不存在則返回false。 

/* 

bool linklist::deletenode(int position)    

lnode *ptemp = null; 

if (!(getnode(position - 1, &ptemp)))    

lnode *pdel = null; 

pdel = ptemp->next; 

ptemp->next = pdel->next; 

delete pdel; 

listlength--; 

return true; 

}*///通過指定data刪除節點 

boollinklist::deletenode(intdata)    

ptemp = ptemp->next;    

}    

lnode *pdel = null;    

pdel = ptemp->next;    

ptemp->next = pdel->next;    

deletepdel;    

listlength--;    

return

true;    

}    

//得到指定位置節點的指標。 

boollinklist::getnode(intposition, lnode **node)    

if(curpos != position)    

*node = ptemp;    

return

true;    

}    

//定位與指定資料相等的資料節點。 

//如果在當前鍊錶中已經存在該資料則返回該資料節點的索引號。 

//若不存在這樣的節點則返回0。 

//節點索引從0開始到listlength。 

intlinklist::locateelem(intelem)    

if(ptemp == null)    

returncurindex;    

c 鍊錶簡單實現

鍊錶 include using namespace std template struct node template class linklist public linklist node p new node head p p next null node push front const t...

C 鍊錶簡單功能實現

實現的是乙個小型圖書館的程式,功能包括增加新書,以及讀者借書和還書等。如下 include include include include include using namespace std class patron class book bool operator const book bk ...

簡單鍊錶實現

今天元旦,不想工作。只想寫一寫自己想學習的東西。今天就寫了個鍊錶的單向鍊錶。標頭檔案chain.h ifndef chain define chain include include using namespace std templateclass chain templateclass chai...