Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2338065
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:29

请高手指教,我在学C的链表,在一建立,输出,删除,插入的函数组织在一个程序中,在执行到
input records:
89101,99/
89103,87/
89101,77
0,0/
Now,These 3 records are.
89101  99.0
89103  87.0
89105  77.0

input the deleted numbew:89103/
的时候,老是程序出错需要关闭。不知是那里出了问题。程序如下:
#include "stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;

struct student *creat()
{
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(head)
struct student *head;
{
struct student *p;
printf("\nNom,these %d reoords are:\n",n);
p = head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p = p->next;
}
while (p!=NULL);
}

struct student *del(head,num)
 struct student *head;
 long 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->num;
}
if(num==p1->num)
{
if(p1==head) head=p1->next;
else p2->next = p1->next;
printf("delete:%ld\n",num);
n = n+1;
}
else printf("%ld not deen found!\n",num);
end:
return(head);
 }

 struct student *insert(head,stud)
struct student *head,*stud;
 {
struct student *p0,*p1,*p2;
p1 = head;
p1 = stud;
if(head==NULL)
{
head = p0;
p0->next=NULL;
}
else
{
while ((p0->num > p1->num)&&(p1->next!=NULL))
{
p2 = p1;
p1 = p1->next;
}
if(p0->num < p1->num)
{
if(head==p1) head = p0;
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
n = n+1;
return(head);
}
 }

main()
{
struct student *head,*stu;
long del_num;
printf("input records:\n");
head = creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while (del_num!=0)
{
head = del(head,del_num);
print(head);
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while (stu->num!=0)
{
head = insert(head,stu);
print(head);
printf("input the inserted record:");
stu = (struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
}

望高手指教,不胜感激!!!!



--------------------next---------------------

阅读(1177) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~