博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
别人写的双链表
阅读量:4199 次
发布时间:2019-05-26

本文共 3432 字,大约阅读时间需要 11 分钟。

 

#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;
}

转载地址:http://uxuli.baihongyu.com/

你可能感兴趣的文章
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>
HTML5学习之——HTML 5 服务器发送事件
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
解决Rhythmbox乱码
查看>>
豆瓣爱问共享资料插件发布啦
查看>>
kermit的安装和配置
查看>>
linux中cat命令使用详解
查看>>
java中的异常机制
查看>>
商务智能-基本方法-数据钻取
查看>>
openstack-instance-high-availability-Evacuate
查看>>
evacuate-instance-automatically
查看>>
pycharm常用设置(keymap设置及eclipse常用快捷键总结)
查看>>
关于在openstack的环境变量.bashrc自定自己简化命令
查看>>
Openstack Heat Project介绍(转)
查看>>
How to Perform an Upgrade from Icehouse to Juno(ice升级到juno)
查看>>