- 論壇徽章:
- 0
|
本帖最后由 nOneee_ 于 2014-03-19 23:47 編輯
template<class T>
class Link{//鏈表結點定義
public:
T data;
Link<T>*next;
Link(const T x,const Link<T>*nextValue=NULL){
data=x;
next=nextValue;
}
Link(const Link<T>*nextValue){
next=nextValue;
}
};
template<class T>
class InkList:public Link<T>{//定義鏈表類型
private:
Link<T>*head,*tail;
public:
InkList();//構造
~InkList();//析構
int input(const T x);//輸入
int invert();
void output();//輸出
};
template<class T>//構造
InkList<T>::InkList( ){
head=tail=new Link<T>;
}
template<class T>//析構
InkList<T>::~InkList(){
Link<T>*tmp;
while(head!=NULL)
{
tmp=head;
head=head->next;
delete tmp;
}
}
template<class T>//輸入
int InkList<T>::input(const T x)
{
Link<T>*q;
q=new Link<T>;
q->data=x;
if(!q)
return 0;
q->next=NULL;
tail->next=q;
tail=q;
return 1;
}
template<class T>
int InkList<T>::invert()
{
Link<T>*p,*q;
p=head->next;
head->next=NULL;
while(p!=NULL)
{
r=p->next;
p->next=head->next;
head->next=p;
p=r;
}
}
template<class T>//輸出
void InkList<T>: utput()
{
Link<T>*q;
q=head->next;
while(q!=NULL)
{
cout<<q->data;
q=q->next;
}
cout<<endl;
}
===========================================
#include<iostream>
#include"8.h"
using namespace std;
void main()
{
InkList<int>list;
int x;
cout<<"請輸入需逆置的鏈表"<<endl;
cin>>x;
list.input(x);
list.invert();
list.output();
}
===========================================
j:\數(shù)據(jù)結構\ex2\8.h(32) : error C2512: 'Link<int>' : no appropriate default constructor available
c:\program files (x86)\microsoft visual studio\vc98\include\xstring(79) : while compiling class-template member function '__thiscall InkList<int>::InkList<int>(void)'
j:\數(shù)據(jù)結構\ex2\8.h(33) : error C2512: 'Link<int>' : no appropriate default constructor available
c:\program files (x86)\microsoft visual studio\vc98\include\xstring(79) : while compiling class-template member function '__thiscall InkList<int>::InkList<int>(void)'
j:\數(shù)據(jù)結構\ex2\8.h(51) : error C2512: 'Link<int>' : no appropriate default constructor available
c:\program files (x86)\microsoft visual studio\vc98\include\xstring(79) : while compiling class-template member function 'int __thiscall InkList<int>::input(const int)'
|
|