#include <iostream>
using namespace std;
//const int NULL=0;
struct LNode
{
int data;
LNode* next;
};
class Listfirst
{
public:
Listfirst();
~Listfirst();
void Creat_list(int n);
int Search_data(int e)const;
void print()const;
private:
LNode* head;
};
Listfirst::Listfirst()
{
head=new LNode;
head->next=0;
}
Listfirst::~Listfirst()
{
LNode* p;
while(head->next!=0)
{
p=head;
head=head->next;
delete p;
}
}
void Listfirst::Creat_list(int n)
{
LNode *p,*s;
p=head;
for(int i=0;i<n;++i)
{
s=new LNode;
cin>>s->data;
s->next=p->next;
p->next=s;
p=s;
}
}
int Listfirst::Search_data(int e)const
{
LNode* p;
int i=0;
p=head->next;
while(p)
{
if(p->data==e)
++i;
p=p->next;
}
return i;
}
void Listfirst::print()const
{
LNode* p;
p=head->next;
while(p!=0)
{
cout<<p->data<<" ";
p=p->next;
}
}
int main()
{
Listfirst Lis;
Lis.Creat_list(3);
Lis.print();
cout<<Lis.Search_data(1)<<endl;
system("pause");
return 0;
}
C++链表结构简单实现
最新推荐文章于 2021-05-16 20:43:18 发布