基类:
#ifndef LINEARLIST_H
#define LINEARLIST_H
/*
* FileName: LinearList.h
*Creater:QianChenglong
*Date:2011/10/18
*Comments: 线性表的抽象基类
*/
template<typename T>
class LinearList{
public:
LinearList()//默认构造函数
{}
~LinearList()//析构函数
{}
virtual int Length() const=0;//求表长;
virtual int Search(const T& x)const=0;//根据值搜索位序;
virtual T getData(int i) =0;//取值;
virtual void setData(int i,const T x) =0;//赋值;
virtual bool Insert(int i,T& x) =0;//插入;
virtual bool Remove(int i,T& x) =0;//删除;
virtual bool IsEmpty() const=0;//判断表空;
virtual void output() const=0;//输出;
};
#endif
类定义:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include"LinearList.h"
template<typename T>
class LinkedList:public LinearList<T>
{
private:
struct ListNode;
ListNode *head,*tail;
int length;
public:
LinkedList();//构造函数
~LinkedList();//析构函数
ListNode* Locate(int i);//返回指向第i-1个节点的指针;
int Length() const;//求表长
int Search(const T& x) const;
T getData(int i);
void setData(int i,const T x);
bool PushBack(const T& x);//从链表后端插入;
bool PushFront(T& x);//从链表前端插入;
bool Insert(int i,T& x);//将数据x插入指定位置;
bool Remove(int i,T& x);//删除指定位置数据;
bool IsEmpty() const;
void output() const;
};
template<typename T>
struct LinkedList<T>::ListNode{
T data;
ListNode* next;
ListNode(T const& _data);
ListNode();
};
#include"LinkedList.cpp"
#endif
类实现:
#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP
#include"LinkedList.h"
#include<iostream>
//
template<typename T>
LinkedList<T>::ListNode::ListNode(const T& _data)//嵌套类的构造函数
:data(_data),next(0)
{}
template<typename T>
LinkedList<T>::ListNode::ListNode()
:next(0)
{}
//
template<typename T>
LinkedList<T>::LinkedList()
{
tail=head=new ListNode;
length=0;
}
template<typename T>
LinkedList<T>::~LinkedList()
{
ListNode *work=head->next;//work指向第一个节点
while(work!=0)
{
head->next=work->next;
delete work;
work=head->next;
}
}
template<typename T>
bool LinkedList<T>::PushBack(const T& x)
{
ListNode *work=new ListNode(x);
if(!work) return 0;
tail->next=work;
tail=work;
++length;
return 1;
}
template<typename T>
bool LinkedList<T>::PushFront(T& x)
{
ListNode<T> *work=new ListNode<T>(x);
if(!work) return 0;
work->next=head->next;
head->next=work;
++length;
return 1;
}
template<typename T>
int LinkedList<T>::Length() const
{
return length;
}
//
template<typename T>
typename LinkedList<T>::ListNode *LinkedList<T>::Locate(int i){//嵌套依赖类型名要加上typename
ListNode* work=head;
if(i>length)
return 0;
for(int n=1;n!=i;++n)//找到第i-1个节点的指针
work=work->next;
return work;
}
template<typename T>
bool LinkedList<T>::Insert(int i,T& x)
{
ListNode* newNode=new ListNode(x);
if(!newNode) return 0;
ListNode* work=Locate(i);
newNode->next=work->next;
work->next=newNode;
++length;
return 1;
}
template<typename T>
bool LinkedList<T>::Remove(int i,T& x)
{
ListNode* work=Locate(i);
ListNode* removedNode=work->next;
x=removedNode->data;
work->next=work->next->next;
delete removedNode;
--length;
return 1;
}
template<typename T>
int LinkedList<T>::Search(const T& x) const
{
ListNode* work=head->next;
int i=1;
while(work->data!=x)
{
work=work->next;
++i;
}
if(work->data!=x)
return 0;
else
return i;
}
template<typename T>
T LinkedList<T>::getData(int i)
{
ListNode* work=Locate(i+1);//为什么加了const后就不行?
return work->data;
}
template<typename T>
void LinkedList<T>::setData(int i,const T x)
{
ListNode* work=Locate(i+1);
work->data=x;
}
template<typename T>
bool LinkedList<T>::IsEmpty() const
{
return length==0;
}
template<typename T>
void LinkedList<T>::output() const
{
ListNode* work=head->next;
while(work)
{
std::cout<<work->data<<' ';
work=work->next;
}
std::cout<<std::endl;
}
#endif
功能测试:
#include<iostream>
#include"LinkedList.h"
int main()
{
LinkedList<int> list;
for(int i=0;i!=10;++i)
list.PushBack(i);
list.output();
std::cout<<list.Length()<<std::endl
<<list.IsEmpty()<<std::endl
<<list.Search(5)<<std::endl;
int x;
list.Remove(3,x);
list.output();
std::cout<<x<<std::endl;
std::cout<<list.getData(3)<<std::endl;
list.setData(3,4);
list.output();
std::cout<<list.getData(3)<<std::endl;
return 0;
}