关于链表模版的问题

chuan_qiang 2009-02-26 11:00:37

template<class T>
class Chain {
friend ChainIterator<T>;
public:
Chain() {first = 0;}
~Chain();
bool IsEmpty() const {return first == 0;}
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k, T& x);
Chain<T>& Insert(int k, const T& x);
void Output(ostream& out) const;
private:
ChainNode<T> *first; // pointer to first node
};

template<class T>
Chain<T>::~Chain()
{// Chain destructor. Delete all nodes in chain.
ChainNode<T> *next; // next node
while (first) {
next = first->link;
delete first;
first = next;
}
}

template<class T>
int Chain<T>::Length() const
{// Return the number of elements in the chain.
ChainNode<T> *current = first;
int len = 0;
while (current) {
len++;
current = current->link;
}
return len;
}

template<class T>
bool Chain<T>::Find(int k, T& x) const
{// Set x to the k'th element in the chain.
// Return false if no k'th; return true otherwise.
if (k < 1) return false;
ChainNode<T> *current = first;
int index = 1; // index of current
while (index < k && current) {
current = current->link;
index++;
}
if (current) {x = current->data;
return true;}
return false; // no k'th element
}

template<class T>
int Chain<T>::Search(const T& x) const
{// Locate x. Return position of x if found.
// Return 0 if x not in the chain.
ChainNode<T> *current = first;
int index = 1; // index of current
while (current && current->data != x) {
current = current->link;
index++;
}
if (current) return index;
return 0;
}

template<class T>
Chain<T>& Chain<T>::Delete(int k, T& x)
{// Set x to the k'th element and delete it.
// Throw OutOfBounds exception if no k'th element.
if (k < 1 || !first)
throw OutOfBounds(); // no k'th

// p will eventually point to k'th node
ChainNode<T> *p = first;

// move p to k'th & remove from chain
if (k == 1) // p already at k'th
first = first->link; // remove
else { // use q to get to k-1'st
ChainNode<T> *q = first;
for (int index = 1; index < k - 1 && q;
index++)
q = q->link;
if (!q || !q->link)
throw OutOfBounds(); // no k'th
p = q->link; // k'th
q->link = p->link;} // remove from chain

// save k'th element and free node p
x = p->data;
delete p;
return *this;
}

template<class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{// Insert x after the k'th element.
// Throw OutOfBounds exception if no k'th element.
// Pass NoMem exception if inadequate space.
if (k < 0) throw OutOfBounds();

// p will eventually point to k'th node
ChainNode<T> *p = first;
for (int index = 1; index < k && p;
index++) // move p to k'th
p = p->link;
if (k > 0 && !p) throw OutOfBounds(); // no k'th

// insert
ChainNode<T> *y = new ChainNode<T>;
y->data = x;
if (k) {// insert after p
y->link = p->link;
p->link = y;}
else {// insert as first element
y->link = first;
first = y;}
return *this;
}

template<class T>
void Chain<T>::Output(ostream& out) const
{// Insert the chain elements into the stream out.
ChainNode<T> *current;
for (current = first; current;
current = current->link)
out << current->data << " ";
}

// overload <<
template <class T>
ostream& operator<<(ostream& out, const Chain<T>& x)
{x.Output(out); return out;}

我要问的是,Output函数中输出为什么要写成out<<current->data而不直接写cout<<current->data呢
还有就是重载<<的这个函数,既不是成员函数又不是友元函数,为什么能通过编译呀?运算符重载不是只成是成员函数或友元函数么?
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnzdgs 2009-02-26
  • 打赏
  • 举报
回复
不是只有cout才能<<的,只要重载了<<操作符,相应类型的对象都可以<<。
友元可以访问类的私有成员,如果不需要访问类的私有成员,就不需要声明为友元。
liuzxchina 2009-02-26
  • 打赏
  • 举报
回复
current->data的类型是不是与《有友元关系?
yjgx007 2009-02-26
  • 打赏
  • 举报
回复
study iostream part..
oyljerry 2009-02-26
  • 打赏
  • 举报
回复
< < 是ostream的成员函数...
out是ostream,不一定只有cout这种控制台输出,还可以有其他输出,你自己定义

19,464

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧