|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
对顺序表进行深度复制,即操作其中一个顺序表是,对另一个顺序表没有影响。代码应该是复制那块出了问题,可能是关键字类型不同,有没有大佬帮我改一下。
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <conio.h>
- #define MAX_NUM 32768
- int compareTimes;//比较次数
- int moveTimes;//移动次数
- typedef int ElemType;//元素类型
- typedef struct
- {
- ElemType key;
- } KeyType;//关键字的类型
- typedef struct
- {
- KeyType R[MAX_NUM+1];
- int length;
- }*orderList,Node;
- orderList init_orderList()
- {
- orderList l=(orderList)malloc(sizeof(Node));
- l->length=0;
- // printf("顺序表初始化成功");
- return l;
- }
- //随机产生一些数字
- int produce_randomNum(orderList l)
- {
- srand(time(0));
- int n,m,a,i,j;
- printf("随机产生1-m范围的 n个数:\n");
- scanf("%d %d",&m,&n);//产生n个1-m之间的数字
- for(i=1; i<=n; i++)
- {
- a=rand()%m+1;
- //printf("%d ",a);
- l->R[++l->length].key=a;
- }
- return n;
- }
- void copy(orderList l,orderList r)
- {
- int j;
- for(j=0;j<l->length;j++)
- {
- r[j]=l[j];
- }
- }
- void insertSort(orderList l)
- {
- int i,j;
- int signal;
- compareTimes=moveTimes=0;
- for(i=2; i<=l->length; i++) //从第二个数开始依次与前面的数字比较
- {
- signal=l->R[i].key;//当前结点作为标志结点
- j=i-1;
- while(j>=1)//从当前结点下一个开始与标志结点比较
- {
- compareTimes++;
- if(l->R[j].key>signal)//大于标志结点,结点后移
- {
- moveTimes++;
- l->R[j+1]=l->R[j];
- j--;
- }
- if(l->R[j].key<=signal)//小于标志结点,结束
- break;
- }
- moveTimes++;
- l->R[j+1].key=signal;//第j+1个结点标志结点
- }
- printf("插入排序结果:\n");
- // print_orderList(l);
- printf("比较次数%d ,移动次数:%d\n",compareTimes,moveTimes);
- }
- void print_orderList(orderList l)
- {
- int i;
- for(i=1; i<=l->length; i++)
- printf("%d ",l->R[i].key);
- printf("\n");
- }
- int main()
- {
- clock_t start, finish;
- double Total_time;
- int choice;
- orderList l;
- orderList r;
- int n;
- while(1)
- {
- printf("1.初始化数据\n");
- printf("2.直接插入排序\n");
- printf("8.输出数据\n");
- printf("请选择操作\n");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- l=init_orderList();
- n=produce_randomNum(l);
- copy(l,r);
- break;
- case 2:
- start = clock();
- insertSort(l);//直接插入排序
- finish = clock();
- Total_time = (double) (finish - start) / CLOCKS_PER_SEC;
- printf("所用时间为%lf\n",Total_time);
- break;
- case 8:
- //打印数字
- print_orderList(l);
- print_orderList(r);;
- break;
- case 0:
- //退出
- exit(0);
- default:
- printf("您输入的数据有误,请重新输入\n");
- break;
- }
- printf("请按任意键继续\n");
- getch();
- system("cls");
- continue;
- }
- return 0;
- }
复制代码 |
-
|