回复 2楼 wp231957
#include"stdio.h"
#include"stdlib.h"
#define LEN sizeof(struct Student)
typedef struct Student
{
int num;
char name[20];
char sex[5];
struct Student
*next;
}Node ;
Node
*creat()
{
Node *head,*p1,*p2;
head=NULL;
p1=p2=(Node *)malloc(LEN);
printf("Input number:");
scanf("%d",&p1->num);
while(p1->num)
{
printf("Input his information(name sex):\n");
scanf("%s %s",p1->name,p1->sex);
if(head==NULL)head=p1;
else p2->next=p1;
p2=p1;
p1=(Node *)malloc(LEN);
printf("Input number:");
scanf("%d",&p1->num);
}
p2->next=NULL;
return head;
}
Node *del(Node *head,int num)
{
Node *p1,*p2;
int find=1;
p1=head;
start:
while((p1->num!=num)&&(p1->next!=NULL))
{
p2=p1;p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)head=p1->next;
else
p2->next=p1->next;
goto start;
}
else printf("\n%d Not found\n",num);
return head;
}
void print(Node *head)
{
Node *p;
p=head;
while(p!=NULL)
{
printf(" name:%s
sex:%s
\n",p->name,p->sex);
p=p->next;
}
}
int main()
{
Node *head;
int num;
head=creat();
printf("Before::\n");
print(head);
printf("Input an num:");
scanf("%d",&num);
del(head,num);
printF("After::\n");
print(head);
return 0;
}