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

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

猜你喜欢

转载自blog.csdn.net/qq_53293179/article/details/116945458