链表还没怎么看,让我情何以堪~~~~纠结的
#include <iostream>#include<conio.h>
using namespace std;
//enum bool{false,true};
struct element
{
int val;
element *next;
};
class list
{
element *elems;
public:
list(){element=0;}
~list();
virtual bool insert(int);
virtual bool deletes(int);
bool contain(int);
void print ();
};
class set:public list
{
int card;
public:
set(){card=0;}
bool insert(int);
bool deletes(int);
};
list::~list()
{
element *tmp=elems;
for(element *elem=elems;elem!=0;)
{
tmp=elem;
elem=elem->next;
delete tmp;
}
}
bool list::insert(int val)
{
element *elem=new element;
if(elem!=0)
{
elem->val=val;
elem->next=elems;
elems=elem;
return true;
}
else return false;
}
bool list::deletes(int val)
{
if(elems==0)return false;
element *tmp=elems;
if(elems->val==val)
{
elems=elems->next;
delete tmp;
return true;
}
else
for(element*elem=elems;elem->next!=0;elem=elem->next)
if(tmp=elem->next->val==val)
{
tmp=elem->next;
elem->next=tmp->next;
delete tmp;
return true;
}
return false;
}
bool list::contain(int val)
{
if(elems==0)return false;
if(elems->val==val)return true;
else
for(element*elem=elems;elem->next!=0;elem=elem->next)
if(elem->next->val==val)
return true;
return false;
}
void list::print()
{
if(elems==0) return;
for(element *elem=elems; elem!=0; elem=elem->next)
cout<<elem->val<<" ";
cout<<endl;
}
bool set::insert(int val)
{
if((1))
{
++card;
return true;
}
return false;
}
bool set::deletes(int val)
{
if(list::deletes(val))
{
(2);
return true;
}
return false;
}
int main()
{
list *ptr,list1;
list *ptr,list1;
set set1;
ptr=&list1;
ptr->insert(30);
ptr->insert(40);
ptr->insert(543);
ptr->insert(40);
ptr->print();
ptr=&set1;
ptr->insert(23);
ptr->insert(672);
ptr->insert(456);
ptr->insert(23);
ptr->print();
getch();
system("pause");
return 1;
}