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

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

单链表-插入:

【样例】

从键盘依次输入某班学生的姓名和成绩并保存。然后输入某个学生的待插入位置,以及该学生的姓名和成绩。如果可以插入,则输出插入该学生后的单链表;如果不能插入,则输出NO。

【输入样例】

3
a 50
b 70
c 65
3 d 60

【输出样例】

a 50
b 70
d 60
c 65

【分析】

bool  insertNode(node*  first,int  location,string  name,int  score){
    
    
      node *s=first;
node *c;
c=new node;

for(int i=1;i<location;i++)
{
    
    
     if(s->next==NULL)
          return 0;
     s=s->next;
}

c->name=name;
c->score=score;
c->next=s->next;
s->next=c;

return 1;
}

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

在这里插入图片描述

【完整代码】

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

bool  insertNode(node*  first,int  location,string  name,int  score);
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  location,stuScore;
        string  stuName;
        cin>>location>>stuName>>stuScore;
        bool  flag=insertNode(first,location,stuName,stuScore);
        if(flag  ==  false)
                cout<<"NO"<<endl;
        else
                show(first);
        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;
        }
}
bool  insertNode(node*  first,int  location,string  name,int  score){
    
    
      node *s=first;
node *c;
c=new node;

for(int i=1;i<location;i++)
{
    
    
     if(s->next==NULL)
          return 0;
     s=s->next;
}

c->name=name;
c->score=score;
c->next=s->next;
s->next=c;

return 1;
}

单链表-删除:

【样例】

从键盘依次输入某班学生的姓名和成绩并保存。然后输入某个学生的编号,删除该学生的下一个学生。如果可以删除,则输出删除后的单链表;如果不能插入,则输出NO。

【输入样例】

3
a 50
b 70
c 65
1

【输出样例】

a 50
c 65

【分析】

bool  deleteNode(node*  first,int  location,string  &name,int  &score){
    
    
     node *s=first;
node *n;
for(int i=0;i<location;i++)
{
    
    
        if(s->next==NULL)
           return 0;
       s=s->next;
}
n=s->next;
s->next=n->next;
delete(n);
return 1; 
}

在这里插入图片描述

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

【完整代码】

#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);
bool  deleteNode(node*  first,int  location,string  &name,int  &score);
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  location,stuScore;
        string  stuName;
        cin>>location;
        bool  flag=deleteNode(first,location,stuName,stuScore);
        if(flag  ==  false)
                cout<<"NO"<<endl;
        else
                show(first);
        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;
        }
}

bool  deleteNode(node*  first,int  location,string  &name,int  &score){
    
    
     node *s=first;
node *n;
for(int i=0;i<location;i++)
{
    
    
        if(s->next==NULL)
           return 0;
       s=s->next;
}
n=s->next;
s->next=n->next;
delete(n);
return 1; 
}

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

猜你喜欢

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