练习C/C++基本功(列表和迭代器的实现和测试)
来源:互联网 发布:php李文凯百度百科 编辑:程序博客网 时间:2024/04/30 06:55
练习C/C++基本功(列表和迭代器的实现和测试)
#include "stdafx.h"#include <iostream>#include "stdlib.h"#include "stdio.h"#include "time.h"using namespace std;long DEFAULT_LIST_CAPACITY = 30;template <class Item> class List{public: List ( long size = DEFAULT_LIST_CAPACITY ) { arr=new Item[size]; this->count=0; } List ( List& ls ) { if(ls.arr) delete arr; arr=new Item[ls.count]; for(int i=0;i<ls.count;i++) arr[i]=ls.arr[i]; this->count=ls.count; } ~List ( ) { if(arr) delete []arr; arr=NULL; } List operator = ( const List& ls ) { if(this=&ls) return *this; if(ls.arr) delete arr; arr=new Item[ls.count]; for(int i=0;i<ls.count;i++) arr[i]=ls.arr[i]; this->count=ls.count; } long Count ( ) const { return this->count; } Item& Get ( long index ) const { return arr[index]; } Item& First ( ) const { return arr[0]; } Item& Last ( ) const { return arr[this->count-1]; } bool Includes ( const Item& item) const { for(int i=0;i<this->count;i++) if(item==arr[i]) return true; return false; } void Append ( const Item& item) { Item* temp=new Item[this->count+1]; for(int i=0;i<this->count;i++) temp[i]=arr[i]; temp[i+1]=item; delete []arr; arr=temp; this->count++; } void Prepend ( const Item& item) { Item* temp=new Item[this->count+1]; for(int i=1;i<this->count+1;i++) temp[i]=arr[i]; temp[0]=item; delete []arr; arr=temp; this->count++; } void Remove ( const Item& item) { Item* temp=new Item[count-1]; for(int i=0;i<this->count;i++) if(item==arr[i]) { i++; } else { temp[i]=arr[i]; } delete []arr; arr=temp; this->count--; } void RemoveLast ( const Item& ) { Item* temp=new Item[count-1]; for(int i=0;i<count-1;i++) temp[i]=arr[i]; count--; } void RemoveFisrt ( const Item& ) { Item* temp=new Item[count-1]; for(int i=0;i<count-1;i++) temp[i]=arr[i+1]; count--; } void RemoveAll ( const Item& ) { delete []arr; arr=NULL; count=0; } Item& Top ( ) const { return arr[0]; } void Push ( const Item& item ) { Item* temp=new Item[count+1]; for(int i=0;i<count;i++) temp[i]=arr[i]; temp[i+1]=item; count++; } Item& Pop () { Item* temp=new Item[count-1]; Item item=arr[0]; for(int i=0;i<count-1;i++) temp[i]=arr[i+1]; delete arr; arr=temp; return item; }private: Item* arr; int count;};template <class Item> class Iterator {public: virtual void First ( ) = 0; virtual void Next ( ) = 0; virtual bool IsDone ( ) const = 0; virtual Item CurrentItem ( ) const = 0;protected: Iterator ( ) {}};template <class Item> class ListIterator : public Iterator<Item> {public: ListIterator(const List<Item>* aList); virtual void First(); virtual void Next(); virtual bool IsDone() const; virtual Item CurrentItem() const;private: const List<Item>* _list; long _current;};template <class Item>ListIterator<Item>::ListIterator ( const List<Item>* aList ) : _list(aList), _current(0) { }template <class Item>void ListIterator<Item>::First () { _current = 0; }template <class Item>void ListIterator<Item>::Next () { _current++;}template <class Item>bool ListIterator<Item>::IsDone () const { return _current >= _list->Count(); }template <class Item>Item ListIterator<Item>::CurrentItem () const { if (IsDone()) { throw "IteratorOutOfBounds"; } return _list->Get(_current);}class Item{public: Item():a(0) { } Item(int a) { this->a=a; } bool operator!=(const Item& item) { if(this->a!=item.a) return true; else return false; }friend ostream& operator<<(ostream& os,const Item& item) { os<<item.a<<" "; return os; }private: int a;};int main(int argc, char* argv[]){ List<Item>* ls=new List<Item>(10); ListIterator<Item> itor(ls); for(int i=0;i<10;i++) { Item item(i); ls->Prepend(item); } while(!itor.IsDone()) { cout<<itor.CurrentItem(); itor.Next(); } return 0;}
- 练习C/C++基本功(列表和迭代器的实现和测试)
- 散列表的插入和检索(c语言实现)
- 基本功练习_3_2_之(GPRS.c)
- c和指针练习
- 再次实现Logistic Regression(c++)_实现和测试
- C语言实现串,和简单的主函数测试
- C语言基本功教程系列 文件, Socket 和 其它
- C++Primer的练习和心得
- lua和c的交互练习
- c/c++基本功测试题一套
- 搞不清楚NULL, "", '\0', '0', 0, "0"的程序猿(C系列)应该好好练习基本功
- c函数和指针练习
- C和指针-编程练习
- C++,类的拷贝构造函数,类的申明和实现分开写, 语法练习
- 递归练习之 a 到 b 的整数和 (c/c++)
- C/C++编程小练习 计算10亿之内的素数和(合数筛选算法实现)
- 关于C指针和数据类型的测试
- 编写测试简单的服务器和客户端 (C++)(十一)
- 你该相信谁?
- 使用Carbide.vs与VS.NET2003构建Symbian开发平台(s60)
- 数据库连接串大全
- The value for the useBean class attribute XXX is invalid
- CoreJava Day 6
- 练习C/C++基本功(列表和迭代器的实现和测试)
- 试验C++构造函数,析构函数,拷贝构造函数和赋值构造函数
- 一个VSTF程序集测试管理工具
- 引一篇讲什么是多线程的
- WEB服务器版本运行不兼容的问题解决方案
- 李开复:21世纪7种人才最抢手
- WEB服务器运行的不是ASP.NET1.1版
- (转)专家称中国房地产的问题越来越严重
- 文件上传例程