| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 397 人关注过本帖
标题:链表的创建,添加!
取消只看楼主 加入收藏
kisaa
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-5-5
收藏
 问题点数:0 回复次数:0 
链表的创建,添加!

运行一下这个程序,我不知道那里错了,一直是这样的结果。

#include "stdio.h"
#include "malloc.h"
#define NULL 0
#define KAICH sizeof(struct student)
int n;
struct student
{int num;
float score;
struct student *next;
};
struct student *creat(void)
{int f1;
float f2;
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(KAICH);
scanf("%d,%3.1f",&p1->num,&p2->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(KAICH);
scanf("%d,%3.1f",&p1->num,&p2->score);
}
p2->next=NULL;
return(head);
}
struct student *dele(struct student *head,int number)
{struct student *p1,*p2;
if(head==NULL)
printf("the lianbiao is null!\n");
else
p2=head;
while(p2->num!=number&&p2->next!=NULL)
{p1=p2;p2=p2->next;}
if(p2->num==number)
{if(p2==head)head=p2->next;
else p1->next=p2->next;
printf("the node is delete %d!\n",number);
n=n-1;
}
else
free(p2);
printf("the number is not find!\n");
return(head);
}
struct student *insert(struct student *head,struct student *inp)
{struct student *p1,*p2,*p3;
p1=head;
p3=inp;
if(head==NULL)
{head=p3;
p3->next=NULL;}
else
{while((p1->num<p3->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(p1->num>p3->num)
{if(head==p1)head=p3;
else p2->next=p3;
p3->next=p1;
}
else
p1->next=p3;
p3->next=NULL;
}
n=n+1;
return(head);
}
void print(struct student *head)
{struct student *p;
p=head;
printf("\nyour lianbiao are %d\n",n);
while(head!=NULL&&p->num!=NULL)
{printf("%d\t%3.1f\n",p->num,p->score);p=p->next;}
}
main()
{struct student *head,*pum;
int delnum;
printf("input what you want and over when num is NULL!\n");
head=creat();
print(head);
printf("\ninput you delete number\n");
scanf("%d",&delnum);
while(delnum!=0)
{
head=dele(head,delnum);
print(head);
printf("you delete number\n");
scanf("%d",&delnum);
}
printf("input you want insert number\n");
pum=(struct student*)malloc(KAICH);
scanf("%d,%3.1f",&pum->num,&pum->score);
while(pum->num!=0)
{head=insert(head,pum);
print(head);
printf("go on insert\n");
pum=(struct student*)malloc(KAICH);
scanf("%d,%3.1f",&pum->num,&pum->score);
}
}
搜索更多相关主题的帖子: 链表 
2007-05-10 15:51
快速回复:链表的创建,添加!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025583 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved