题解 | #牛牛的链表删除#

牛牛的链表删除

https://www.nowcoder.com/practice/d3df844baa8a4c139e103ca1b1faae0f

#include <stdio.h>
#include<stdlib.h>

typedef int data_t;
typedef struct node
{
    data_t data;
    struct node *next;
}linknode,*linklist;

linklist list_create()
{
    linklist H = (linklist)malloc(sizeof(linknode));
    if(H == NULL)
    {
        printf("malloc is failed");
        return NULL;
    }
    H->data = 0;
    H->next = NULL;
    return H;
}

void list_insert(linklist H)
{
     linklist L = (linklist)malloc(sizeof(linknode));
     linklist h = H;
    if(L == NULL)
    {
        printf("malloc is failed");
        return ;
    }
    int value;
    scanf("%d",&value);
    L->data = value;
    L->next = NULL;
    while(h->next != NULL)
    h = h->next;

    h->next = L;
}

void list_show(linklist H)
{
    linklist h = H->next;
    while (h->next != NULL) {
    printf("%d ",h->data);
    h = h->next;
    }
    printf("%d ",h->data);
}

void list_delete(linklist H,int value)
{
linklist h = H;
linklist h1;
while (h->next != NULL) {
    if(h->next->data == value &&h->next->next!=NULL)
    {
        linklist h2 = h->next;
        h->next = h->next->next;
        free(h2);
    }
    h1 = h;
    h = h->next;
}
   if(h1->next->data == value)
   h1->next = NULL;
}


int main() {
    int i,n,m;
    linklist H = list_create();
    scanf("%d %d",&n,&m);
  for(i = 0;i<n;i++)
  list_insert(H);
 list_delete(H,m);
  list_show(H);
    return 0;
}

全部评论

相关推荐

勤奋努力的椰子这就开摆:美团骑手在美团工作没毛病
投递美团等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务