[原创]自己的库+四则运算测试
主要为了学习数据结构,建个自己的库,日后不断更新.....代码很垃圾,有兴趣的朋友可以试一下,bug应该不少///
/********************************************************
** 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) throw();
stack(const stack<type>& source_stack) throw();
~stack() throw();
//main function
int size() const;
bool empty() const;
void push(const type& value) throw(error_stack);
void pop() throw(error_stack) ;
const type& top() const throw(error_stack);
//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) throw(): _top( -1)
{
_stack = new type[ _size=size ];
}
template<typename type>
stack<type>::stack(const stack<type>& source_stack) throw()
{
_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() throw()
{
delete [] _stack;
}
// main function
template<typename type>
inline
void stack<type>::push(const type& value) throw(error_stack)
{
if( ++_top >= _size ) throw error_stack("OVERFLOW");
_stack[ _top ] = value;
}
template<typename type>
inline
void stack<type>::pop() throw(error_stack)
{
if( empty() ) throw error_stack("DWONFLOW");
--_top;
}
template<typename type>
inline
const type& stack<type>::top() const throw(error_stack)
{
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>
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
[[it] 本帖最后由 中学者 于 2008-5-10 13:09 编辑 [/it]]