指针的删除

来源:百度知道 编辑:UC知道 时间:2024/07/04 12:39:09
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);

scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student*head)
{
struct student*p;
printf("\nNow,these %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%d,%5.1f\n",p->num,p->score);
p=p->next;}while(p!=NULL);
}
struct student*del(struc

//程序有问题
struct student*del(struct student*head,int num)
{
    struct student*p1,*p2;
    if(head==NULL)
    {
        printf("\nthe list is null\n");
        //goto end;
        return head;
    }
    p1 = head;
    //while(num!=p1->num&&p1->next==NULL)
    while(p1 && p1->num != num)
    {
        p2=p1;
        p1=p1->next;
    }
if(p1)
    {
        if(p1==head) head=p1->next;