模板——链表模板、有序链表模板及测试
//==============================================================================
//链表模板《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&¤t!=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;
}