| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 411 人关注过本帖
标题:关于链表
只看楼主 加入收藏
smcok
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2010-3-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
关于链表
小弟学习c到了链表,但书上写得太杂乱,研究了很久都没能弄懂。
请求各位大虾写个关于链表的程序并带上注释让小弟研究,小弟感激不尽。
搜索更多相关主题的帖子: 链表 
2010-03-25 10:24
寻找南方
Rank: 2
等 级:论坛游民
帖 子:57
专家分:75
注 册:2009-10-21
收藏
得分:7 
你可以去看看谭浩强的《C程序设计》,他上面关于链表说的你应该能看的懂!!

我一路向前~~~~~~~~~~~~~
2010-03-25 10:34
ldg628
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:3
帖 子:526
专家分:3036
注 册:2009-6-23
收藏
得分:7 
你看书上的程序时 ,跟着它的步骤自己画一下图,思路就会很清晰了。。。不要急躁。。。
2010-03-25 10:36
一口三个汉堡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:155
专家分:525
注 册:2010-3-21
收藏
得分:0 
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student//定义结构体
{long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)//建立链表
{struct student *head;//定义结构体类型指针head,p1,p2
struct student *p1,*p2;
n=0;//定义结点长度n
p1=p2=(struct student *)malloc(LEN);//给p1,p2开辟相同的空间
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;//给表头赋值
else p2->next=p1;//把输入的数连在后面
p2=p1;
p1=(struct student *)malloc(LEN);//重新给P1开辟空间
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;//把表尾设为0
return head;
}
void print(struct student *head)//输出链表
{struct student *p;
printf("\nNow,These %d records 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(struct student *head,long num)//删除结点
{struct student *p1,*p2;
if(head==NULL){printf("\nlist null!\n");return head;}
p1=head;//把表头付给p1
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;//n+1的结点与n-1的结点相连
printf("delete:%d\n",num);
n=n-1;
}
else printf("%ld not been fould!\n",num);
return head;
}
struct student * insert(struct *head,struct student *stud)//结点的插入
{struct student *p0,*p1,*p2;
p1=head;
p0=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;}
}
/*插入功能的缺点:无法再本来的两个数之间插入一个如102与103之间则无法进行*/
n=n+1;
return head;
}
main()//主函数
{struct student *head;
struct student *stu;
long del_num;
printf("input record:\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 insert record:");
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);
}
}

坚持做对的事情,而不是容易的事情。
2010-03-25 12:17
smcok
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2010-3-2
收藏
得分:0 
回复 4楼 一口三个汉堡
thank you!

老师说:好好学习 天天向上!
2010-03-26 19:50
快速回复:关于链表
数据加载中...
 
   



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

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