C:单链表的查找(超详细图解)

C: 单链表的构建(超详细图解)
C: 单链表的插删(超详细图解)

求表长:

【样例】

从键盘依次输入某班学生的姓名和成绩并保存。输出表长。

【输入样例】

3
a 50
b 70
c 65

【输出样例】

3

【分析】

int  getLength(node  *  first)   //求链表表长函数
{
      node *s=first;
	  int n=0;
	  while(s->next!=NULL)
	  {
		n++;
		s=s->next;
	  }
	  return n;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

【完整代码】

#include  <iostream>
using  namespace  std;
typedef  struct  node{
        string  name;
        int  score;
        node*  next;
}node;

node*  create(node*  head,string  name[],int  score[],int  len);
int  getLength(node*  first);

int  main()
{
        int  n;
        cin>>n;
        string  name[n];
        int  score[n];
        node*  first=NULL;
        for(int  i=0;i<n;i++)
                cin>>name[i]>>score[i];
        first=create(first,name,score,n);
        cout<<getLength(first)<<endl;
        return  0;
}

node*  create(node*  first,string  name[],int  score[],int  len){
        first  =  new  node;
        first->next  =  NULL;
        node*  rear=first;
        for(int  i=0;i<len;i++){
                node*  s=  new  node;
                s->name=name[i];
                s->score=score[i];
                s->next=NULL;
                rear->next=s;
                rear=s;
        }
        return  first;
}
int  getLength(node  *  first)   //求链表表长函数{
      node *s=first;
	  int n=0;
	  while(s->next!=NULL)
	  {
		n++;
		s=s->next;
	  }
	  return n;
}

按位查找:

【样例】

从键盘依次输入某班学生的姓名和成绩并保存,然后输入待查找学生的序号。如果该学生存在,程序输出该学生的姓名和成绩;如果不存在,程序输出NO。

【输入样例1】

3
a 50
b 70
c 65
2

【输出样例1】

b 70

【输入样例2】

3
a 50
b 70
c 65
5

【输出样例1】

NO

【分析】

node*  get(node  *  first,int  num)
{
      node *s=first;
	  for(int i=0;i<num;i++)
	  {
       		if(s!=NULL)
           	  s=s->next;
     	    else
          	   return 0;
	  }
	  return s;
}
样例1

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

样例2

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

【完整代码】

#include  <iostream>
using  namespace  std;
typedef  struct  node{
        string  name;
        int  score;
        node*  next;
}node;

node*  create(node*  head,string  name[],int  score[],int  len);
node*  get(node  *  first,int  num);
int  main()
{
        int  n;
        cin>>n;
        string  name[n];
        int  score[n];
        node*  first=NULL;
        for(int  i=0;i<n;i++)
                cin>>name[i]>>score[i];
        first=create(first,name,score,n);
        int  num;
        cin  >>  num;
        node*  p  =  get(first,num);
        if(p  ==  NULL)
                cout<<"NO"<<endl;
        else
                cout<<p->name<<"  "<<p->score<<endl;
        return  0;
}
node*  create(node*  first,string  name[],int  score[],int  len){
        first  =  new  node;
        first->next  =  NULL;
        node*  rear=first;
        for(int  i=0;i<len;i++){
                node*  s=  new  node;
                s->name=name[i];
                s->score=score[i];
                s->next=NULL;
                rear->next=s;
                rear=s;
        }
        return  first;
}
node*  get(node  *  first,int  num)
{
     node *s=first;
for(int i=0;i<num;i++)
{
         if(s!=NULL)
             s=s->next;
         else
             return s;
}
return s;

}

按值查找位置:

【样例】

从键盘依次输入某班学生的姓名和成绩并保存。然后输入待查找学生的名字。如果该学生存在,程序输出该学生在单链表中的位置;如果不存在,程序输出NO。

【输入样例】

3
a 50
b 70
c 65
b

【输出样例】

2

【分析】

int  getLocation(node*  first,string  name)
{
      int count=0;
node *s=first;
while(s)
{ 
         if(s->name==name) 
         {
             return count;
         }  
         count++;
         s=s->next;
}
return 0;
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

【完整代码】

#include  <iostream>
using  namespace  std;
typedef  struct  node{
        string  name;
        int  score;
        node*  next;
}node;

node*  create(node*  head,string  name[],int  score[],int  len);
void  show(node*  first);
int  getLocation(node*  first,string  name);
int  main()
{
        int  n;
        cin>>n;
        string  name[n];
        int  score[n];
        node*  first=NULL;
        for(int  i=0;i<n;i++)
                cin>>name[i]>>score[i];
        first=create(first,name,score,n);
        //show(first);
        string  stuName;
        cin>>stuName;
        int  location  =  getLocation(first,stuName);
        if(location  ==  0)
                cout<<"NO"<<endl;
        else
                cout<<location<<endl;
        return  0;
}
node*  create(node*  first,string  name[],int  score[],int  len){
        first  =  new  node;
        first->next  =  NULL;
        node*  rear=first;
        for(int  i=0;i<len;i++){
                node*  s=  new  node;
                s->name=name[i];
                s->score=score[i];
                s->next=NULL;
                rear->next=s;
                rear=s;
        }
        return  first;
}

void  show(node  *  first){
        node*  p  =  first->next;
        while(p  !=  NULL){
                  cout<<p->name<<"  "<<p->score<<endl;
                  p=p->next;
        }
}
int  getLocation(node*  first,string  name)
{
      int count=0;
node *s=first;
while(s)
{ 
         if(s->name==name) 
         {
             return count;
         }  
         count++;
         s=s->next;
}
return 0;
}

如果对你有帮助的话就点个赞吧 ( ๑ŏ ﹏ ŏ๑ )

  • 15
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值