|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
template<class T>
class list1
{
public:
list1();
void add(T&);
void remove(T&);
void printlist();
~list1();
protected:
struct node
{
node *pnext;
T data;
};
node *phead;
};
template<class T>
list1<T>::list1()
{
phead=NULL;
}
template<class T>
void list1<T>::add(T &t)
{
node *temp=new node;
temp->data=t;
temp->pnext=phead;
phead=temp;
}
template<class T>
void list1<T>::remove(T &t)
{
node *q=0;
if((phead->data)==t)
{
q=phead;
phead=phead->pnext;
}
else
{
for(node *p=phead;p->pnext;p=p->pnext)
if((p->pnext->data)==t)
{
q=p->pnext;
p->pnext=q->pnext;
break;
}
}
if(q)
{
delete q;
}
}
template<class T>
void list1<T>::printlist()
{
for(node *p=phead;p;p=p->pnext)
{
cout<<(p->data)<<" ";
}
cout<<endl;
}
template<class T>
list1<T>::list1()
{
node *p;
while(phead!=NULL)
{
p=phead;
phead=phead->pnext;
delete p;
}
}
int main()
{
list1<int>intlist;
int a;
for(int i=0;i<10;i++)
{
intlist.add(i);
}
cout<<"the int list is :";
intlist.printlist();
cout<<"please input a integer to remove:";
cin>>a;
intlist.remove(a);
cout<<"after removed :"<<endl;
cout<<"the int list is :";
intlist.printlist();
return 0;
}//不知道哪里错了,麻烦指点下。
|
|