,本人新人,跪求:一链表程序有错,找不出来!谁能帮忙,谢咯!

作者在 2010-05-18 15:55:41 发布以下内容
题目:建立一个链表,每个结点包括:学号,姓名,性别,年龄。输入一个年龄,如果链表中的所包含年龄等于此年龄,则将此结点删除。
#include "stdafx.h"
#include"stdio.h"
#include"malloc.h"
#define NULL0
#define LEN sizeof(struct student)
struct student
{
 int num;
 char name[20];
 char sex;
 int age;
 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("%d %s %c %d",&p1->num,&p1->name,&p1->sex,&p1->age);
 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("%d %s %c %d",&p1->num,&p1->name,&p1->sex,&p1->age);
 }
 p2->next=NULL;
 return(head);
}
void print(struct student *head)
{
 struct student *p;
 printf("\nnow,the %d records are:\n",n);
 p=head;
 if(head!=NULL)
  do
  {
   printf("%d %s %c %d\n",p->num,p->name,p->sex,p->age);
   p=p->next;
  }while(p!=NULL);
}
struct student *del(struct student *head,int num)
{
 struct student *p1,*p2;
 if(head==NULL)
 {
  printf("\nlist null!\n");
  goto end;
 }
 p1=head;
 while(num!=p1->num&&p1->next!=NULL)
 {
  p2=p1;p1=p1->next;
 }
 if(num==p1->num)
 {
  if(p1==head) head=p1->next;
   else
    p2->next=p1->next;
  printf("delete:%d\n",num);
  n=n-1;
 }
  else
   printf("%d not been found!\n",num);
 end:
 return(head);
}
void main()
{
 struct student *head,*stu;
 int del_age;
 printf("input records:\n");
 printf("num name sex age\n\n");
 head=creat();
 print(head);
 printf("\ninput the delete age:\n");
 scanf("%d",&del_age);
 while(del_age!=0)
 {
  head=del(head,del_age);
  print(head);
  printf("input the delete age:\n");
  scanf("%d",&del_age);
 }
}
默认分类 | 阅读 908 次
文章评论,共0条
游客请输入验证码
浏览5250次
文章分类