数据结构一:链表

线性链表、循环链表:

//
// created: 2009/08/05
// created: 5:8:2009   16:29
// author:  WangFei 
// purpose: 模板节点定义

//  reWrite history:2009/08/05---模板节点定义
//                  2009/08/06---修改为双链表节点
///

#if !defined(ROBOTTHINKING_LISTBASE_H__WANGFEI__INCLUDED_20090805)
#define ROBOTTHINKING_LISTBASE_H__WANGFEI__INCLUDED_20090805

#include <stdlib.h>

template<class T>
class RtCNode
{
 public:
  RtCNode<T> *front;//指向前面节点
  RtCNode<T> *back;//指向后面节点
  T data;//数据
};

#endif//ROBOTTHINKING_LISTBASE_H__WANGFEI__INCLUDED_20090805


///
// created: 2009/07/13
// created: 13:7:2009   14:14
// author:  WangFei 
// purpose: 链表队列模板类

//  reWrite history:2009/07/13---新建
//                  2009/08/05---队列头为第一个插入的节点(mvo_list指针所指示的位置),后来的插入为在队列尾部插入.
//                               队列的第一个节点为索引号为0
///
#if !defined(ROBOTTHINKING_LISTSINGLY_H__WANGFEI__INCLUDED_20090713)
#define ROBOTTHINKING_LISTSINGLY_H__WANGFEI__INCLUDED_20090713

#include "rtCListBase.h"

template<class T>
class RtCListSingly
{
 friend RtCNode<T>;
private:
 RtCNode<T> *mvo_list;//存储的地址
 long mvi_num;//长度


public:
 RtCListSingly():mvi_num(0),mvo_list(0){}
 ~RtCListSingly(){
  removeAll();
 }
 
 int insert(const T data);//插入新节点
 int getAt(int index,T &data)//得到每个位置的值
 {
  if ((index > mvi_num-1) || (0 == mvi_num))
  {
   return -1;
  }
  
  RtCNode<T> *current = NULL;
  
  //current指向应该在当前节点前的节点
  current = mvo_list;
  int i = 0;
  while(i<index)
  {
   current = current->back;
   i++;
  }
  data = current->data;
  return 0;
 }
 int remove(int index);//删除末节点
 void removeAll()//清空队列
 {
  RtCNode<T> *current;//下一个节点
  while(mvo_list)
  {
   current = mvo_list->back;
   delete mvo_list;
   mvo_list = current;
  }
  mvi_num = 0;
 }
 int length() const //队列的长度
 {
  return mvi_num;
 }
 bool empty()
 {
  return mvi_num==0?true:false;
 }
};

template<class T>
int RtCListSingly<T>::insert(const T data)
{
 //存储新节点
 RtCNode<T> *newNode = new RtCNode<T>;
 if (newNode == NULL)
 {
  return -1;
 }

 newNode->data = data;
 newNode->back = NULL;

 if (mvi_num==0)//队列为NULL
 {
  mvo_list = newNode;
 }
 else//队列已经存在节点
 {
  RtCNode<T> *current = NULL;
     current = mvo_list;
  while(current->back != NULL)
  {
   current = current->back;
  }

  current->back = newNode;
 } 

 mvi_num++;//存储节点数目

 return 0;
}

template<class T>
int RtCListSingly<T>::remove(int index)
{
 //超出队列索引号
 if ((index > mvi_num-1) || (0 == mvi_num))
 {
  return -1;
 }

 RtCNode<T> *current = mvo_list; 
 if (0==index)//索引号为起始点
 {
  mvo_list = mvo_list->back;
 }
 else//索引号大于0
 {
  RtCNode<T> *next = mvo_list;

  for (int i = 0; (i<index-1) && next;i++)
  {
   next = next->back;
  }
  current = next->back;
  next->back = current->back;
 }

 delete current;
 mvi_num--;

 return 0;
}

#endif //ROBOTTHINKING_LISTSINGLY_H__WANGFEI__INCLUDED_20090713


//
// created: 2009/08/05
// created: 5:8:2009   16:47
// author:  WangFei 
// purpose: 环形队列模板类

//  reWrite history:2009/08/05---新建
///

#if !defined(ROBOTTHINKING_LISTCIRCULAR_H__WANGFEI__INCLUDED_20090805)
#define ROBOTTHINKING_LISTCIRCULAR_H__WANGFEI__INCLUDED_20090805

#include "rtCListBase.h"

//环形队列默认大小
#if !defined(RT_CLIST_CIRCLUAR_SIZE_DEFAULT)

#define RT_CLIST_CIRCLUAR_SIZE_DEFAULT       100

#endif //RT_CLIST_CIRCLUAR_SIZE_DEFAULT

template<class T>
class RtCListCircular
{
 friend RtCNode<T>;
 private:
  RtCNode<T> *mvo_front;//存储的环形队列首地址
  RtCNode<T> *mvo_rear;//存储的环形队列末地址

     long mvi_num;//当前环形队列长度
  long mvi_size;//设置的环形队列大小
 
 public:
  RtCListCircular():mvi_num(0),mvi_size(RT_CLIST_CIRCLUAR_SIZE_DEFAULT),mvo_front(0),mvo_rear(0){
  }
  ~RtCListCircular(){}
  void setSize(const long size){//设置环形队列大小
   mvi_size = size;
  }
  int getSize() const{
   return mvi_size;
  }
  int length() const{ //队列的长度  
   return mvi_num;
  }
  bool empty(){
   return mvi_num==0?true:false;
  }
  void removeAll(){//清空队列  
   RtCNode<T> *current;//下一个节点
   int i = 0;
   while(i<mvi_num)
   {
    current = mvo_front->back;
    delete mvo_front;
    mvo_front = current;
    i++;
   }
   mvo_front = NULL;
   mvo_rear = NULL;
   mvi_num = 0;
  }

  int insert(const T data);//插入新节点
  int getAt(int index,T &data);//得到每个位置的值
  int remove(int index);//删除末节点
};

template<class T>
int RtCListCircular<T>::insert(const T data)
{
 if (mvi_size<1)//设置环形大小太小
 {
  return -1;
 }

 RtCNode<T> *current = new RtCNode<T>;//存储新节点
 if (!current)
 {
  return -1;
 }
 current->data = data;

 if (mvi_num<mvi_size)//环形队列没有存储满
 {
  switch(mvi_num)
  {
  case 0://当环形队列没有数据
   mvo_front = current;//当环形队列只有一个数据时,首尾指针都指向第一个数据
   break;
  case 1://当环形队列只有一个数据
   mvo_front->back = current;
   break;

  default:
   if ((mvi_size - 1) == mvi_num){//当存储到最一个数据时要连接一个闭环形
    mvo_front->front = current;
   }

   mvo_rear->back = current;

   break;
  }

  current->front = mvo_rear; 
  current->back = (((mvi_num + 1) == mvi_size) ? mvo_front:NULL); //当存储到最一个数据时要连接一个闭环形

  mvo_rear = current;//环形队列尾部指针指向当前新添加的数据节点

  mvi_num++;
 }
 else//环形队列存储满
 {
  RtCNode<T> *lvo_temNode = mvo_front;

  mvo_front = mvo_front->back;//队列首部为此时第二个节点

  current->back = mvo_front;//当前数据前面节点为此时队列尾部,后面节点为此时队列首部节点
  current->front = mvo_rear;

  mvo_rear->back = current;
  mvo_front->front = current;
  mvo_rear = current;

  delete lvo_temNode;//释放内存
 }

 return 0;
}

template<class T>
int RtCListCircular<T>::getAt(int index,T &data)
{
 if ((index > mvi_num-1) || (0 == mvi_num))
 {
  return -1;
 }
 
 RtCNode<T> *current = NULL;
 
 //current指向应该在当前节点前的节点
 current = mvo_front;
 int i = 0;
 while(i<index)
 {
  current = current->back;
  i++;
 }
 data = current->data;
 return 0;
}

template<class T>
int RtCListCircular<T>::remove(int index)
{
 //超出队列索引号
 if ((index > mvi_num-1) || (0 == mvi_num))
 {
  return -1;
 }
 
 RtCNode<T> *current = mvo_front; 

 if (0==index)//索引号为起始点
 {
  mvo_front = mvo_front->back;
 }
 else//索引号大于0
 {
  if (index == (mvi_size-1))//当删除最后一个节点时
  {
   current = mvo_rear;
   mvo_rear = mvo_rear->front;
  }
  else
  {
   RtCNode<T> *next = NULL;
   RtCNode<T> *last = mvo_front;
   for (int i = 0; (i<index-1);i++)
   {
    last = last->back;
   }
   
   current = last->back;
   next = current->back;
   last->back = next;
      next->front = last;
  }
 }
 mvo_rear->back = NULL;
 mvo_front->front = NULL;

 delete current;
 mvi_num--;
 
 return 0;
}

#endif//ROBOTTHINKING_LISTCIRCULAR_H__WANGFEI__INCLUDED_20090805


//
// created: 2009/08/06
// created: 6:8:2009   12:27
// author:  WangFei 
// purpose: 

//  reWrite history:2009/08/06---模板队列汇总类
///

#if !defined(ROBOTTHINKING_LIST_H__WANGFEI__INCLUDED_20090806)
#define ROBOTTHINKING_LIST_H__WANGFEI__INCLUDED_20090806

#include "rtCListCircular.h"
#include "rtCListSingly.h"

//链表队列类型
typedef enum RtListType{
 RT_SINGLY,//单链表队列
 RT_CIRCULAR//环形链表队列
}RtListType;


template<class T>
class RtCList
{
 private:
  RtCListCircular<T> mvo_circular;//环形队列
  RtCListSingly<T> mvo_singly;//单链队列
  RtListType mvo_type;//队列类型

 public:
  RtCList():mvo_type(RT_SINGLY){}//默认情况下为单链表队列

 void SetType(RtListType type){//队列类型
  mvo_type = type;
 }
 int SetSize(const long size){//设置环形队列大小
  switch(mvo_type)
  {
  case RT_SINGLY:
   return -1;
   break;
  case RT_CIRCULAR:
   mvo_circular.setSize(size);
   break;
  default:
   return -1;
  }
 }
 
 int GetSize() const{
  switch(mvo_type)
  {
  case RT_SINGLY:
   return -1;
   break;
  case RT_CIRCULAR:
   return mvo_circular.getSize();
   break;
  default:
   return -1;
  }
 }

 int Lenth() const{//队列的长度
  switch(mvo_type)
  {
  case RT_SINGLY:
   return mvo_singly.length();
   break;
  case RT_CIRCULAR:
   return mvo_circular.length();
   break;
  default:
   return -1;
  }
 }  

 
 bool Empty(){
  switch(mvo_type)
  {
  case RT_SINGLY:
   return mvo_singly.empty();
   break;
  case RT_CIRCULAR:
   return mvo_circular.empty();
   break;
  default:
   return true;
  }
 }

 void RemoveAll(){//清空队列
  switch(mvo_type)
  {
  case RT_SINGLY:
   return mvo_singly.removeAll();
   break;
  case RT_CIRCULAR:
   return mvo_circular.removeAll();
   break;
  default:
   return;
  }
 }
 
 int Insert(const T data){//插入新节点
  switch(mvo_type)
  {
  case RT_SINGLY:
   return mvo_singly.insert(data);
   break;
  case RT_CIRCULAR:
   return mvo_circular.insert(data);
   break;
  default:
   return -1;
  }
 }
 
 int GetAt(int index,T &data){//得到每个位置的值
  switch(mvo_type)
  {
  case RT_SINGLY:
   return mvo_singly.getAt(index,data);
   break;
  case RT_CIRCULAR:
   return mvo_circular.getAt(index,data);
   break;
  default:
   return -1;
  }
 }

 int Remove(int index){//删除末节点
  switch(mvo_type)
  {
  case RT_SINGLY:
   return mvo_singly.remove(index);
   break;
  case RT_CIRCULAR:
   return mvo_circular.remove(index);
   break;
  default:
   return -1;
  }
 }
  
};

#endif//ROBOTTHINKING_LIST_H__WANGFEI__INCLUDED_20090806

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值