全部博文(158)
分类: C/C++
2009-09-16 11:21:10
这段代码是我从网上复制过来的, 有一位仁兄说它没有错误, 但是我发现一个明显的错误:
throw 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
DoubleNode
};
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
Double
void output(ostream& out)const;
friend ostream& operator << (ostream& out, const Double
private:
DoubleNode
DoubleNode
int length;
};
template
Double
{
head = new DoubleNode
end = new DoubleNode
head->pre = NULL;
head->next = end;
end->pre = head;
end->next = NULL;
length = 0;
}
template
Double
{
Erase();
}
template
void Double
{
DoubleNode
while (current)
{
head = head->next;
delete current;
current = head;
}
length = 0;
}
template
int Double
{
return length;
}
template
bool Double
{
return length == 0;
}
template
bool Double
{
if (length == 0)
{
throw exception("DoubleNode is empty!");
}
else if(k<1 || k>length)
{
throw exception("no find the position of k");
}
DoubleNode
for (int i=1; (i
current = current->next;
}
if (current)
{
x = current->data;
return true;
}
return false;
}
template
int Double
{
int nIndex = 1;
DoubleNode
while (current && current->data != x)
{
++nIndex;
current = current->next;
}
if (current)
{
return nIndex;
}
return -1;
}
template
Double
{
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
for (int i=1; (i
current = current->next;
}
DoubleNode
current->pre->next = current->next;
current->next->pre = current->pre;
x = p->data;
delete p;
p = NULL;
--length;
return *this;
}
template
Double
{
if (k>=0 && k<= length)
{
DoubleNode
newNode->data = x;
DoubleNode
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
{
DoubleNode
while (current!=end)
{
out << current->data << " ";
current = current->next;
}
}
template
ostream& operator<< (ostream& out, const Double
{
x.output(out);
return out;
}
template
void Double
{
DoubleNode
DoubleNode
DoubleNode
while (p1 != NULL)
{
pNode = p1;
pNode->pre = p1->next;
p1 = p1->next;
pNode->next = p2;
p2 = pNode;
}
end = head;
head = p2;
}
#endif