(第19列)C语言:单链表删除操作,循环删除,直到不想再删除为止。

简介: (第19列)C语言:单链表删除操作,循环删除,直到不想再删除为止。

先看结果,是不是你们想要的:

我们还是一步一步的来:(完整代码在最后!)



第一步:

1、预处理命令:

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)

2、结构体类型:

struct student 
{
  int num;
  float score;
  struct student *next;
};

3、声明:

    struct student *creat();//输入函数,用于输入数据
  struct student *del(struct student *head,int sum);//删除函数,用于删除数据
  void print(struct student *head);//输出函数,用于输出数据


第二步:输入函数

struct student *creat()
{
  struct student *head,*p1,*p2;  //在函数中定义结构体变量
  p2=p1=(struct student *)malloc(LEN); //开辟新的空间
  printf("请输入学生学号、成绩:");
  scanf("%d%f",&p1->num,&p1->score); //第一次输入
  n=0;    //全局变量n 
  head=NULL; //头指针首先不指向任何数据
  while(p1->num!=0) //输入的数据为0,则停止循环
  {
    n++; //循环一次,数据就加一组,可以判断数据有多少组
    if(n==1) //第一组数据时
    {
      head=p1; //头指针指向第一组数据
    }
    else 
    {
      p2->next=p1; //否则p2的next指向下一组数据
      }
      p2=p1;  //前面是指向下一组数据的开头,现在是把下一组数据都赋值给p2
      p1=(struct student *)malloc(LEN);  //重新开辟空间
      printf("请输入学生学号、姓名:");
      scanf("%d%f",&p1->num,&p1->score); //继续输入
  }
  p2->next=NULL;  //最后一组数据的尾部为NULL
  return head;  //返回头指针
}


第三步:删除函数

struct student *del(struct student *head,int sum)
{
  struct student *p1,*p2;  //重新定义变量,与输入函数的p1,p2不同,是独立不联系的
  p2=p1=head;    //p1,p2指向链表的头部
    while(p1->num!=sum&&p1->next!=NULL) //循环查找要删除的结点
    {
      p2=p1;
      p1=p1->next;
    }
    if(p1->num==sum) //如果有要删除的结点
    {
      if(p1==head) //此结点在头部
      {
        head=p1->next; 
      }
      else //此结点在中间或尾部
      {
        p2->next=p1->next;
      }
    }
    else //没有要删除的结点
    {
      printf("未找到数据!"); 
    }
  return head; //返回更新过后的链表
}


第四步:输出

void print(struct student *h1)
{
  struct student *p;  //定义结构体类型
  p=h1;  //头指针给p
  if(h1!=NULL) //如果指针不为空
  {
    printf("\n结果为:\n");
    do
    {
      printf("\t%d\t%.2f\n",p->num,p->score);
      p=p->next;
    }while(p!=NULL); //do...while循环打印结果
  }
}


第五步:主函数main中实现无限循环

int ch;
while(1) //无限循环
  {
    printf("\n你是否需要删除数据(Y/N):");
    do
    {
      ch=getchar(); //输入单个字母(Y/N)
    }while(ch!='Y'&&ch!='N');  //如果不为指定字母,则持续输入
    if(ch=='Y') //符合yes
    {
      printf("请输入你要删除的数据:");
      scanf("%d",&sum);  //输入数据
      h1=del(h1,sum); //删除数据
      print(h1);  //打印删除后的结果
    }
    else
    {
      break;  //如果为no,则跳出循环
    }
  }

完整代码:

代码来了!

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student 
{
  int num;
  float score;
  struct student *next;
};
int n;
int main()
{
  struct student *creat();
  struct student *del(struct student *head,int sum);
  void print(struct student *head);
  struct student *h1;
  int sum;
  int ch;
  h1=creat();
  print(h1);
  while(1)
  {
    printf("\n你是否需要删除数据(Y/N):");
    do
    {
      ch=getchar();
    }while(ch!='Y'&&ch!='N');
    if(ch=='Y')
    {
      printf("请输入你要删除的数据:");
      scanf("%d",&sum);
      h1=del(h1,sum);
      print(h1);
    }
    else
    {
      break;
    }
  }
  printf("删除完毕!最终数据为:\n");
  print(h1); 
  return 0;
}
struct student *creat()
{
  struct student *head,*p1,*p2;
  p2=p1=(struct student *)malloc(LEN);
  printf("请输入学生学号、成绩:");
  scanf("%d%f",&p1->num,&p1->score);
  n=0;
  head=NULL;
  while(p1->num!=0)
  {
    n++;
    if(n==1)
    {
      head=p1;
    }
    else
    {
      p2->next=p1;
      }
      p2=p1;
      p1=(struct student *)malloc(LEN);
      printf("请输入学生学号、姓名:");
      scanf("%d%f",&p1->num,&p1->score);
  }
  p2->next=NULL;
  return head;
}
struct student *del(struct student *head,int sum)
{
  struct student *p1,*p2;
  p2=p1=head;
    while(p1->num!=sum&&p1->next!=NULL)
    {
      p2=p1;
      p1=p1->next;
    }
    if(p1->num==sum)
    {
      if(p1==head)
      {
        head=p1->next;
      }
      else
      {
        p2->next=p1->next;
      }
    }
    else
    {
      printf("未找到数据!"); 
    }
  return head;
}
void print(struct student *h1)
{
  struct student *p;
  p=h1;
  if(h1!=NULL)
  {
    printf("\n结果为:\n");
    do
    {
      printf("\t%d\t%.2f\n",p->num,p->score);
      p=p->next;
    }while(p!=NULL); 
  }
}


相关文章
|
14天前
|
C语言
爱上C语言:分支与循环(分支篇)多个if与if — else if区别
爱上C语言:分支与循环(分支篇)多个if与if — else if区别
|
1月前
|
C语言
利用C语言中的while语句实现循环
利用C语言中的while语句实现循环
18 0
|
1月前
|
C语言
C语言的循环程序
C语言的循环程序
11 0
|
2月前
|
人工智能 C语言 Python
关于c语言循环,我想说的是:
关于c语言循环,我想说的是:
|
3月前
|
存储 算法 编译器
C语言之分支与循环【附6个练习】(二)
C语言之分支与循环【附6个练习】(二)
|
3月前
|
C语言 C++
C语言之分支与循环【附6个练习】(一)
C语言之分支与循环【附6个练习】(一)
|
1月前
|
C语言
介绍c语言中的分支,循环
介绍c语言中的分支,循环
22 0
|
22天前
|
C语言
【C语言】“分⽀与循环第一章:开启创新之门,探索无尽可能性的第一篇章“2
【C语言】“分⽀与循环第一章:开启创新之门,探索无尽可能性的第一篇章“2
|
22天前
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解
|
22天前
|
存储 算法 C语言
【算法与数据结构】 C语言实现单链表队列详解1
【算法与数据结构】 C语言实现单链表队列详解