实验6.1 排序程序设计实验
一、目的要求
(1)了解排序算法的一分析过程。
(2)掌握选择排序的基本思路。
(3)掌握选择排序算法的程序实现方法。
(4)学习数组在排序程序中的应用。
二、实验内容
(1)从键盘输入一组数存放在数组中。
(2)写一个函数对所输入的数采用选择排序方法进行排序。
(3)将排序的结果显示出来。
(4)改进的选择排序的方法,其思路是:将待排序的记录与数据序列中的每个记录从最后一个倒着比较,如果发生数据交换,则记住其交换的位置,因为交换后的数比其之后所有数都大(或小根据从大到小还是从小到大的排序),下一趟比较,只从最后一次交换的位置倒着比较。这样,就大大的减小了比较的次数。
(5)对程序进行改进,采用改进的选择排序方法,重新对这些数据进行排序,对比两种方法的相同点和不同点。
实验6.2 单向链表程序设计实验
一、实验目的、要求
(1)了解动态数据结构的优点与缺点。
(2)掌握单向链表的创建方式。
(3)掌握单向链表的插入、删除结点的操作。
(4)掌握单向链表的输出及结点空间释放。
二、实验的内容
(1)编写一个函数,要求从键盘输入n个数,建立一个以输入数据为关键字的一个单向链表,并返回链表的表首结点。
(2)从键盘输入两个数K和V,写一个函数,在链表中查找数K,如果该数存在,则在其对应的结点前面插入一个新结点,并将该结点的关键字设为V。
(3)如果在链表中没有关键字为K的结点,则在链表的末尾插入新结点,并设其关键字为V。
(4)将该链表的中间结点删除掉。
(5)输出该链表的内容。
三、源程序
一
#include<stdio.h>
#include<malloc.h>
int main()
{
int n,*a=0,i=0,j,temp;
printf("please input the number of element: ");
scanf("%d",&n);
a=(int *)malloc(sizeof(int)*n);
if(a==0)
{
printf("fail\n");
return 0;;
}
printf("please input %d elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", (a+i));
printf("%d elements are: \n", n);
for (i = 0; i < n; i++)
printf("%4d", *(a+i));
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(*(a+i)>*(a+j))
{ temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
printf("\n排序后是\n");
for (i = 0; i < n; i++)
printf("%4d", *(a+i));
printf("\n");
return 0;
}
二
#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student{
int number;
struct student *next;
};
struct student *create()
{
struct student *head ;
struct student *p1, *p2;
p1=p2=(struct student *)malloc(sizeof(len));
head=NULL;
printf("请输入一个数:当输入为0时结束输入\n");
scanf("%d",&(p1->number));
while(p1->number!=0)
{
if(head==NULL)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(sizeof(len));
printf("请输入一个数,当输入为0时结束输入\n");
scanf("%d",&(p1->number));
}
p2->next=NULL;
return head;
}
struct student *Delete(struct student * head)
{
struct student *p1;
int i;
p1=head;
if(NULL==head)
{
printf("链表为空!\n");
return head;
}
while(p1->next!=NULL)
{
for(i=0;p1->next!=NULL;p1=p1->next)
{i++;
}
head->next=p1;
}
return head;
}
void Print(struct student *h)
{
struct student * p;
p=h;
if(NULL==h)
{
printf("链表为空!\n");
}
else
{
while(p!=NULL)
{
printf("%d \n",p->number);
p=p->next;
}
}
}
int main()
{
struct student *p1,*p0,*head,*p2,*p3,*P;
int k,n,v,i,count1=1,count2=1,luan=0;
p1 = create();
head=p1;
printf("\n输入K v的值(先k后v):");
scanf("%d %d ",&k,&v);
for(count1=1;p1->next!=NULL;p1=p1->next)
count1++;
p1=head;
p3=head;
for(;p3->next!=NULL&&(p3->number!=k);p3=p3->next)
count2++;
printf("%3d %3d\n",count1,count2);//print//
if(count2<count1)
luan=1;
else if(count2==6)
{ if(p3->number==k)
luan=1;
}
else
luan=0;
p1=head;
p0= (struct student *)malloc(sizeof(len));
switch(luan)
{
case 1:
{
p0->number=v;
for(n=1;p1->next!=NULL&&(p1->number!=k);p1=p1->next)
n++;
printf("%3d\n",n);//n表示j等于第几个链表的数据// p1=head; if(n==1) { head=p0; head->next=p1; head->number=v; } else { P=p1=head;
for(i=1;i<n-1;i++) p1=p1->next; p2=p1; p2=p2->next; p1->next=p0; p0->next=p2; printf("插入v后的链表\n"); Print(head); Delete(head); printf("删除后的链表:\n"); Print(head); } } break; case 0: { p1=head; for(i=0;p1->next!=NULL;p1=p1->next) {i++;} p1->next=p0; p0->next=NULL; p0->number=v; printf("插入v后的链表:\n"); Print(head); } break; } return 0; } 四、运行截图