S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

写了个双向链表

Posted on 2009-04-05 23:43 S.l.e!ep.¢% 阅读(1927) 评论(3)  编辑 收藏 引用 所属分类: Data Struct

#include<iostream.h>

template<class Type>
struct nodeType
{  
 Type info;
 nodeType<Type>  *link;
 nodeType<Type>  *back;
};

template<class Type>
class doublelist
{
public:
 doublelist();
 // doublelist(const doublelist<Type>& otherlist);
 const doublelist<Type>&operator=(const doublelist<Type>&otherlist);

 virtual ~doublelist();

 void initializelist();
    void destory();

 bool isEmptylist();
 void print();
 void rprint();
 int  length();

    void insertItem(const Type& insertItem);
 void deleteItem(const Type& deleteItem);

private:
    nodeType<Type>  *first;
};

template<class Type>
const doublelist<Type>& doublelist<Type>::operator=(const doublelist<Type>& otherlist)
{  
    if(first != NULL)
 { 
  destory();
 }

 if(this != &otherlist)
 { 
  if(otherlist.first == NULL)
  {
   first = NULL;
  }
  else
  {  
   nodeType<Type>* pointer = otherlist.first;
   nodeType<Type>* this_pointer = first;
   
            while( pointer != NULL )
   {
    nodeType<Type>* newNode = new nodeType<Type>;
    newNode->info = pointer->info;
    newNode->link = NULL;
    newNode->back = NULL;

    if( this_pointer == NULL )
    {
     this_pointer = first = newNode;
     newNode->back = NULL;
     newNode->link = NULL;
    }
    else
    {
     newNode->back = this_pointer;
     newNode->link = NULL;

     this_pointer->link = newNode;
     this_pointer = this_pointer->link;
    }        
    
    pointer = pointer->link;
   }   
  }
 }

 return *this;
}

template<class Type>
doublelist<Type>::~doublelist()
{
 nodeType<Type>  *temp;
   
 while( first != NULL)
 {  
  temp  = first;
  first = first->link;
  delete temp;
 }
 
 //cout<<"析构函数被调用"<<endl;
}

template<class Type>
doublelist<Type>::doublelist()
{    
 first = NULL;
}

template<class Type>
void doublelist<Type>::initializelist()

 doublelist<Type>::destory();
}

template<class Type>
bool doublelist<Type>::isEmptylist()

 return(first == NULL);
}

template<class Type>
void  doublelist<Type>::destory()
{
 nodeType<Type> *temp;
 
 while( first != NULL)
 {
  temp  = first;
  first = first->link;
  delete temp;
 }
}

template<class Type>
void doublelist<Type>::print()

 nodeType<Type> *current;
 current=first;
 while(current!=NULL)
 {
  cout<<current->info<<" ";
        current=current->link;
 }
}

template<class Type>
void doublelist<Type>::rprint()

 nodeType<Type> *current = first;
 while(current!=NULL && current->link!= NULL)
  current = current->link;

 while(current!=NULL)
 {
  cout<<current->info<<" ";
        current=current->back;
 }
}

template<class Type>
int doublelist<Type>::length()
{  
 int count=0;
 nodeType<Type> *current;
 current=first;
 while(current!=NULL)
 {
  count++;
  current=current->link;
 }
 return count;
}

template<class Type>
void doublelist<Type>::insertItem(const Type& insertItem)
{
 nodeType<Type>* pointer = first;
 bool bFind = false;

 while( pointer != NULL )
 {
  if( pointer->info == insertItem )
  {
   bFind = true;
   break;
  }
  else
  {
   pointer = pointer->link;
  }
 }

 if( bFind )
 {
  cout << insertItem << " have already exist!!" << endl;
 }
 else
 {
  nodeType<Type>* newnode = new nodeType<Type>();

  if( first == NULL )
  {
   first = newnode;
      newnode->back = NULL;
  }
  else
  {
   pointer = first;
   while( pointer->link != NULL )
    pointer = pointer->link;

   pointer->link = newnode;
   newnode->back = pointer;
  }

  newnode->link = NULL;
  newnode->info = insertItem;
 }
}

void main()
{
 doublelist<int> b,c;
 
 int num = 0, i = 0, j = 0;
 cout<<"please input the count of number:"<<endl;
 cin>>num;

 while(i < num)
 {
  cout<<"please input the number:";
  cin >> j;
  cout<<endl;
  b.insertItem(j);
  i++;
 }

 cout << "b normal: ";
    b.print();
 cout << endl;

 c=b;
 
 cout << "normal: ";
 c.print();
 cout << endl;
 
 cout << "not normal: ";
 c.rprint();
 cout << endl;
}

Feedback

# re: 写了个双向链表  回复  更多评论   

2009-04-06 00:16 by 陈梓瀚(vczh)
你的接口表达出来的不是一个链表,而是一个集合。而且好像还没办法从doublelist<T>里面访问那些内容……

# re: 写了个双向链表  回复  更多评论   

2009-04-06 09:16 by S.l.e!ep.¢%
不好意思,楼上的,被你看穿了,我会改进的。

# re: 写了个双向链表  回复  更多评论   

2009-04-06 23:59 by wZt
双向链表似乎没有很复杂 即使你想写双向列表也不用这么多代码。。

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