向Data Structrue & Algorithm 宣战!数据结构代码库
2010-01-05 12:22
393 查看
从亚马逊买了一本《数据结构算法与应用——C++语言描述》,打算静下心来好好学习一下数据结构和算法了,这次一定要坚持到底,搞懂搞透,多写代码,做到通透。
这里存放一些实现的数据结构代码,留待后用。
线性表数组实现(公式化描述):
线性表的链表实现:
这里存放一些实现的数据结构代码,留待后用。
线性表数组实现(公式化描述):
#ifndef _LLIST_H_ #define _LLIST_H_ #include "xcept.h" #include <iostream> using namespace std; template<class T> class LinearList { public: LinearList(int MaxListSize = 10); ~LinearList(){delete []element;} bool IsEmpty(){return length==0;} int Length(){return length;} bool Find(int k,T& x) const; int Search(const T& x) const; LinearList<T>& Delete(int k,T& x); LinearList<T>& Insert(int k,const T&x); void Output(ostream& out) const; private: T* element; int MaxSize; int length; }; template <class T> LinearList<T>::LinearList(int MaxListSize) { element = new T[MaxListSize]; MaxSize = MaxListSize; length = 0; } template <class T> bool LinearList<T>::Find(int k,T& x) const { if(k<1&&k>length) return false; x = element[k-1]; return true; } template <class T> int LinearList<T>::Search(const T& x) const { for(int i = 0;i < length;i++) { if(element[i]==x) return i+1; } return 0; } template <class T> LinearList<T>& LinearList<T>::Delete(int k,T& x) { if(Find(k,x)) { for(int i = k;i < length;i++) element[i-1] = element[i]; length--; return *this; } else throw OutOfBounds(); } template <class T> LinearList<T>& LinearList<T>::Insert(int k,const T& x) { if(k<1&&k>length) throw OutOfBounds(); if(MaxSize==length) throw NoMem(); for(int i = length-1; i >= k;i--) element[i+1] = element[i]; element[k] = x; length++; return *this; } template <class T> void LinearList<T>::Output(ostream& out) const { for(int i = 0;i < length;i++) out<<element[i]<<" "; } template<class T> ostream& operator<<(ostream& out,LinearList<T>& x) { x.Output(out); return out; } #endif
线性表的链表实现:
#ifndef _CLIST_H_ #define _CLIST_H #include <iostream> #include "xcept.h" using namespace std; template <class T> class Chain; template <class T> class ChainNode { public: friend Chain<T>; private: T data; ChainNode<T>* link; }; template <class T> class Chain { public: Chain(){first = 0;} ~Chain(); bool IsEmpty(){return first==0;} int Length() const; bool Find(int k,T& x) const; int Search(const T& x) const; Chain<T>& Delete(int k,T& x); Chain<T>& Insert(int k,const T& x); void Output(ostream& out) const; private: ChainNode<T>* first; }; template <class T> Chain<T>::~Chain() { ChainNode<T>* next; while (first) { next = first->link; delete first; first = next; } } template <class T> int Chain<T>::Length() const { int len = 0; ChainNode<T>* cur = first; while (cur) { len++; cur = cur->link; } return len; } template <class T> bool Chain<T>::Find(int k,T& x) const { if (k < 1) { return false; } int pos = 1; ChainNode<T>* cur = first; while (cur&&pos<k) { pos++; cur = cur->link; } if(cur) { x = cur->data; cout<<"inside the find,the element found is "<<x<<endl; return true; } return false; } template <class T> int Chain<T>::Search(const T& x) const { ChainNode<T>* cur = first; int pos = 1; while (cur&&cur->data!=x) { pos++; cur = cur->link; } if (cur) { return pos; } return 0; } template <class T> Chain<T>& Chain<T>::Delete(int k,T& x) { if (k<1||!first) { throw OutOfBounds(); } if (k==1) { x = first->data; first = first->link; return *this; } ChainNode<T>* cur = first; ChainNode<T>* pre = first; int pos = 1; while (pos<k-1&&pre) { pos++; pre = pre->link; } if (!pre||!pre->link) { throw OutOfBounds(); } cur = pre->link; pre->link = cur->link; x = cur->data; delete cur; return *this; } template <class T> Chain<T>& Chain<T>::Insert(int k,const T& x) { if(k<0) throw OutOfBounds(); ChainNode<T>* n = new ChainNode<T>; n->data = x; if (k==0) { n->link = first; first = n; return *this; } int pos = 1; ChainNode<T>* cur = first; while(pos<k&&cur) { pos++; cur = cur->link; } if (cur) { n->link = cur->link; cur->link = n; return *this; } throw OutOfBounds(); } template <class T> void Chain<T>::Output(ostream& out) const { ChainNode<T>* cur = first; while (cur) { out<<cur->data<<" "; cur = cur->link; } out<<endl; } template <class T> ostream& operator<<(ostream& out,Chain<T>& x) { x.Output(out); return out; } #endif
相关文章推荐
- 【Data Structure & Algorithm】求1+2+…+n
- [Data Structure & Algorithm] 七大查找算法
- 【Data structure & Algorithm】把二元查找树转变成排序的双向链表
- Data Structures and algorithm analysis—1.2.4&1.2.5Modular Arithmeti&The P Word(数据结构—模数运算&P字)
- [Data Structure & Algorithm] 八大排序算法
- [Data Structure & Algorithm]Sort Algorithms
- 【DataStructure&AlgorithmInJava】Ch02-BinarySearchDemo
- [Data Structure & Algorithm] 七大查找算法
- 【Data Structure & Algorithm】 查找最小的k个元素
- Data Structures and algorithm analysis—1.2.1&1.2.2&1.2.3 exponents&logarithms&series(数据结构—指数,对数,级数)
- SQL tree data struct(3): nested set (1,12) include (2,3) & (4, 11) ...
- 【DataStructure&AlgorithmInJava】Ch02-PersonArrayDemo
- 【Data Structure & Algorithm】字符串全排列
- 树状数组套线段树 <Geometry><Discretize><Data_Struct>
- 【Data Structure & Algorithm】二叉树中和为某值的所有路径
- [Data Structure & Algorithm] 七大查找算法
- [Data Structure & Algorithm] 七大查找算法
- UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】
- UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】
- spoj ORDERSET - Order statistic set Hash+树状数组。附另一种解法,使用了policy based data structrue