單項鍊表迭代器的實現

2021-08-21 11:48:03 字數 579 閱讀 1957

之前說過迭代器將不同的容器的訪問方式統一起來,所以鍊錶也有自己的迭代器。單項鍊表由於不能向後查詢,所以只能實現向前迭代器。

要使用迭代器,鍊錶一定要支援迭代器,一般加入下面兩個函式就行了。

iterator begin() 

iterator end()

即返回首尾指標的位置。

class iterator

t& operator*()const

t* operator->()const

iterator& operator++()

iterator operator++(int)

bool operator!=(const iterator right)const

bool operator==(const iterator right)const

protected:

chainnode*node;

}

以上就是鍊錶迭代器類了,裡面比較難得可能就是對各種運算子的過載了,說實話->這個符號的過載我也有點不懂,所以

查清楚後在基礎篇記錄下來。

建立單項鍊表,然後實現單項鍊表逆序

建立乙個任意數目的單項鍊表,每項的位置作為自己的初始資料 返回鏈頭 node initlink int num return head 輸出單項鍊表的全部資料 void display node head node curnode head while curnode.next null syste...

單項鍊表反轉

遍歷,將當前節點的下乙個節點快取後更改當前節點指標 public static node reverse node head node pre head node cur head.getnextnode node next while null cur 將原鍊錶的頭節點的下乙個節點置為null,再...

建立單項鍊表

鍊錶是動態分配儲存空間的鏈式儲存結構,其中包括乙個 頭指標 變數,頭指標中存放乙個位址,該位址指向乙個元素。鍊錶中每乙個元素稱為 節點 每個節點都由兩部分組成,即儲存資料元素的資料域和儲存直接後繼儲存位置的指標域。指標域中儲存的即是鍊錶的下乙個節點的位置,是乙個指標。多個節點構成乙個鍊錶。inclu...