链表是c++数据结构中重要的一节。下面通过一个程序来说明如何定义链表以及如何操控链表。
程序如下:
#ifndef LISTNODE_H
#define LISTNODE_H
template<class NODETYPE> class List;
template<class NODETYPE>
class ListNode
{
friend class List<NODETYPE > ;
public:
ListNode(const NODETYPE &);
NODETYPE getData() const;
private:
NODETYPE data;
ListNode<NODETYPE> *nextPtr;
};
template<class NODETYPE>
ListNode<NODETYPE>::ListNode(const NODETYPE &info):data(info),nextPtr(0)
{
}
template<class NODETYPE>
NODETYPE ListNode<NODETYPE>::getData() const
{
return data;
}
#endif
以上是ListNode.h的定义
#ifndef LIST_H
#define LIST_H
#include<iostream>
using std::cout;
#include<new>
#include "ListNode.h"
template<class NODETYPE>
class List
{
public:
List();
~List();
void insertAtFront(const NODETYPE & );
void insertAtBack(const NODETYPE &);
bool removeFromFront( NODETYPE &);
bool removeFromBack( NODETYPE &);
bool isEmpty() const;
void print() const;
private:
ListNode<NODETYPE> *firstPtr;
ListNode<NODETYPE> *lastPtr;
ListNode<NODETYPE> *getNewNode(const NODETYPE &);
};
template<class NODETYPE>
List<NODETYPE>::List():firstPtr(0),lastPtr(0)
{
}
template<class NODETYPE>
List<NODETYPE>::~List()
{
if(!isEmpty())
{
cout<<"Destroying nodes ...\n";
ListNode<NODETYPE> *currentPtr = firstPtr;
ListNode<NODETYPE> *tempPtr;
while(currentPtr != 0)
{
tempPtr = currentPtr;
cout<<tempPtr->data<<"\n";
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
cout<<"All nodes destroyed\n";
}
template<class NODETYPE>
void List<NODETYPE>::insertAtFront(const NODETYPE &value )
{
ListNode<NODETYPE> *newPtr = getNewNode(value);
if(isEmpty())
firstPtr = lastPtr = newPtr;
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
template<class NODETYPE>
void List<NODETYPE>::insertAtBack(const NODETYPE &value )
{
ListNode<NODETYPE> *newPtr = getNewNode(value);
if(isEmpty())
firstPtr = lastPtr = newPtr;
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
template<class NODETYPE>
bool List<NODETYPE>::removeFromFront( NODETYPE &value )
{
if(isEmpty())
return false;
else
{
ListNode< NODETYPE> *tempPtr = firstPtr;
if(firstPtr == lastPtr)
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr ;
value = tempPtr->data;
delete tempPtr;
return true;
}
}
template<class NODETYPE>
bool List<NODETYPE>::removeFromBack( NODETYPE &value )
{
if(isEmpty())
return false;
else
{
ListNode< NODETYPE> *tempPtr = lastPtr;
if(firstPtr == lastPtr)
firstPtr = lastPtr = 0;
else
{
ListNode< NODETYPE> *currentPtr = firstPtr;
while(currentPtr->nextPtr != lastPtr)
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}
value = tempPtr->data;
delete tempPtr;
return true;
}
}
template<class NODETYPE>
bool List<NODETYPE>::isEmpty() const
{
return firstPtr == 0;
}
template<class NODETYPE>
ListNode<NODETYPE> *List<NODETYPE>::getNewNode(const NODETYPE &value)
{
return new ListNode<NODETYPE>(value);
}
template<class NODETYPE>
void List<NODETYPE>::print() const
{
if(isEmpty())
{
cout<<"The list is empty\n\n";
return ;
}
ListNode<NODETYPE> *currentPtr = firstPtr;
cout<<"The list is ";
while(currentPtr != 0)
{
cout<<currentPtr -> data<<" ";
currentPtr = currentPtr -> nextPtr;
}
cout<<"\n\n";
}
#endif
以上是list.h的定义
#include<iostream>
using std::cin;
using std::endl;
#include<string>
using std::string;
#include "list.h"
template<class T>
void testList(List<T> &listObject,const string &typeName)
{
cout<<"Testing a list of "<<typeName<<"value\n";
instructions();
int chioce;
T value;
do
{
cout<<"?";
cin>>chioce;
switch(chioce)
{
case 1:
cout<<"Enter "<<typeName<<" :";
cin>>value;
listObject.insertAtFront(value);
listObject.print();
break;
case 2:
cout<<"Enter "<<typeName<<" :";
cin>>value;
listObject.insertAtBack(value);
listObject.print();
break;
case 3:
if( listObject.removeFromFront(value))
cout<<value<<"removed from list\n";
listObject.print();
break;
case 4:
if( listObject.removeFromBack(value))
cout<<value<<"removed from list\n";
listObject.print();
break;
}
}while(chioce != 5);
cout<<"End list test\n\n";
}
void instructions()
{
cout<<"Enter one of the following :\n"
<<"1 to insert at begging of list\n"
<<"2 to insert at the end of list\n"
<<"3 to delete from beginning of list\n"
<<"4 to delete from end of list\n"
<<"5 to end list processing\n";
}
int main()
{
List<int> integerList;
testList(integerList,"integer");
List<double> doubleList;
testList(doubleList,"double");
return 0;
}
以上是主函数的定义。
程序运行的结果是:
该程序使用了两个类模板,在每个List对象中封了一个NodeListd对象的链表。
注意:
新节点连接成员赋空值,指针在使用前初始化。