模板——链表模板、有序链表模板及测试

//==============================================================================

//链表模板《C++编程——数据结构与程序设计方法》16.2作为抽象数据类型的链表

//Header File:linkedList.h
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H

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

template<class Type>
class linkedListType
{
public:
 const linkedListType<Type>& operator =(const linkedListType<Type>&);
 //对于含有指针数据成员的类,必须明确重载赋值运算符。
 void initializeList();
 bool isEmptyList();
 bool isFullList();
 void print();
 int length();
 void destroyList();
 void retrieveFirst(Type& firstElement);
 void search(const Type& searchItem);
 void insertFirst(const Type& newItem);
 void insertLast(const Type& newItem);
 void deleteNode(const Type& deleteItem);
 linkedListType();
 linkedListType(const linkedListType<Type>& otherList);//拷贝构造函数
 ~linkedListType();
protected:
 nodeType<Type>* first;//数据成员是protected,而不是private.这是由于要从这个类中派生其他类。
 nodeType<Type>* last;
};

template<class Type>
bool linkedListType<Type>::isEmptyList()
{
 return(first==NULL);
}

template<class Type>
bool linkedListType<Type>::isFullList()
{
 return false;
}

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

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

template<class Type>
void linkedListType<Type>::initializeList()
{
 destroyList();
}

template<class Type>
void linkedListType<Type>::print()
{
 nodeType<Type>* current;
 current=first;
 while(current!=NULL)
 {
  cout<<current->info<<" ";
  current=current->link;
 }
}

//链表的长度
template<class Type>
int linkedListType<Type>::length()
{
 int count=0;
 nodeType<Type>* current;
 current=first;
 while(current!=NULL)
 {
  count++;
  current=current->link;
 }
 return count;
}

//检索第一个节点中的数据
template<class Type>
void linkedListType<Type>::retrieveFirst(Type& firstElement)
{
 firstElement=first->info;
}

//查找链表
template<class Type>
void linkedListType<Type>::search(const Type& item)
{
 nodeType<Type>* current;
 bool found;

 if(first==NULL)
  cout<<"Cannot search an empty list. "<<endl;
 else
 {
  current=first;
  found=false;
  while(!found&&current!=NULL)
   if(current->info==item)
    found=true;
   else
    current=current->link;
  if(found)
   cout<<"Item is found in the list. "<<endl;
  else
   cout<<"Item is not in the list."<<endl;
 }
}

//插入第一个节点
template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
 nodeType<Type>* newNode;

newNode=new nodeType<Type>;
 newNode->info=newItem;
 newNode->link=first;
 first=newNode;

 if(last==NULL)
  last=newNode;
}
//插入最后一个节点
template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
 nodeType<Type>* newNode;
 newNode=new nodeType<Type>;
 newNode->info=newItem;
 newNode->link=NULL;

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

//删除节点
template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
 nodeType<Type>* current;
 nodeType<Type>* trailCurrent;
 bool found;

 if(first==NULL)
  cout<<"Cannot delete from an empty list./n";
 else
 {
  if(first->info==deleItem)
  {
   current=first;
   first=first->link;
   if(frist==NULL)
    last=NULL;
   delete current;
  }
  else
  {
   found=false;
   trailCurrent=first;
   current=first->link;

   while(!found&&(current!=NULL))
   {
    if(current->info!=deleteItem)
    {
     trailCurrent=current;
     current=current->link;
    }
    else
     found=true;
   }
   if(found)
   {
    trailCurrent->link=current->link;
    if(last=current)
     last=trailCurrent;
    delete current;
   }
   else
    cout<<"Item to be deleted is not in the list."<<endl;
  }
 }
}

 

//析构函数
template<class Type>
linkedListType<Type>::~linkedListType()
{
 nodeType<Type>* temp;

 while(first!=NULL)
 {
  temp=first;
  first=first->link;
  delete temp;
 }
 last=NULL;
}

//拷贝构造函数
template<class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& otherList)
//形参是对象的常引用,在函数中不能改变实参的值
{
 nodeType<Type>* newNOde;
 nodeType<Type>* current;

 if(otherList.First==NULL)
 {
  first=NULL;
  last=NULL;
 }
 else
 {
  //拷贝第一个节点
  current=otherList.first;
  first=new nodeType<Type>;
  first->info=current->info;
  first->Link=NULL;
  last=first;

  current=current->link;
  while(current!=NULL)
  {
   newNode=new nodeType<Type>;
   newNode->info=current->info;
   newNode->link=NULL;

   last->link=newNode;
   current=current->link;
  }
 }
}

//重载赋值运算符
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator =(const linkedListType<Type>& otherList)
{
 nodeType<Type>* newNode;
 nodeType<Type>* current;

 if(this!=&otherList)//避免自拷贝
 {
  if(first!=NULL)
   destroyList();
  if(otherList.first==NULL)//如果otherList是空
  {
   first=NULL;
   last=NULL;
  }
  else
  {
   //拷贝第一个节点
   current=otherList.first;
   first=new nodeType<Type>;
   first->info=current->info;
   first->link=NULL;
   last=first;

   current=current->link;
   while(current!=NULL)
   { 
    newNode=new nodeType<Type>;
    newNode->info=current->info;
    newNode->link=NULL;

    last->link=newNode;
    last=newNode;
    current=current->link;
   }
  }
 }
 return *this;
}

#endif

//===========================================================================

//有序链表模板——《C++编程——数据结构与程序设计方法》16.3有序链表
//Header File:orderedList.h
#ifndef _ORDEREDLIST_H
#define _ORDEREDLIST_H

#include <iostream>
#include "linkedList.h"

using namespace std;
//有序链表通常进行下面的操作
//1.初始化链表
//2.检查链表是否为空
//3.检查甸表是否为满
//4.打印链表
//5.逆向打印链表
//6.删除链表
//7.在链表中查找给定的元素
//8.在链表中插入一个元素
//9.在链表中删除一个元素
//10.查找链表长度
//11。拷贝链表
//有序链表与一般链表很类似,可以从类linkedListType派生
//需要改变查找、插入和删除操作的算法
template<class Type>
class orderedLinkedListType:public linkedListType<Type>
{
 public:
  void search(const Type& item);//查找
  void insertNode(const Type& newItem);//插入
  void deleteNode(const Type& deleteitem);//删除
  void printListReverse() const;//逆向打印,常成员函数,只能引用本类中的数据成员,而不能修改它们。
 private:
  void reversePrint(nodeType<Type> *current) const;
};

//查找节点
template<class Type>
void orderedLinkedListType<Type>::search(const Type& item)
{
 bool found;
 nodeType<Type>* current;
 
 found=false;
 current=first;

 if(first==NULL)
  cout<<"Cannot search an empty list. "<<endl;
 else
 {
  while(current!=NULL&&!found)
   if(current->info>=item)//有序链表,找到比要找的数大的,就不用再找了
    found=true;
   else
    current=current->link;
  if(current==NULL)
   cout<<"Item is not in the List"<<endl;
  else
   if(current->info==item)
    cout<<"Item is found in the list"<<endl;
   else
    cout<<"Item is not in the list"<<endl;
 }
}

//插入节点
template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newitem)
{
 nodeType<Type>* current;
 nodeType<Type>* trailCurrent;
 nodeType<Type>* newNode;
 bool found;

 newNode=new nodeType<Type>;
 newNode->info=newitem;
 newNode->link=NULL;

 if(first==NULL)
  first=newNode;
 else
 {
  current=first;
  found=false;

  while(current!=NULL&&!found)
   if(current->info>=newitem)
    found=true;
   else
   {
    trailCurrent=current;
    current=current->link;
   }
  if(current==first)
  {
   newNode->link=first;
   first=newNode;
  }
  else
  {
   trailCurrent->link=newNode;
   newNode->link=current;
  }
 }
}

//删除节点
template<class Type>
void orderedLinkedListType<Type>::deleteNode(const Type& deleteItem)
{
 nodeType<Type>* current;
 nodeType<Type>* trailCurrent;
 bool found;

 if(first==NULL)
  cout<<"Cannot delete from an empty List."<<endl;
 else
 {
  current=first;
  found=false;

  while(current!=NULL&&!found)
   if(current->info>=deleteItem)
    found=true;
   else
   {
    trailCurrent=current;
    current=current->link;
   }
  if(current==NULL)
   cout<<"Item to be delete is not in the list."<<endl;
  else
   if(current->info==deleteItem)
   {
    if(first==current)
    {
     first=first->link;
     delete current;
    }
    else
    {
     trailCurrent->link=current->link;
     delete current;
    }
   }
   else
    cout<<"Item to be delete is not in the list."<<endl;
 }
}

//逆向打印链表
template<class Type>
void orderedLinkedListType<Type>::printListReverse() const
{
 reversePrint(first);
 cout<<endl;
}

template<class Type>
void orderedLinkedListType<Type>::reversePrint(nodeType<Type>* current) const
{
 if(current!=NULL)
 {
  reversePrint(current->link);
  cout<<current->info<<" ";
 }
}

#endif

 

//========================================================================

//测试程序,测试有序链表中的各种操作《C++编程——数据结构与程序设计方法》16.3有序链表
//实现文件:testOrderedList.cpp
#include <iostream>
#include "orderedList.h"

using namespace std;

int main()
{
 orderedLinkedListType<int> list1,list2;
 int num;

 cout<<"Line 3:Enter integers ending with -999"<<endl;
 cin>>num;

 while(num!=-999)
 {
  list1.insertNode(num);
  cin>>num;
 }

 cout<<endl;

 cout<<"Line 9:List 1 : ";
 list1.print();
 cout<<endl;

 cout<<"Line 12: List 1 in the reverse order: "<<endl;
 list1.printListReverse();
 cout<<endl;

 list2=list1;

 cout<<"Line 16: List 2: ";
 list2.print();
 cout<<endl;

 cout<<"Line 19:Enter the number to be deleted: ";
 cin>>num;
 cout<<endl;

 list2.deleteNode(num);

 cout<<"Line 23: After deleteing the onde, List 2: "<<endl;
 list2.print();
 cout<<endl;

 return 0;
}

 

 

 

面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
NUDT程序设计模拟测试 1. 请创建一个数据类型为T的链表模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值