要求实现一个函数,删除单链表的第i个结点

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

typedef struct ListNode {
int num;
struct ListNode *next;
}Node;

Node *createlist();
Node *deletelink(Node *head, int i);
void display(Node *head);

int main(void)
{
Node *head;
int i;
head = createlist();
scanf("%d",&i);
head = deletelink(head, i);
display(head);
return 0;
}

Node *deletelink(Node *head, int i)
{
    int j=0;
    if (i < 1){
        printf("error\n");
        return head;
    }
    Node *p = head,*pre = NULL;
    while(p && j < i - 1){
        pre = p;
        p = p->next;
        j++;
    }
    if (p == head){
        head = p->next;
        free(p);
    }
    else if (p == NULL){
        printf("error\n");
    }
    else {
        pre->next = p->next;
        free(p);
    }
    return head;
}


您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632