链表删除结点出现问题
问题是这样的:链表的结点包括学号、姓名。从键盘输入一个学号,如果该学号与链表中某一结点的学号相同,则删去该结点。我的程序是:
#include "stdio.h"
#include "stdlib.h"
#define N 81
typedef struct student
{
long num;
char name[N];
struct student *next;
}Student;
Student* create()
{
int i;
Student *p,*head=NULL,*tail=NULL;
for(i=0;i<N;i++)
{
p=(Student*)malloc(sizeof(Student));
scanf("%ld",&p->num);
if(p->num<0)
{
getchar();
free(p);
break;
}
gets(p->name);
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
return head;
}
void output(Student *i)
{
while(i!=NULL)
{
printf("%ld\t%s\n",i->num,i->name);
i=i->next;
}
}
Student* Delete(Student* a,long n)
{
Student *head,*temp,*before;
temp=before=head=a;
while(temp!=NULL)
{
if(temp->num==n)
if(temp==head)
{
head=head->next;
free(temp);
temp=before=head;
}
else
{
before->next=temp->next;
free(temp);
temp=before->next;
}
else
{
before=temp;
temp=temp->next;
}
}
return head;
}
void main()
{
long i,j=0;
Student *p,*head;
puts("input Num and name:(the num <0 quit input!)");
p=create();
puts("input need search Num:");
scanf("ld",&i);
head=Delete(p,i);
puts("the former linked list:");
output(p);
puts("after delete linked list:");
output(head);
}
运行可以,但是Delete删除函数没有作用,大神们帮忙看看呗~