在链表的类里面定义了一个指向头节点的指针 chiannode <T> *head;
但是在链表chain中的析构函数中怎么对这个指针进行定义!
链表如果不要头节点那么怎么定义这个指针!
有头节点的指针和没有头节点的指针在计算链表长度的时候这么初始化计数的变量是有0开始还是有1开始!
在双向循环链表中删除节点的时候为什么在最后要对它的head->lefe和head->right的指针附0值?而不能直接delete head;呢?
 2006-03-22 22:37
	    2006-03-22 22:37
  这么没有人回呢?我今天把源程序贴上大家给看看把!!
 这个程序回出现死循环!这是这么回事呢?
#include<iostream>
#include"01.h"
using namespace std;
void main()
{
 CirList<int> L ;
    for(int i=1;i<10;i++)
      L.insert(i,i*3);
    cout<<L;
    int y  ;
 L.Find(1,y);
    cout<<y<<endl;
 cout<<"the result of search 4: "<<L.search(4)<<endl;
 L.Delete(1,y);
 cout<<L;
 cout<<y<<endl;
    system("pause"); 
 
}
           
//循环双链表 
#include<iostream>
using namespace std;
 const int Null=0;
 template <class T>class ChainNode ;
 template <class T>
 class CirList
 { 
  public:
      CirList();
     ~CirList(); 
      bool isEmpty() const;  
      int length() const;                       //求链表的长度
      bool Find (int k,T&x) const;              //找寻第k个元素的值并把他放在x里
   int search(const T&x)const;               //找有没有x这个数有的话返回位置,没有的话返回出错
      CirList<T>& Delete(int k,T &x);           //删除k节点注意得把指针的处理放为重点
      CirList<T>& insert(int k, T x);           //注意上面两项的返回类型
      void output(ostream &out) const;           //输出这个链表
  private:
        ChainNode<T> * head;
 }; 
 
template <class T>
class ChainNode  
{  
public:
  friend class   CirList<T>;
private:
     T date;
     ChainNode<T>*left;
  ChainNode<T>*right;
};
 
 template <class T>
 CirList<T>:: CirList()//构造函数
{  
   head=new ChainNode<T>;///这样的初始化可以嘛?这个是头节点嘛?头节点是空??
   head->left=head; head->right=head;
   head->date=Null;
}
template <class T>
CirList<T>:: ~CirList()  //析构函数    
{   
   ChainNode<T>* p=head->right;
   while(head->right!=head)
   {  
   p=head->right; p->left=head->left;
   delete head;head=p;    
   }     
   head->right=Null; //为什么要附空呢?
   head->left=Null;   
}
  template<class T>
  bool CirList<T>::isEmpty() const// 判否为空
  { 
   if(head->left==head->right 
   {
    return true; 
   }
   return false;
  }
 
  template<class T>
   int CirList<T>::length() const  //计算长度
  {   
   int index=1;  
   ChainNode<T>*p=head->right;
        //注意请教这儿得初值听该为0还是1???
   while(p!=head)
   { p=p->right;
     index++;
   }
   return index;
  }
 
template<class T>
bool CirList<T>::Find(int k,T&x)const  //注意此处定义index时一定要有1开始嘛?
                                       // 要找第k个数那么跳出循环时p->link应该指向第k个数的节点位置
{
 ChainNode<T>*p=head;
    int index=1;
 while(index<k && p!=head)
 { 
  p=p->right; index++;
 }
 if(p->right!=head)
 {
  x=p->date;
     return true;
 }
 return false;
}
 
template<class T>
int CirList<T>::search(const T&x) const//查找函数
{
    ChainNode<T>*p=head->right;
    int index=1; head->date=x;
    while ( p->right!=head&&p->date!=x)
    { p=p->right;
      index++;
    }
    if(p->right!=head )
    return index;
    cout<<"没有找到要的数据"<<endl;
    return 0;
}
 
 template <class T>
 CirList<T> & CirList<T>::Delete(int k,T&x)//删除函数
 {  
 int  index=1;
  if(k<1||k!=length())
  { exit(1);}//异常处理没有编;
ChainNode <T> *p=head; 
  while(p!=head &&  index<k )
  {
 p=p->right; index++;
  }
  if( p!=head)
  { 
   p->right->left=p->right;
   p->left->right=p->left;
   x=p->date;
   delete p;
  }
  return *this;
 }
  //////***************//////////
template <class T>
void CirList<T>::output(ostream& out) const//输出函数
{
   ChainNode<T> *current;
   for(current=head->right;current->right!=head;current=current->right)
        out<<current->date<<" "; //注意<<的重载
   out<<endl;
}
template<class T>
ostream & operator<<(ostream& out,const CirList<T>& x)
{
       x.output(out);
       return out;
}
template <class T>
 CirList<T> & CirList<T>::insert(int k,T x)   //插入函数
{ 
 int index=1;
 ChainNode<T> *y=new ChainNode<T>; 
 
 y->date=x;
 ChainNode <T>*p=head->right;
  if(k<0||k>length() )                          
 {exit(1);}
  while(p !=head && index<k )
 {
  p=p->right;
  index++;
 }
 p->right->left=y;
 p->right=y;
 y->right=p->right;    
 y->left=p;
    return *this;
}
 

 2006-03-25 00:23
	    2006-03-25 00:23