您的位置:首页 > 其它

链表迭代器

2016-04-08 20:12 363 查看
#ifndef _LIST_H
#define _LIST_H

template<class Type> class List;
template<class Type> class ListIterator;

template<class Type>
class ListNode
{
friend class List<Type>;
friend class ListIterator<Type>;
private:
Type data;  // 节点里的数据,
ListNode *link; // 指向下一个节点的数据,
ListNode(Type);
};

template<class Type>
class List
{
friend class ListIterator<Type>;
public:
List() { first = 0; }
void Delete(Type);
void Insert(Type);
void Invert();  // 翻转,
void Concatenate(List<Type>); // 链接,
void Show();  // 测试用的,
private:
ListNode<Type> *first;
};

template<class Type>
class ListIterator
{
public:
ListIterator(const List<Type>& l):list(l),current(l.first){}
bool NotNull();
bool NextNotNull();
Type* First();
Type* Next();
private:
const List<Type> &list;
ListNode<Type>* current;
};

template<class Type>
bool ListIterator<Type>::NotNull()
{
if(current) return true;
else return false;
}

template<class Type>
bool ListIterator<Type>::NextNotNull()
{
if(current && current->link) return true;
else return false;
}

template<class Type>
Type* ListIterator<Type>::First()
{
if(list.first) return &list.first->data;
else return 0;
}

template <class Type>
Type* ListIterator<Type>::Next()
{
if(current)
{
current = current->link;
return ¤t->data;
}
else return 0;
}

template<class Type>
ListNode<Type>::ListNode(Type element)
{
data = element; // 参数就是节点里的数据放到data里边,
link = 0;
}

template<class Type>
void List<Type>::Insert(Type k)
{
ListNode<Type> *newnode = new ListNode<Type>(k);
newnode->link = first;  // 新的节点作为第一个节点,
first = newnode;
}

template<class Type>
void List<Type>::Delete(Type k)
{
ListNode<Type> *previous = 0; // 前一个,
ListNode<Type> *current;
for(current = first;
current && current->data  != k;
previous = current, current = current->link)
{
;// 什么都不做,空循环,找到要被删除的节点,
}
if(current)
{
if(previous) previous->link = current->link;
else first = first->link;
delete current;
}
}

template<class Type>
void List<Type>::Concatenate(List<Type> b)
{
if(!first) { first = b.first; return; }
if(b.first)
{
ListNode<Type> *p;
for(p = first; p->link; p = p->link);  // 空循环,
p->link = b.first;
}
}

template<class Type>
void List<Type>::Invert()
{
ListNode<Type> *p = first, *q = 0;
while(p)
{
ListNode<Type> *r = q;
q = p;
p = p->link;
q->link = r;
}
first = q;
}

template<class Type>
void List<Type>::Show()
{
for(ListNode<Type> *current = first; current;current = current->link)
{
std::cout << current->data;
if(current->link) std::cout << " -> ";
}
std::cout << std::endl;
}

#endif _LIST_H


#include <iostream>
#include "List.h" //这个是自己做的链表,
#include <list>  // C++ STL中的链表

using namespace std;

int main()
{
cout << "这是标准C++ STL中的链表和迭代器:" << endl;
list<int> listIntegers;
listIntegers.push_front(3);
listIntegers.push_front(6);
list<int>::iterator i = listIntegers.begin();
while(i != listIntegers.end())
{
cout << *i << " -> ";
++i;
}
cout << endl;

cout << "这是自己设计的链表和迭代器:" << endl;
List<int> intList;
intList.Insert(31);
intList.Insert(12);
intList.Insert(3);
intList.Insert(6);

ListIterator<int> li(intList);
if(li.NotNull())
{
cout << *li.First() * 10;  // 在迭代的过程中可以进行操作,
while(li.NextNotNull())
cout << " -> " << *li.Next() * 10;
cout << endl;
}

return 0;

intList.Show();
intList.Invert();
intList.Show();

intList.Delete(6);
intList.Show();

intList.Delete(60);
intList.Show();

List<char> charList;
charList.Insert('x');
charList.Insert('i');
charList.Insert('a');
charList.Insert('o');
charList.Invert();
charList.Show();

List<char> charList2;
charList2.Insert('c');
charList2.Insert('u');
charList2.Insert('i');
charList2.Invert();
charList2.Show();

charList.Concatenate(charList2);
charList.Show();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: