徐世龙


私信TA

用户名:dotcpp0681143

访问量:364

签 名:

等  级
排  名 7789
经  验 1233
参赛次数 0
文章发表 12
年  龄 0
在职情况 学生
学  校 xdu
专  业

  自我简介:

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
    int data;
    struct Node* next;
}Node;
void AddAtEnd(Node**head, int data){
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = NULL;

    if(*head ==NULL)
    {
        *head = new_node;
        return;
    }
    Node* cur = *head;
    while(cur->next!=NULL)
    {
        cur = cur->next;
    }
    cur->next = new_node;
}

void deleteNode(Node** head,int a){
    if(*head == NULL)
    return; //链表为空无需删除

    else if((*head)->next == NULL&&(*head)->data == a)//光棍头节点恰好被全删了
    {
        *head = NULL;
        return;
    }
    Node* cur = *head;
    Node* prev = NULL;
    while(cur->next!=NULL)
    {
        prev = cur;
        cur = cur->next;
        if(cur->data == a)
        {
            prev->next = cur->next;
            return;
        }
    }
}

void showNode(Node* head){
    if(head == NULL)
    {
        printf("空链表!");
        return;
    }
   

    Node* cur = head;
    while(cur!=NULL)
    {
        printf("%d ",cur->data);
        cur = cur->next; //cur肯定非空,如果cur是最后一个,这句就会给cur赋值NULL,下一轮就不会继续循环了
    }
    printf("\n");
}
int main(){
    int n,n1,m;
    Node* head = NULL;
    scanf("%d",&n);
    n1=n;
    while (n--)
    {
        scanf("%d",&m);
        AddAtEnd(&head,m);
    }
    int a;
    scanf("%d",&a);
    while(n1--)
    deleteNode(&head,a);
    showNode(head);
    return 0;
}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区