C++链表结构简单实现

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zanglengyu

敲碗要饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值