Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510162
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2009-09-16 11:21:10

这段代码是我从网上复制过来的, 有一位仁兄说它没有错误, 但是我发现一个明显的错误:

throw exception("..")是有问题的,首先代码中根本就没有一个exception的类, 再说了

因为没有包含, 怎么可以使用它来进行报出异常,再说exception类根本就

没有string作为参数的的构造函数, 还有就是ostream, 不叫std::ostream, 根本

就没有办法编译。不知道说得对不对。。

#ifndef _DOUBLE_H_
#define _DOUBLE_H_

template
class Double;

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

template
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& Delete(int k, T& x);
  Double& Insert(int k, const T& x);
  void output(ostream& out)const;
  friend ostream& operator << (ostream& out, const Double& x);
 private:
  DoubleNode *head;
  DoubleNode *end;
  int length;
};

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

 length = 0;
}

template
Double::~Double()
{
 Erase();
}

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

template
int Double::GetLength()const
{
 return length;
}

template
bool Double::IsEmpty()const
{
 return length == 0;
}

template
bool Double::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 *current = head->next;
 for (int i=1; (i {
  current = current->next;
 }

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

 return false;
}


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

 if (current)
 {
  return nIndex;
 }

 return -1;
}

template
Double& Double::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 *current = head->next; 
 for (int i=1; (i {
  current = current->next;
 }

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

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

 return *this;
}


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

  DoubleNode *current = head;
  for (int i=0; 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
void Double::output(ostream& out)const
{
 DoubleNode *current = head->next;
 while (current!=end)
 {
  out << current->data << " ";
  current = current->next;
 }
}

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

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

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

 end = head;
 head = p2;
}

#endif

阅读(849) | 评论(0) | 转发(0) |
0

上一篇:我自己写的list

下一篇:list修正版

给主人留下些什么吧!~~