数据结构C语言编程

Python018

数据结构C语言编程,第1张

#include "stdio.h"

#include <stdlib.h>

#include "time.h"

int main(int argv,char *argc[]){

double x[10]={0.0,}

int i

srand((unsigned)time(NULL))

while(rand()%10000!=0){//

for(i=0i<9x[i++]=x[i+1])

x[9]=rand()/32767.0*100000//模拟采集数据

}

for(i=0i<10printf("%10.3f\n",x[i++]))//输出最后10个数

return 0

}

运行样例:

#include

"stdio.h"

#include

"malloc.h"

#define

null

0

struct

node

/*定义结构体*/

{int

data

struct

node

*next

}

struct

node

*head

struct

node

*p

struct

node

*s

void

creat()

/*创建单链表*/

{int

ch

head=(struct

node

*)malloc(sizeof(struct

node))

head->next=null

p=head

printf("请输入数据:

")

scanf("%d",&ch)

while(ch!=-1)

{s=(struct

node

*)malloc(sizeof(struct

node))

s->data=ch

s->next=null

p->next=s

p=s

printf("请输入数据:

")

scanf("%d",&ch)

}

}

void

outline()

/*输出单链表*/

{p=head->next

while(p!=null)

{printf("%d

",p->data)

p=p->next

}

printf("\n")

}

int

locate(int

x)

/*按值查找*/

{p=head->next

while((p!=null)&&(p->data!=x))

p=p->next

if(p==null)

return

0

else

return

(p->data)

}

main()

/*主函数*/

{int

a=0

creat()

outline()

printf("请输入你要找的数:\n")

scanf("%d",&a)

printf("你要找的数的下标是:

%d\n",locate(a))

printf("结点个数是:

%d\n",countnode())

}

把scanf("%d\n",&q->name)改成scanf("%s",q->name)。

把scanf("%d\n",&q->score)改成scanf("%d",&q->score)。

函数studlist *CreateStudent()应该有一个返回值。若不需要返回值,请改成void CreateStudent()。

if(p->Next->score<q->score)中p->Next->score并未赋值,怎么能与q->score比较?这里就会跳出运行。

char name[3]中3太小只能放下一个汉字或两个字符。

适当的地方应该有释放所申请的内存的语句。