数据结构与算法(C语言版)__链表的迭代器
2016-12-24 23:12
429 查看
STL中的链表
链表有一个迭代器,迭代器可以一个一个的输出数据,功能强大。今天我们也设计链表迭代器。
我们自己设计的链表迭代器
在VS2013中新建项目,在头文件中加入MyList.h,在源文件里面加入Main.cpp
下面是主程序
总结:自己写是链表虽然小,但是五脏俱全,但是真正在开发过程中,一般使用标准C++做好的链表,除非自己要设计有特殊功能的链表。
#include<list>
链表有一个迭代器,迭代器可以一个一个的输出数据,功能强大。今天我们也设计链表迭代器。
我们自己设计的链表迭代器
//MyList.h template<class Type> class ListIterator{ public: Boolean NotNull(); Boolean NextNotNull; Type* First(); Type* Next(); }
在VS2013中新建项目,在头文件中加入MyList.h,在源文件里面加入Main.cpp
#ifndef MYLIST_H #define MYLIST_H #include<iostream> 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> void List<Type>::Insert(Type k){ ListNode<Type> *newnode = new ListNode<Type>(k);//k是存放节点里面的数据; newnode->link = first; first = newnode; } template<class Type> ListNode<Type>::ListNode(Type element){ data = element; link = 0; } template<class Type> void List<Type>::Show(){ for (ListNode <Type> *current = first; current; current = current->link){ std::cout << current->data; if (current->link)cout << " -> "; } std::cout << std::endl; } 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>::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; } } #endif
下面是主程序
#include<iostream> #include<list>//C++STL中的链表 #include "MyList.h" using namespace std; int main(){ cout << "测试:" << endl; cout << "这是标准C++ STL中的链表和迭代器:" << endl; std::list<int> listIntegers; listIntegers.push_front(5); listIntegers.push_back(15); listIntegers.push_back(25); listIntegers.push_back(35); std::list<int>::iterator i = listIntegers.begin();//返回一个迭代器,指向链表里的第一个数据 while (i != listIntegers.end()){ cout << *i * 10<< "->"; ++i; } cout << endl; cout << "这是我的链表和迭代器:" << endl; List<int> initList; initList.Insert(5); initList.Insert(15); initList.Insert(25); initList.Insert(35); ListIterator<int>li(initList); //迭代是一种手段,可以一个一个的处理数据,想怎么处理就怎么处理 if (li.NotNull()){ cout << *li.First() * 10; while (li.NextNotNull()) cout << " -> " << *li.Next() * 10; cout << endl; } initList.Show(); initList.Delete(15); initList.Show(); initList.Delete(20); initList.Show(); initList.Invert(); initList.Show(); List<char> charList; charList.Insert('a'); charList.Insert('b'); charList.Insert('c'); charList.Insert('d'); charList.Show(); charList.Invert(); charList.Show(); List<char> char2List; char2List.Insert('e'); char2List.Insert('f'); char2List.Show(); char2List.Invert(); char2List.Show(); charList.Concatenate(char2List); charList.Show(); system("pause"); return 0; }
总结:自己写是链表虽然小,但是五脏俱全,但是真正在开发过程中,一般使用标准C++做好的链表,除非自己要设计有特殊功能的链表。
相关文章推荐
- 数据结构与算法(C语言版)__双向链表
- 【数据结构与算法】第三章 栈c实现,使用链表结构
- 数据结构之链表合并算法
- 数据结构与算法 2、单向/双向链表
- 算法与数据结构之三----循环链表
- 数据结构——算法之(023)( 单链表就地逆置,头插法)
- 链表相关的算法题大汇总 — 数据结构之链表奇思妙想
- 数据结构算法2.2 链表的合并
- 数据结构实验一--单链表的基本操作的算法
- .net 数据结构与算法基础:泛型链表使用
- 【数据结构与算法基础】单链表及其应用基数排序 / Singly Linked List and radix sort
- 数据结构与算法之链表(三)单链表反转
- [数据结构算法学习] 单向链表
- 数据结构与算法学习笔记——链表部分实现(数组形式)
- 数据结构与算法:链表(源码)!
- 数据结构之链表合并算法
- 数据结构与算法之—链表(二) 有序单链表合并
- 数据结构与算法面试题汇编(1)- 链表的操作与应用
- 数据结构与算法-链表的基本操作---ShinPans
- 数据结构之链表基本操作算法演示