简单实现c++数组,链表及其迭代器
2017-05-08 20:34
330 查看
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<time.h> using namespace std; template<class T> class MyArray { private: int m_nTotalSize; int m_nValidSize; T* m_pData; public: class Iterator { T* t; public: Iterator(T* t) { this->t = t; } bool operator!=(Iterator it) { return this->t != it.t; } void operator++(int) { this->t++; } T operator*() { return *this->t; } }; MyArray(int nSize = 3) { m_pData = new T[nSize]; m_nTotalSize = nSize; m_nValidSize = 0; } void Add(T value) { int i; if (m_nValidSize < m_nTotalSize) { m_pData[m_nValidSize] = value; m_nValidSize++; } else { T* tempData = new T[m_nTotalSize]; for (i = 0;i < m_nTotalSize;i++) { tempData[i] = m_pData[i]; } delete[]m_pData; m_nTotalSize *= 2; m_pData = new T[m_nTotalSize]; for (i = 0;i < m_nValidSize;i++) { m_pData[i] = tempData[i]; } delete[]tempData; m_pData[m_nValidSize] = value; m_nValidSize++; } } int GetSize() { return m_nValidSize; } T Get(int pos) { return m_pData[pos]; } T* Begin() { return m_pData; } T* End() { return m_pData + m_nValidSize; } virtual ~MyArray() { if (m_pData != NULL) { delete[]m_pData; m_pData = NULL; } } }; template <class T> class MyLink { public: struct Unit { T value; Unit* next; }; friend ostream& operator<<(ostream &os, Unit &unit) { os << unit.value; return os; } class Iterator { Unit* init; public: Iterator(Unit* init) { this->init = init; } bool operator!=(Iterator& it) { return this->init != it.init; } void operator++(int) { init = init->next; } Unit operator*() { return *init; } }; Unit* head; Unit* tail; Unit* prev; public: MyLink() { head = tail = prev = NULL; } void Add(T value) { Unit* u = new Unit(); u->value = value; u->next = NULL; if (head == NULL) { head = u; prev = u; } else { prev->next = u; prev = u; } tail = u->next; } Unit* Begin() { return head; } Unit* End() { return tail; } virtual ~MyLink() { Unit* prev = head; Unit* next = NULL; if (head != NULL) { while (prev != tail) { next = prev->next; delete prev; prev = next; } } } }; template <class Init> void display(Init start, Init end) { cout << endl; for (Init mid = start;mid != end;mid++) { cout << *mid << "\t"; } cout << endl; } int main() { int i; MyArray<int> MyArr; MyLink<int> MyLnk; srand(static_cast<unsigned int>(time(NULL))); for (i = 0;i < 5;i++) { MyLnk.Add(rand()); } MyLink<int>::Iterator start = MyLnk.Begin(); MyLink<int>::Iterator end = MyLnk.End(); display(start, end); srand(static_cast<unsigned int>(time(NULL))); for (i = 0;i < 5;i++) { MyArr.Add(rand()); } MyArray<int>::Iterator start1 = MyArr.Begin(); MyArray<int>::Iterator end1 = MyArr.End(); display(start1, end1); system("pause"); return 0; }
相关文章推荐
- STL之双向链表及其迭代器简单实现
- 双向链表下的简单迭代器实现
- C语言实现一个简单的单向链表list
- IOC介绍及其简单实现
- c#自定义简单链表通过IEnumerable接口,范型实现内部迭代
- 并查集及其链表与不相交集合森林实现
- c++简单链表实现
- 一种简单的嵌入式字库设计及其实现原理
- 程序2:用单链表实现简单的栈
- C# 简单链表的实现下(使用泛型)
- C# 简单链表的实现上(object类型)
- c语言中链表的概念和简单的实现
- 单向链表的简单实现
- 简单链表的实现
- silverlight2实现播放器的简单原理及其mediaelement的属性介绍
- c#自定义简单链表通过IEnumerable接口实现内部迭代
- 使用C#链表简单实现的约瑟夫环
- c#中简单链表的实现(非泛型)
- 一个简单的双向循环链表的实现
- 一个简单的链表的实现