#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;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!