KISS(Keep It Simple, Standard)

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  10 Posts :: 0 Stories :: 24 Comments :: 0 Trackbacks

常用链接

留言簿(10)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

#ifndef _DOUBLE_H_
#define _DOUBLE_H_

template<class T>
class Double;

template<class T>
class DoubleNode
{
 friend class Double<T>;
private:
 T data;
 DoubleNode<T> *pre;
 DoubleNode<T> *next;
};

template<class T>
class Double
{
 public:
  Double();//{head=end=NULL;}
  ~Double();
  void Erase();
  void reverse();
  int GetLength()const;
  bool IsEmpty()const;
  bool Find(int k, T& x)const;
  int Search(T& x)const;
  Double<T>& Delete(int k, T& x);
  Double<T>& Insert(int k, const T& x);
  void output(ostream& out)const;
  friend ostream& operator << (ostream& out, const Double<T>& x);
 private:
  DoubleNode<T> *head;
  DoubleNode<T> *end;
  int length;
};

template<class T>
Double<T>::Double()
{
 head = new DoubleNode<T>;
 end = new DoubleNode<T>;
 head->pre = NULL;
 head->next = end;
 end->pre = head;
 end->next = NULL;

 length = 0;
}

template<class T>
Double<T>::~Double()
{
 Erase();
}

template<class T>
void Double<T>::Erase()
{
 DoubleNode<T> *current = head;
 while (current)
 {
  head = head->next;
  delete current;
  current = head;
 }
 length = 0;
}

template<class T>
int Double<T>::GetLength()const
{
 return length;
}

template<class T>
bool Double<T>::IsEmpty()const
{
 return length == 0;
}

template<class T>
bool Double<T>::Find(int k, T& x)const
{

 if (length == 0)
 {
  throw exception("DoubleNode is empty!");
 }
 else if(k<1 || k>length)
 {
  throw exception("no find the position of k");
 }

 DoubleNode<T> *current = head->next;
 for (int i=1; (i<k)&&current; ++i)
 {
  current = current->next;
 }

 if (current)
 {
  x = current->data;
  return true;
 }

 return false;
}


template<class T>
int Double<T>::Search(T& x)const
{
 int nIndex = 1;
 DoubleNode<T> *current = head->next;
 while (current && current->data != x)
 {
  ++nIndex;
  current = current->next;
 }

 if (current)
 {
  return nIndex;
 }

 return -1;
}

template<class T>
Double<T>& Double<T>::Delete(int k, T& x)
{
 if (length == 0)
 {
  throw exception("DoubleNode is empty!");
 }
 else if(k<1 || k>length)
 {
  throw exception("no find the position of k, so can't delete!");
 }

 DoubleNode<T> *current = head->next; 
 for (int i=1; (i<k)&&current; ++i)
 {
  current = current->next;
 }

 DoubleNode<T> * p = current;
 current->pre->next = current->next;
 current->next->pre = current->pre;

 x = p->data;
 delete p;
 p = NULL;
 --length;

 return *this;
}


template<class T>
Double<T>& Double<T>::Insert(int k, const T& x)
{
 if (k>=0 && k<= length)
 {
  DoubleNode<T> *newNode = new DoubleNode<T>;
  newNode->data = x;

  DoubleNode<T> *current = head;
  for (int i=0; i<k; ++i)
  {
   current = current->next;
  }

  newNode->pre = current;
  newNode->next = current->next;
  current->next->pre = newNode;
  current->next = newNode;
  
  
  ++length;
 }
 else
 {
  throw exception("no find the position of k, so can't insert!");
 }

 return *this;
}

template<class T>
void Double<T>::output(ostream& out)const
{
 DoubleNode<T> *current = head->next;
 while (current!=end)
 {
  out << current->data << " ";
  current = current->next;
 }
}

template<class T>
ostream& operator<< (ostream& out, const Double<T>& x)
{
 x.output(out);
 return out;
}

template<class T>
void Double<T>::reverse()
{
 DoubleNode<T> *p1 = head;
 DoubleNode<T> *p2 = NULL;
 DoubleNode<T> *pNode;

 while (p1 != NULL)
 {
  pNode = p1;
  pNode->pre = p1->next;
  p1 = p1->next;
  pNode->next = p2;
  p2 = pNode;
 }

 end = head;
 head = p2;
}

#endif

以上为双链表的基本操作,代码已经测试过了,可以直接用!
其中,head. end在构造函数时,New了两个对象,是为了Insert 和 Delete操作的方便!
更好的方式是:把指针和数据分开,这样head,end就可以节省存贮空间了!
方式如下:
//指针数据部分(后续指针和前驱指针)
struct Node_base
{
 Node_base *next;
 Node_base *pre;
};

//添加实际数据部分
template <class T>
struct Node : public Node_base
{
 T m_data;
};

posted on 2007-08-24 17:06 QUIRE-0216 阅读(1381) 评论(4)  编辑 收藏 引用 所属分类: C++

Feedback

# re: 双链表的代码实现 2007-08-29 11:04 帅哥
欢迎来##公司面试的人,认真阅读!  回复  更多评论
  

# re: 双链表的代码实现 2007-08-29 11:13 胡锦涛说他同意毛主席的观点!
大师搞个java版的链表吧,C++就像一摊屎一样!  回复  更多评论
  

# re: 双链表的代码实现 2007-09-02 13:43 蚂蚁终结者
双链表还是STL的list比较棒  回复  更多评论
  

# re: 双链表的代码实现 2011-03-17 14:51 leiwei
楼主,你这代码有问题啊,  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理