自己的数据结构库
以学习为目的,创建自己的库,以后不断更新。代码很垃圾,日后会渐渐完善.....有兴趣的朋友可以测试一下//
程序代码:
/********************************************************
** Highlight software by yzfy(雨中飞燕) http://yzfy.org *
*********************************************************/
#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
** Highlight software by yzfy(雨中飞燕) http://yzfy.org *
*********************************************************/
#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
----------------解决方案--------------------------------------------------------