乙個簡單鍊錶的C 實現 二

2022-05-17 08:44:08 字數 2105 閱讀 9252

/*

llist.cpp

* author: qiang xiao

* time: 2015-07-12

*/#include

using

namespace

std;

class

node

};class

llist

int getelementbypos(int

);

void

pop();

int getlast() const

;

bool deleteelementbypos(int

);};

int llist::getlast() const

bool llist::deleteelementbypos(int

pos)

node* tmp= this->head;

int k= -1

;

while(k< pos- 1

) node* del= tmp->ptr;

tmp->ptr= tmp->ptr->ptr;

delete

del;

this->length--;

return

true;}

void

llist::pop()

node* t= this->tail;

this->tail=tmp;

this->length--;

delete

t, tmp;

}int llist::getelementbypos(int

pos)

node* p= this->head->ptr;

int k= 0

;

while(kint m= p->data;

returnm;}

llist::llist()

llist::~llist()

delete

head;

delete

tail;

}bool llist::insert(int pos, node*node)

int i= 0

; node* fence= new

node();

fence= this->head;

while(inode->ptr= fence->ptr;

fence->ptr=node;

this->length++;

return

true;}

this->tail->ptr=node;

this->tail=node;

this->length++;

return

true;}

void

llist::print()

cout

<}int

main()

cout

<<"

\n\n*********************pop3***********************\n";

list->pop();

list->print();

cout

<<"

\n\n*******************get last*********************\n";

cout

/*node* node7= new node(7);

list->print();

*/cout

<<"

\n******************after delete********************\n";

int k2= 2

; list->deleteelementbypos(k2);

list->print();

return0;

}

比上一版本(僅僅是多了兩個操作:pop() 和 deleteelementbypos(int pos)。

還是沒有把指標給完全弄懂,這兩天得花一些時間,好好看一下書中現成的**。

敬請指正。

歡迎交流!

乙個簡單的雙向鍊錶(C 實現)

直接上 親測有用。ifndef dlink h define dlink h phead index0 index1 index2 phead phead index0 index1 index2 phead phead 不儲存資料。index是從0開始的。count index 1 templat...

乙個簡單的單向鍊錶 C

singlylinkedlist.h include include class node 構造方法,方便快速建立結點 node int data,node next class list 返回鍊錶長度 list 建構函式 list const list temp 拷貝建構函式 list 析構函式 ...

迴圈鍊錶的乙個C 實現

下面是迴圈鍊錶的乙個c 實現,參考了 資料結構與演算法分析c語言版 及不少牛人的分析總結,在此一併感謝了。在vc2005上經反覆驗證試驗,結果非常不錯。這是乙個比較簡易的實現,讓其繼承自單鏈表,並提供了乙個專為解決約瑟夫問題的函式 removeat 以後將會開發乙個更一般性的版本。注意 單鏈表及雙向...