#include "stdafx.h"
#include<stdio.h>
#include <iostream>
using namespace std;
template <class T> class Link //定义单链表结点
{
public:
T data;
Link<T> *next;
Link(const T info,const Link<T> *nextValue = NULL)
{
data=info;
next = nextValue;
}
Link(const Link<T> *nextValue)
{
next=nextValue;
}
};
template <class T>
class List {
void clear(); //置空线性表
bool isEmpty(); //线性表为空时,返回true //在表尾添加一个元素value,表的长度增1
bool insert(const int p, const T value); //在位置p上插入一个元素value,表的长度增1
bool delete1(const int p); //删除位置p上的元素,表的长度减 1
bool getPos(int & p, const T value); //查找值为value的元素并返回其位置
bool getValue(const int p, T& value); //把位置p的元素值返回到变量value中
bool setValue(const int p, const T value); //用value修改位置p的元素值
int length(T& value); //求表长
void display(); //显示表的内容
bool append(const T value); //追加
};
template<class T> class lnkList:public List<T>
{
private:
Link<T> *head,*tail;
Link<T> *setPos(const int i)
{
int count=0;
if(i==-1)
{
return head;
}
p=p->next;
while(p!=NULL&&count<i)
{
p=p->next;
count++;
}
return p;
}
public:
lnkList(){ //构造函数
head=tail=new Link<T>; //创建头结点,head和tail指向该头结点
head->next=NULL; //头结点的指针域为空
}
~lnkList()//finished
{
Link<T>* tmp;
while(head!=NULL)
{
temp=head;
head=head->next;
delete tmp;
}
}
bool isEmpty()
{
if((head->next)!=NULL)
return true;
}
void clear()
{
if(head->next==NULL)
{
cout<<"表未创建,清除表失败"<<endl;
}
Link<T>* tmp;
while(head!=NULL)
{
temp=head;
head=head->next;
delete tmp;
}
}
int length()//finished
{
Link<T> *p;
int n=0;
if((head->next)==NULL)
return 0;
p=head->next;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
bool append(const T value)
{
Link<T> *p,*q;
p==tail;
q=new Link<T>;
q->data=value;
p->next=q;
q=tail;
return true;
}
bool insert(const int i,const T value)
{
Link<T> *p,*q;
if((p=setPos(i-1))==NULL)
{
cout<<"非法插入点"<<endl;
return false;
}
q=new Link<T>(value,p->next);
p->next=q;//一般情况
if(p==tail)//特殊情况
{
q=tail;
}
return true;
}
bool delete1(const int i)
{
Link<T> *p,*q;
if((p=setPos(i-1)==NULL)||p==tail)
{
cout<<"非法删除点"<<endl;
return false;
}
q=p->next;
if(q==tail)
{
tail=p;
p->next=NULL;
delete q;
}
p->next=q-next;
delete q;
return true;
}
bool getValue(const int p,T&value)
{
Link<T> *p;
int count=0;
if(i==-1)
{
cout<<"此处为头结点,获取值失败"<<endl;
return false;
}
p=head->next;
while(p!=NULL&&count<i)
{
p=p->next;
count++;
}
value = p->data;
return true;
}
bool getPos(int &i,const T value)
{
i=0;
Link<T> *p;
if(head->next==NULL)
{
cout<<"表为空,无法获取位置"<<endl;
return false;
}
p=head->next;
while((p!=NULL)&&(p->data!=value))
{
i++;
p=p->next;
}
cout<<"位置为:"<<i;
return true;
}
bool setvalue(const int i, T& value)
{
Link<T> *p;
int count=0;
if(head->next==NULL)
{
cout<<"表为空,无法修改"<<endl;
return false;
}
p=head->next;
while(p!=NULL&&count<i)
{
count++;
p=p->next;
}
p->data=value;
return true;
}
void display()
{
Link<T> *p;
p=head->next;
if(p==NULL) cout<<"表空,无法展示"<<endl;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
}
};
int main()
{
lnkList<char> lL;
int choice;
int i;
char value;
bool ok;
do{
cout<<"----------------------------"<<endl;
cout<<"请选择"<<endl;
cout<<"(0.退出 1.清除 2.显示 3.表长 4.插入)"<<endl;
cout<<"(5.追加 6.删除 7.查找 8.修改 9.定位 10.创建 11.销毁)"<<endl;
cout<<"-----------------------------"<<endl;
scanf("%d",&choice); getchar();
switch(choice)
{
case 0:
cout<<"已退出,拜拜!"<<endl;
break;
case 1:
lL.clear();
break;
case 2:
lL.display();
break;
case 3:
cout << "表长:" <<lL.length()<<endl;
break;
case 4:
cout<<"位置:"; cin>>i;
cout<<"元素:"; cin>>value;
ok=lL.insert(i,value);
if(ok==true) cout<<"插入操作成功"<<endl;
if(ok==false) cout<<"插入操作失败"<<endl;
break;
case 5:
cout<<"元素: "; cin>>value;
ok=lL.append(value);
if(ok==true) cout<<"追加操作成功!"<<endl;
else cout<<"追加失败!"<<endl;
break;
case 6:
cout<<"位置: "; cin>>i;
ok=lL.delete1(i);
if(ok==true) cout<<"删除操作成功!"<<endl;
else cout<<"删除操作失败!"<<endl;
break;
case 7:
cout<<"位置: "; cin>>i;
ok=lL.getValue(i,value );
if(ok==true) cout<<"元素值为:"<<value<<endl;
if(ok==false) cout<<"查找失败"<<endl;
break;
case 8:
cout<<"位置:"; cin>>i;
cout<<"元素:"; cin>>value;
ok=lL.setvalue(i,value );
if(ok==true) cout<<"修改操作成功"<<endl;
if(ok==true) cout<<"修改操作失败"<<endl;
break;
case 9:
cout<<"元素:"; cin>>value;
ok=lL.getPos(i,value );
if(ok==true) cout<<"元素位置为:"<<i;
if(ok==false)cout<<"元素不存在"<<endl;
break;
case 10:
break;
case 11:
break;
default:
cout<<"您的输入有误,请重新输入"<<endl;break;
}
}while(choice!=0);
return 0;
}
错误如下:
Compiling...
w.cpp
D:\c语言\w\w.cpp(59) : error C2512: 'Link<char>' : no appropriate default constructor available
D:\c语言\w\w.cpp(58) : while compiling class-template member function '__thiscall lnkList<char>::lnkList<char>(void)'
执行 cl.exe 时出错.
w.exe - 1 error(s), 0 warning(s)