鍊錶以及鍊錶的迭代器

2021-09-10 11:01:42 字數 2411 閱讀 7576

list.h:

#pragma once 

template

class list;

template

class listiterator;

//鍊錶節點

template

class listnode

;template

class list

;template

class listiterator

//用來判斷鍊錶迭代器所指向的煉表裡的當前的節點是否為空

bool notnull();

//用來判斷當前節點的下乙個節點是否為空

bool nextnotnull();

//指向鍊錶的第乙個節點,用來返回鍊錶的第乙個節點的指標

type* first();

//返回當前節點的下乙個節點的指標

type* next();

private:

//這個迭代器就是這個鍊錶的迭代器

const list&list;

//指向鍊錶中的乙個節點(當前的節點)

listnode* current;

//用來判斷鍊錶迭代器所指向的煉表裡的當前的節點是否為空

template

bool listiterator::notnull()

else

//用來判斷當前節點的下乙個節點是否為空

template

bool listiterator::nextnotnull()

else

}//指向鍊錶的第乙個節點,用來返回鍊錶的第乙個節點的指標

template

type* listiterator::first()

else

}//返回當前節點的下乙個節點的指標

template

type* listiterator::next()

else}/

template

listnode::listnode(type element) :data(element)

/template

list::list()

template

void list::insert(type k)

//template

//void list::show()//用於測試

////    std::cout << std::endl;

//}template

void list::delete(type k)

//如果找到了節點

if (current)

else

delete current;}}

template

void list::invert()

first = q;

}template

void list::concatenate(listb)

if (b.first)//如果第二個鍊錶不為空,則找到第乙個鍊錶的結尾,然後把第二個鍊錶頭部接過來

p->link = b.first;}}

main.cpp:

#include

#include "list.h"

#include

using namespace std;

int main()

cout << endl;

}listcharlist;

charlist.insert('a');

charlist.insert('b');

charlist.insert('c');

charlist.insert('d');

charlist.invert();

listcharlist1;

charlist1.insert('e');

charlist1.insert('f');

charlist1.invert();

charlist.concatenate(charlist1);

cout << "ok" << endl;

//c++中的鍊錶

cout << "c++ iterator\n";

listlistintergers;

listintergers.push_front(5);

listintergers.push_front(15);

listintergers.push_front(25);

listintergers.push_front(35);

list::iterator iter = listintergers.begin();

while (iter != listintergers.end())為最後乙個元素的下乙個(可以認為是結束符)

cout << endl;;

return 0;

}

python 鍊錶 迭代器

class mylinkedlist 雙向鍊錶 class node def init self,data,prev none next none self.data data self.prev prev self.next next def init self 有頭的雙向鍊錶 self.size...

STL 鍊錶和迭代器

include include include include include include include include 迭代器 空間配置器 using namespace std 鍊錶實現 namespace my 構造 template class t1,class t2 void con...

鍊錶的基本概念以及靜態鍊錶和動態鍊錶

鍊錶概念 鍊錶使用說明 畫圖示意 建立關係 node1.next node2 node2.next node3 node3.next node4 node4.next node5 node5.next null lk struct linknode lk nodecurrent node1 遍歷輸出...