求大手帮忙一一解释下意思 急.#include "stdio.h"#include "stdlib.h"#include "windows.h"#define N 10 /*输入10个数*/struct d{int data;struct d * next;};typedef struct d D;D * creat(){int i;D *h,*s,*r;h=(D *)malloc(sizeof(D));r=h;srand(ti
来源:学生作业帮助网 编辑:作业帮 时间:2024/08/29 05:52:15
求大手帮忙一一解释下意思 急.#include "stdio.h"#include "stdlib.h"#include "windows.h"#define N 10 /*输入10个数*/struct d{int data;struct d * next;};typedef struct d D;D * creat(){int i;D *h,*s,*r;h=(D *)malloc(sizeof(D));r=h;srand(ti
求大手帮忙一一解释下意思 急.
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#define N 10 /*输入10个数*/
struct d
{
int data;
struct d * next;
};
typedef struct d D;
D * creat()
{
int i;
D *h,*s,*r;
h=(D *)malloc(sizeof(D));
r=h;
srand(time(0));
for(i=0;idata=rand()%100;
r->next=s;
r=s;
}
r->next='\0';
return h;
}
void sort(D *h)
{
int t;
D *z,*y,*r;
z=h->next;
while(z->next!='\0')
{
y=z->next;
while(y->next!='\0')
{
if(z->data < y->data)
{
t=z->data;
z->data = y->data;
y->data = t;
}
y=y->next;
}
if(z->data < y->data)
{
t=z->data;
z->data = y->data;
y->data = t;
}
z=z->next;
}
}
void out(D *h)
{
int i;
D *s;
s=h->next;
while(s)
{
printf("%d\n",s->data);
s=s->next;
}
}
void ins(int n )
{
}
void dele()
{
}
int fun(int n)
{
}
main()
{
D *head;
head=creat();
out(head);
sort(head);
printf("排序后:\n");
out(head);
}
求大手帮忙一一解释下意思 急.#include "stdio.h"#include "stdlib.h"#include "windows.h"#define N 10 /*输入10个数*/struct d{int data;struct d * next;};typedef struct d D;D * creat(){int i;D *h,*s,*r;h=(D *)malloc(sizeof(D));r=h;srand(ti
D * creat()
{
int i; D *h,*s,*r;
h=(D *)malloc(sizeof(D)); //动态申请内存 大小为结构体struct d所占内存大小
r=h; //结构体r指向h
srand(time(0)); //产生随即数种子
for(i=0;idata=rand()%100; //rand()%100 可以随即获得0 - 99的整数
r->next=s; //r->next指向s
r=s; //r指向s 即在尾部添加链表s
}
r->next='\0'; //添加结束符
return h; //链表头部地址
}
void out(D *h)
{
int i;
D *s;
s=h->next;
while(s) //s不指向空,就往下执行
{
printf("%d\n",s->data); //输出数据
s = s->next; //指向下一个链表单元
}
}
void sort(D *h)
{ //排序采用的是冒泡排序算法
int t;
D *z,*y,*r;
z=h->next;
while(z->next!='\0') //指针不为空
{
y=z->next; //y指向z->next
while(y->next!='\0') // y找出最大数
{
if(z->data < y->data) //比较大小
{
t=z->data; //数据交换
z->data = y->data;
y->data = t;
}
y=y->next;
}
if(z->data < y->data) //比较数据大小
{
t=z->data; //数据交换
z->data = y->data;
y->data = t;
} z=z->next; //指向下一个元素,继续比较
}
}