数据结构系列之线性表(间接寻址)


#ifndef INDIRECTLIST_H_
#define INDIRECTLIST_H_

#include <iostream>
using namespace std;

template <class T> class IndirectIterator;
template <class T>
class IndirectList
{
 friend IndirectIterator<T>;
public:
 IndirectList(int nMaxSize = 10);
 ~IndirectList();
 bool IsEmpty() const { return m_nLength==0; };
 int Length() const { return m_nLength; };
 bool Find(int nIndex,T& nValue) const;
 int Search(const T& nValue) const;
 IndirectList<T>& Delete(int nIndex,T& nValue);
 IndirectList<T>& Insert(int nIndex,const T& nValue);
 void Output(ostream& out) const;
 int SearchHalf(const T& nValue) const;
 void InsertSort();
 void BubbleSort();
protected:
private:
 T **m_Table;
 int m_nMaxSize;
 int m_nLength;
};

template <class T>
class IndirectIterator
{
public:
 T* Initialize(const IndirectList<T>& ilValue)
 {
  m_cnLocation = ilValue.m_Table[0];
  m_nLength = ilValue.m_nLength;
  if (m_cnLocation) {
   m_nCurrent = 1;
   return m_cnLocation;
  }  
  return 0;
 }
 T* Next(const IndirectList<T>& ilValue){
  if (!m_cnLocation) {
   return 0;
  }
  m_nCurrent++;
  if (m_nCurrent<=m_nLength) {
   m_cnLocation = ilValue.m_Table[m_nCurrent-1];
  }else{
   return 0;
  }
  if (m_cnLocation) {
   return m_cnLocation;
  }
  return 0;
 }
protected:
private:
 T* m_cnLocation;
 int m_nLength;
 int m_nCurrent;
};

#endif

 

 

#include <iostream>
#include "IndirectList.h"
#include "xcept.h"


/************************************************************************
Function:构造函数
Parameter:
Return:
************************************************************************/
template <class T>
IndirectList<T>::IndirectList(int nMaxSize /* = 10 */)
{
 m_nMaxSize = nMaxSize;
 m_Table = new T *[m_nMaxSize];
 m_nLength = 0;
}
/************************************************************************
Function:析构函数
Parameter:
Return:
************************************************************************/
template <class T>
IndirectList<T>::~IndirectList()
{
 int i;
 for (i= 0;i < m_nLength; i++) {
  delete m_Table[i];
 }
 delete [] m_Table;
}
/************************************************************************
Function:按索引nIndex查找
Parameter:
Return:
************************************************************************/
template <class T>
bool IndirectList<T>::Find(int nIndex,T& nValue) const
{
 if (nIndex>m_nLength||nIndex<1) {
  return false;
 }
 nValue = *m_Table[nIndex-1];
 return true;
}
/************************************************************************
Function:按索引nIndex删除元素
Parameter:
Return:
************************************************************************/
template <class T>
IndirectList<T>& IndirectList<T>::Delete(int nIndex,T& nValue)
{
 int i;
 if (Find(nIndex,nValue)) {
  delete m_Table[nIndex - 1];
  m_Table[nIndex - 1] = 0;
  for (i = nIndex;i < m_nLength; i++) {
   m_Table[i-1] = m_Table[i];
  }
  m_nLength--;
  return *this;
 }else{
  throw OutOfBounds();
 }
}
/************************************************************************
Function:按索引nIndex插入元素
Parameter:
Return:
************************************************************************/
template <class T>
IndirectList<T>& IndirectList<T>::Insert(int nIndex,const T& nValue)
{
 int i;
 if (nIndex < 0||nIndex > m_nLength) {
  throw OutOfBounds();
 }
 if (nIndex==m_nMaxSize) {
  throw NoMem();
 }
 for (i = m_nLength - 1;i >= nIndex; i--) {
  m_Table[i+1] = m_Table[i];
 }
 m_Table[nIndex] = new T;
 *m_Table[nIndex] = nValue;
 m_nLength++;
 return *this;
}
/************************************************************************
Function:按值查找,并返回索引
Parameter:
Return:
************************************************************************/
template <class T>
int IndirectList<T>::Search(const T& nValue) const
{
 int nIndex = 0;
 T *temp;
 for (nIndex = 0;nIndex < m_nLength; nIndex++) {
  temp = m_Table[nIndex];
  if (*temp==nValue) {
   nIndex++;
   break;
  }
 }
 return nIndex;
}
/************************************************************************
Function:递增线性表,折半搜索
Parameter:
Return:
************************************************************************/
template <class T>
int IndirectList<T>::SearchHalf(const T& nValue) const
{
 int nLeft,nRight,nMiddle;
 nLeft = 0;
 nRight = m_nLength-1;
 nMiddle = m_nLength/2;
 while (*m_Table[nMiddle]!=nValue) {
  if (*m_Table[nMiddle]<nValue) {
   nLeft = nMiddle;
   nMiddle = (nLeft + nRight + 1)/2;
  }else{
   nRight = nMiddle;
   nMiddle = (nLeft + nRight)/2;
  }
 }
 if (((nMiddle==0)||(nMiddle==m_nLength-1))&&((*m_Table[nMiddle])!=nValue)) {
  throw OutOfBounds();
 }
 nMiddle++;
 return nMiddle;
}
/************************************************************************
Function:插入排序
Parameter:
Return:
************************************************************************/
template <class T>
void IndirectList<T>::InsertSort()
{
 int i,j;
 T* temp;
 for (i = 1;i < m_nLength; i++) {
  temp = m_Table[i];
  for (j = i-1;(j >= 0)&&(*m_Table[j] > *temp); j--) {
   m_Table[j+1] = m_Table[j];
  }
  m_Table[j+1] = temp;
 }
}
/************************************************************************
Function:冒泡排序
Parameter:
Return:
************************************************************************/
template <class T>
void IndirectList<T>::BubbleSort()
{
 int i,j;
 T *temp;
 bool swapped = false;//交换标记
 for (i = m_nLength;i > 1; i--) {
  swapped = false;
  for (j = 0;j < i-1; j++) {
   if (*m_Table[j] > *m_Table[j+1]) {
    swapped = true;
    temp = m_Table[j];
    m_Table[j] = m_Table[j+1];
    m_Table[j+1] = temp;
   }
  }
  if (swapped==false) {
   break;
  }
 }
}
/************************************************************************
Function:输出
Parameter:
Return:
************************************************************************/
template <class T>
void IndirectList<T>::Output(ostream& out) const
{
 int i = 0;
 for (i = 0;i < m_nLength; i++) {
  out << *m_Table[i] << " ";
 }
}
/************************************************************************
Function:
Parameter:
Return:
************************************************************************/
template <class T>
ostream& operator<<(ostream& out,const IndirectList<T>& ilvalue)
{
 ilvalue.Output(out);
 return out;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值