#include<stdio.h>
#include<stdlib.h>
struct stu{
int num;
int scores;
struct stunext;};
struct stukiang(){
struct stu*head,*tail,p;
int num;
head=tail=NULL;
scanf("%d",&num);
while(num){
p=(struct stu)malloc(sizeof(struct stu));
scanf("%d",&p->num);
scanf("%d",&p->scores);
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
num--;}
return head;}
int main(){
struct stu *head,*o,*p1,*p2;
int n;
scanf("%d",&n);
head=kiang();
p1=head;
p2=head->next;
if(head->num==n)
{o=head;
head=head->next;
}
while(p2!=NULL)
if(p2->num==n)
{p1->next=p2->next;
break;}
else
{p1=p2;
p2=p2->next;}
while(head!=NULL)
{printf("%d %d\n",head->num,head->scores);
head=head->next;}
return 0;}
这三处均是指针,分别改为
struct stu *next;
*p;
p=(struct stu *)malloc
没问题啊 题主你用的什么测试用例
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
struct stu {
int num;
int scores;
struct stu* next;
};
struct stu* kiang()
{
struct stu* head, * tail, * p;
int num;
head = tail = NULL;
scanf("%d", &num);
while (num) {
p = (struct stu*)malloc(sizeof(struct stu));
scanf("%d", &p->num);
scanf("%d", &p->scores);
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
num--;
}
return head;
}
int main() {
struct stu* head, * o, * p1, * p2;
int n;
head = kiang();
scanf("%d", &n);//修改
p1 = NULL; //修改
p2 = head; //修改
while (p2 != NULL) //修改
{
if (p2->num == n)
{
if (!p1) { //修改
o = head;
head = head->next;
free(o);
}
else { //修改
p1->next = p2->next;
free(p2);
p2 = p1->next;
}
}
else{ //修改
p1 = p2;
p2 = p2->next;
}
}
p1 = head;
while (p1 != NULL)
{
printf("%d %d\n", p1->num, p1->scores);
p1 = p1->next;
}
return 0;
}