1.在随机产生的数中寻找数字
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 20
int main()
{
int arr[N],x,n,i;
int f=-1;
srand(time(NULL));
for(i=0;i<N;i++)
{
arr[i] = rand()/1000;
}
printf("请输入您要查找的数: ");
scanf("%d",&x);
for(i=0;i<N;i++)
{
if(x==arr[i])
{
f=i;
break;
}
}
printf("\n随机产生的数据序列为: \n");
for(i=0;i<N;i++)
{
printf("%d ",arr[i]);
}
printf("\n\n");
if(f<0)
{
printf("没有找到相应数据!\n\n");
}
else
{
printf("数据:%d 位于数组的第 %d 个元素处。\n",x,f+1);
}
system("pause");
return 0;
}
2.链表学习(一)
#include<stdio.h>
struct Student
{
int num;
float score;
struct Student *next;
};
int main()
{
struct Student a,b,c,*head,*p;
a.num=1001; a.score=80;
b.num=1002; b.score=90.1;
c.num=1003; c.score=93.2;
head=&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do
{
printf("%d\t%.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
return 0;
}
3.链表的创建
#include<stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
int main()
{
struct Student *head=NULL,*p1,*rear=NULL;
p1=(struct Student*)malloc(LEN);
scanf("%d%f",&p1->num,&p1->score);
while(p1->num!=-999)
{
if(head==NULL)
head=p1;
else
rear->next=p1;
rear=p1;
p1=(struct Student*)malloc(LEN);
scanf("%1d%f",&p1->num,&p1->score);
}
rear->next=NULL;
return(head);
}