#include <iostream>
using namespace std;
typedef struct Node
{
int m_data;
Node *m_next;
}Node;
class IntList
{
public:
IntList();
~IntList();
bool append(int value);
bool remove(int value);
void printList();
Node *find(int value);
private:
Node *m_head;
};
IntList::IntList()//构造
{
m_head = new Node;//空头结点
m_head->m_next = NULL;
}
IntList::~IntList()//析构
{
Node *p = m_head;
while(p!=NULL)
{
Node *temp = p;
p = p->m_next;
delete temp;
}
}
bool IntList::append(int value)//尾部添加
{
Node * temp = new Node;
if(temp==NULL)
{
return false;
}
Node *p = m_head;
while(p->m_next!=NULL)
{
p = p->m_next;
}
temp->m_data = value;
temp->m_next = p->m_next;
p->m_next = temp;
return true;
}
bool IntList::remove(int value)//删除
{
Node * pre = m_head;
Node * p = m_head->m_next;
if(p == NULL)
return false;
while(p->m_data!=value&&p->m_next!=NULL)
{
pre = p;
p = p->m_next;
}
if(p->m_data == value)
{
pre ->m_next = p->m_next;
delete p;
}
else
{
return false;
}
return true;
}
void IntList::printList()//打印
{
Node *p = m_head->m_next;
while(p!=NULL)
{
cout<<p->m_data<<' ';
p = p->m_next;
}
cout<<endl;
}
Node *IntList::find(int value)//查找
{
Node *p = m_head->m_next;
if(p==NULL)
{
return NULL;
}
while(p->m_data!= value&&p->m_next!=NULL)
{
p = p->m_next;
}
if(p->m_data == value)
{
return p;
}
else
{
return NULL;
}
}
int main(int argc, char* argv[])
{
IntList L;
L.append(10);
L.append(13);
L.append(8);
L.append(6);
L.printList();
L.remove(8);
L.printList();
Node * f = L.find(10);
if(f==NULL)
{
cout<<"NULL"<<endl;
}
else
{
cout<<f<<'\t'<<f->m_data<<endl;
}
return 0;
}