链表迭代器
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; }
相关文章推荐
- iOS简单动画效果
- 文章标题
- 密码验证合格程序(华为oj)
- 3、MongoDB数据库集合常用操作
- freemarker生成java代码
- 小米笔试题 风口的猪-中国牛市
- 关于ListView中点击item后背景色不改变,但是事件的确执行了
- hbase family qualifier设置
- HashMap实现原理,利用数组和链表存储元素
- HDU 4283 You Are the One(区间DP)
- 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
- 00001--awk取时间段日志
- 扣丁学堂笔记第27天NDK、屏幕适配与APP上线
- iOS滑动条UISlider的使用方法
- busybox中tftp服务器使用命令
- 结对编程之黄金点游戏
- html中的表格 bootstrap-table
- gcc编译选项学习
- 337. House Robber III
- [C#]解决第三方控件引起的"类型Universe无法解析程序集"问题