您的位置:首页 > 编程语言 > C语言/C++

C++链表结构简单实现

2012-12-17 14:04 706 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: