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

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

C: 单链表的构建

头插法:

【样例】

对某班学生成绩排序。从键盘依次输入某班学生的姓名和成绩(一个班级人数最多不超过50人)并保存,然后按照输入顺序的反序输出每个学生的姓名和成绩。

【输入样例】

4
aaa 50
bbb 70
ccc 65
ddd 90

【输出样例】

ddd 90
ccc 65
bbb 70
aaa 50

【分析】

●构建头指针和普通指针

node *p,*first;
first=new node;
first->next=NULL;
p=new node;

●赋值

p->name=name;
p->score=score;

●把节点的next指向头指针的next

 p->next=first->next;

在这里插入图片描述

●把头指针的next指向节点

first->next=p

在这里插入图片描述

●循环这个过程

for(int i=0;i<len;i++)
{
    
    
        p=new node;
        p->name=name[i];
        p->score=score[i];
        p->next=first->next;
        first->next=p
}

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

【完整代码】

#include  <iostream>
#include<stdio.h>
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  main()
{
    
    
        int  n;
        cin>>n;
        string  name[n];
        int  score[n];
        node*  first;
        for(int  i=0;i<n;i++)
                cin>>name[i]>>score[i];
        first=create(first,name,score,n);
        show(first);
        return  0;
}

node*  create(node*  first,string  name[],int  score[],int  len)
{
    
    
        node *p;
first=new node;
first->next=NULL;
for(int i=0;i<len;i++)
{
    
    
         p=new node;
         p->name=name[i];
         p->score=score[i];
          p->next=first->next;
         first->next=p
}
        return  first;
}

void  show(node  *  first)
{
    
    
        node*  p  =  first->next;
        while(p  !=  NULL){
    
    
                  cout<<p->name<<"  "<<p->score<<endl;
                  p=p->next;
        }
}

尾插法:

【样例】

对某班学生成绩排序。从键盘依次输入某班学生的姓名和成绩(一个班级人数最多不超过50人)并保存,然后按照输入顺序输出每个学生的姓名和成绩。

【输入样例】

4
aaa 50
bbb 70
ccc 65
ddd 90

【输出样例】

aaa 50
bbb 70
ccc 65
ddd 90

【分析】

●构建头指针,尾指针和节点

node *end,*p,*first;
first=new node;
p=new node;

●将尾指针指向头指针

 end=first;

在这里插入图片描述

●赋值

p->name=name;
p->score=score;

●把尾指针的next指向节点

end->next=p;

在这里插入图片描述

●把尾指针指向节点

end=p;

在这里插入图片描述

●循环这个过程

node *end,*p,*first;
first=new node;
   end=first;
   int j;
   for(j=0;j<len;j++)
   {
    
    
           p=new node;
           p->name=name[j];
           p->score=score[j];
           end->next=p;
           end=p;
   }

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

在这里插入图片描述

●最后加NULL

  end->next=NULL;

在这里插入图片描述

【完整代码】

 #include  <iostream>
#include<stdio.h>
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  main()
{
    
    
        int  n;
        cin>>n;
        string  name[n];
        int  score[n];
        node*  first;
        for(int  i=0;i<n;i++)
                cin>>name[i]>>score[i];
        first=create(first,name,score,n);
        show(first);
        return  0;
}
node*  create(node*  first,string  name[],int  score[],int  len)
{
    
    
           node *end,*p;
first=new node;
   end=first;
   int j;
   for(j=0;j<len;j++)
   {
    
    
           p=new node;
           p->name=name[j];
           p->score=score[j];
           end->next=p;
           end=p;
   }
   end->next=NULL;

        return  first;
}
void  show(node  *  first){
    
    
        node*  p  =  first->next;
        while(p  !=  NULL){
    
    
                  cout<<p->name<<"  "<<p->score<<endl;
                  p=p->next;
        }
}

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

猜你喜欢

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