| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 669 人关注过本帖
标题:自己的数据结构库
只看楼主 加入收藏
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
结帖率:33.33%
收藏
 问题点数:0 回复次数:0 
自己的数据结构库
以学习为目的,创建自己的库,以后不断更新。代码很垃圾,日后会渐渐完善.....
有兴趣的朋友可以测试一下//
程序代码:
/******************************************************** ** Highlight software by yzfy(雨中飞燕) http:// * *********************************************************/ #ifndef MyDS_H_ #define MyDS_H_ #include<string> //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // // application: to support some programing by this. The header file included // some basic and important ADT. For example, stack, linkStack, queue, deque // linkQueue, Slink, Clink, Dlink, Max(Min)Heap, BST, RBT, AVL, Splay, BTree, BHeap // FibHeap etc. // // author: taetrie (c) All Rights Reserved // // created: 2008/5/9 // //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/ class error_stack; template<typename type> class stack { public: //ctor and dtor explicit stack(int size = 50) ; stack(const stack<type>& source_stack) ; ~stack() ; //main function int size() const; bool empty() const; void push(const type& value); void pop() ; const type& top() const; //overload operator stack& operator=(const stack<type>& source_stack); template<typename type_> friend bool operator==(const stack<type_>& s1,const stack<type_>& s2); template<typename type_> friend bool operator> (const stack<type_>& s1,const stack<type_>& s2); template<typename type_> friend bool operator< (const stack<type_>& s1,const stack<type_>& s2); private: //copy function void copy_(const stack<type>& source_stack); //data member type* _stack; int _size; int _top; }; //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/ // // define error_stack class error_stack { public: explicit error_stack(const char* msg ):str(msg){} ~error_stack() {} const std::string& what() const { return str; } private: std::string str; }; //\ define stack template<typename type> inline int stack<type>::size() const { return _size; } template<typename type> inline bool stack<type>::empty() const { return -1 == _top ? true : false; } // copy_ function template<typename type> void stack<type>::copy_(const stack<type>& source_stack ) { for(int i = source_stack._top; i>=0; --i ) _stack[i] = source_stack._stack[i]; } // ctor of stack template<typename type> stack<type>::stack(int size): _top( -1) { _stack = new type[ _size=size ]; } template<typename type> stack<type>::stack(const stack<type>& source_stack) { _stack = new type[ _size=source_stack._size ]; if( source_stack.empty() ) { _top = -1; } else { _top = source_stack._top; copy_( source_stack ); } } template<typename type> stack<type>::~stack() { delete [] _stack; } // main function template<typename type> void stack<type>::push(const type& value) { if( ++_top >= _size ) throw error_stack("OVERFLOW"); _stack[ _top ] = value; } template<typename type> void stack<type>::pop() { if( empty() ) throw error_stack("DWONFLOW"); --_top; } template<typename type> const type& stack<type>::top() const { if( empty() ) throw error_stack("EMPTY"); return _stack[ _top ]; } // overload operator // use all of element in the stack to compare template<typename type> stack<type>& stack<type>::operator= (const stack<type>& source_stack) { if( this != &source_stack ) { delete [] _stack; _top = source_stack._top; _stack = new type[ _size=source_stack._size ]; copy_( source_stack ); } return *this; } template<typename type> bool operator== (const stack<type>& s1, const stack<type>& s2) { if( s1._top == s2._top ) { for(int i = s1._top; i>=0 ; --i) if( s1._stack[i] != s2._stack[i] ) return false; return true; } return false; } template<typename type> inline bool operator!= (const stack<type>& s1, const stack<type>& s2) { return !(s1 == s2 ); } template<typename type> inline bool operator< (const stack<type>& s1, const stack<type>& s2) { if( s1._top < s2._top ) return true; else if( s1._top == s2._top ) { for(int i=s1._top; i>=0; --i) if( s1._stack[i] > s2._stack[i] ) return false; return true; } return false; } template<typename type> inline bool operator> (const stack<type>& s1, const stack<type>& s2) { return !(s1 < s2); } template<typename type> inline bool operator<= (const stack<type>& s1, const stack<type>& s2) { return !(s1 > s2 ); } template<typename type> inline bool operator>= (const stack<type>& s1, const stack<type>& s2) { return !(s1 < s2 ); } #endif
搜索更多相关主题的帖子: 数据结构 MyDS yzfy 垃圾 兴趣 
2008-05-10 01:11
快速回复:自己的数据结构库
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020986 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved