首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎么建立动态单链表

2012-03-24 
如何建立动态单链表?请哪为大虾,给个动态建立单链表的函数,函数原形如struct student *creat(void)srtuct

如何建立动态单链表?
请哪为大虾,给个动态建立单链表的函数,函数原形如struct student *creat(void)
srtuct student
{
long num;
float score;
struct student *student;
}为学生的结构体定义.
该函数返回一链表的头接点head

[解决办法]
看看我的这个吧!很久以前学C语言的时候写的一个用链表用冒泡排序法排序的一个程序.要让你先输入要输入的数字的个数,然后让你输入数字,呵呵!然后再排序 ,看看吧!
[code=C/C++][/code]#include <stdio.h>
#include <stdlib.h> 
#include <conio.h>
struct x
{
int num;
struct x *next;
};
main()
{
int i,j,temp=0,d,e,a;
struct x *p1,*p2,*tail,*head;
p1=p2=tail=head=0;

printf("请输入个数:");
scanf("%d",&a);
 

for (d=1;d<=a;d++)
{
if (1 == d)
{
p1=(struct x*)malloc(sizeof(struct x));
p2=tail=head=p1;
}

else 
{
p1=(struct x*)malloc(sizeof(struct x));
tail->next=p1;
tail=p1;
}
}

p1=head;


printf("请输入%d个数字:\n",a);
 
for (i=1;i<=a;i++)
{
scanf("%d",&p1->num);
if (p1->next!=NULL)
p1=p1->next;
}
 
p2=p1=head;

for(i = 0;i < a;i++ )
{
for(j=a-1-i,p2=head; j < a;j++ )
{
if (p1->num > p2->num)
{
temp=p2->num;

p2->num=p1->num;
p1->num=temp;


}
 
if (p2->next!=NULL)
p2=p2->next;

}
if (p1->next!=NULL) 
p1=p1->next;

  }
  
  
  p1=head;
  
  for (i=0; i < a;i++)
  {
printf ("%d ",p1->num);
if (p1->next!=NULL)
  p1=p1->next;
  }

}

热点排行