C++模板,高手支招

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:38:39
#include<iostream.h>
#include<stdlib.h>

template<class T>class LIST{
private:
struct mylist{
T data;
mylist *next;
}*head,*tail;

int count;

public:
LIST(){
tail=head=new mylist;
head->next=NULL;
count=0;
}
~LIST(){
for(mylist *p=head,head=head->next;p;p=head,head=head->next)
delete p;
}
bool addtohead(T data){
mylist *p=new list;
p->data=data;
p->next=head->next;
head->next=p;
if(!head->next)tail=p;
count++;
return true;
}
void show(){
mylist *p=head->next;
for(;p;p=p->next)
cout<<p->data<<'\t';
}
};

void main(){

LIST<int> p;
int i=0;
for(i=0;i<10000;i++)
p.addtohead(i);

p.show();

}
上面程序编译总是出错

#include<iostream.h>
#include<stdlib.h>

template<class T>class LIST{
private:
struct mylist{
T data;
mylist *next;
}*head,*tail;

int count;

public:
LIST(){
tail=head=new mylist;
head->next=NULL;
count=0;
}
~LIST(){
mylist *p = head, *t= head->next;
while(p)
{
delete p;
p = t;

if (t)
t = t->next;
}

// for(mylist *p=head,head=head->next;p;p=head,head=head->next)
// delete p;
}
bool addtohead(T data){
////////////////////////////////////
mylist *p=new mylist;
p->data=data;
p->next=head->next;
head->next=p;
if(!head->next)tail=p;
count++;
return true;
}
void show(){
mylist *p=head->next;
for(;p;p=p->next)