您的位置:首页 > 理论基础 > 数据结构算法

向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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息