caicaicaic


私信TA

用户名:cai2

访问量:1252

签 名:

等  级
排  名 49703
经  验 253
参赛次数 0
文章发表 1
年  龄 0
在职情况 学生
学  校 清华大学
专  业

  自我简介:

TA的其他文章

解题思路:先创建链表,再删除,比较简单

注意事项:指针

参考代码:

#include <stdio.h> 

#include <stdlib.h>

#include <string.h>

typedef struct List{

int num;

struct List* next;

}List;

//创建链表

List* creat_List(int n)

{

    List* head=malloc(sizeof(List));

    head->num=-1;

    head->next=NULL;

    int i; List* tmp=head;

    for(i=0;i<n;i++) 

    {

    List* newptr=malloc(sizeof(List));

    scanf("%d",&newptr->num);

    newptr->next=NULL;

    tmp->next=newptr;

    tmp=newptr;

    }

    tmp->next=NULL;

    return head;

}

//删除链表节点

List* delete_List(List* head,int m) 

{

    List *pre,*p;

    int j=0;

    p=head->next;

    while(p->next)

    {

    if(p->num!=m)

    {

    while(p->num!=m&&p->next)

    {

    pre=p;

    p=p->next;

    }

    }

    if(p->num==m)

    {

    pre->next=p->next;

    free(p);j++;

}

p=pre->next;

}

return head;

}


int main()

{

    int n;

    scanf("%d",&n);

    List* head=creat_List(n);

    List* tmp=head;

    int m,i,j=0;

    scanf("%d",&m);

    List* tmp2=delete_List(tmp,m);

    tmp2=tmp2->next;

    while(tmp2)

    {

    printf("%d ",tmp2->num);

    tmp2=tmp2->next;

}

return 0;

}


 

0.0分

6 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

若是删除的数字在最后结果还会输出
2022-07-20 20:15:06
题目说是要用数组
2022-05-10 21:07:13
  • «
  • 1
  • »