#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define LENGTH 100
int warn;
int compare;
int move;
typedef struct orderTable {
int* table;
int length;
}ot,*o;
int Times(int times, bool compare) {
if (compare)
printf("----共比较了%d次----\n", times);
else
printf("----共移动了%d次----\n", times);
return 0;
}
o getOrderTable() {
o ota = new ot;
ota->table = new int[LENGTH];
ota->length = 0;
return ota;
}
o initOrdefTable() {
o ota = getOrderTable();
int length;
printf("输入初始化的序列长度:");
warn = scanf("%d", &length);
ota->length = length;
printf("输入空格分开长度为%d的序列:", ota->length);
for (int i = 0; i < length; i++) {
warn = scanf("%d", &ota->table[i]);
}
return ota;
}
bool in(o ota, int value) {
for (int i = 0; i < ota->length; i++)
if (ota->table[i] == value)
return true;
return false;
}
int insertOrderTable(o ota,int index,int value) {
if (index<1 || index>ota->length) {
printf("超出限制,补位在首部或者尾部\n");
index = index < 1 ? 1 : ota->length;
}
for (int i = ota->length; i > index - 1; i--) {
ota->table[i] = ota->table[i - 1];
}
ota->table[index - 1] = value;
ota->length++;
return 0;
}
int searchOrderTable(o ota, int value) {
compare = 0;
int index = -1;
for (int i = 0; i < ota->length; i++) {
compare++;
if (ota->table[i] == value) {
index = i + 1;
break;
}
}
Times(compare, true);
return index;
}
int deleteOrderTable(o ota, int index) {
if (index<1 || index>ota->length)
return -1;
move = 0;
for (int i = index - 1; i < ota->length; i++) {
move++;
ota->table[i] = ota->table[i + 1];
}
ota->length--;
Times(move, false);
return 0;
}
int deleteOTinHE(o ota, int x, int y) {
for (int i = x; i<ota->length&&i + (y - x - 1)<ota->length; i++) {
ota->table[i] = ota->table[i + (y - x - 1)];
}
ota->length = ota->length - y + x + 1;
return 0;
}
int selectsort(o ota) {
compare = 0;
move = 0;
for (int i = 0; i < ota->length; i++) {
for (int j = i + 1; j < ota->length; j++) {
if (ota->table[i] > ota->table[j]) {
move++;
ota->table[i] = ota->table[i] + ota->table[j];
ota->table[j] = ota->table[i] - ota->table[j];
ota->table[i] = ota->table[i] - ota->table[j];
}
compare++;
}
}
Times(compare, true);
Times(move, false);
return 0;
}
int bubblesort(o ota) {
for (int i = 0; i < ota->length; i++) {
for (int j = 0; j < ota->length - 1 - i; j++) {
if (ota->table[j] > ota->table[j + 1]) {
ota->table[j] += ota->table[j + 1];
ota->table[j + 1] = ota->table[j] - ota->table[j + 1];
ota->table[j] = ota->table[j] - ota->table[j + 1];
}
}
}
return 0;
}
int insertsort(o ota) {
for (int i = 1; i < ota->length; i++) {
int index = 0;
int value = ota->table[i];
for (int j = 0; j < i; j++,index=j) {
if (ota->table[j] > value) {
break;
}
}
for (int j = i; j > index; j--)
ota->table[j] = ota->table[j - 1];
ota->table[index] = value;
}
return 0;
}
int halfsearchOrderTable(o ota,int value) {
int left = 0;
int right = ota->length - 1;
compare = 0;
while (left < right) {
compare++;
if (ota->table[(left + right) / 2] == value) {
Times(compare, true);
return (left + right) / 2;
}
else {
if (ota->table[(left + right) / 2] > value)
right = (left + right) / 2;
else
left = (left + right) / 2;
}
}
Times(compare, true);
printf("没有找到相应的值\n");
return -1;
}
void quicksort(o ota, int start, int end)
{
int i, j, temp, x;
i = start;
j = end;
x = ota->table[start];
while (i < j)
{
while (i < j && x <= ota->table[j]&&++compare)
j--;
while (i<j && x>=ota->table[i]&&++compare)
i++;
if (i < j)
{
move++;
compare++;
temp = ota->table[j];
ota->table[j] = ota->table[i];
ota->table[i] = temp;
}
}
temp = x;
x = ota->table[i];
ota->table[i] = temp;
if (start < j)
quicksort(ota, start, j - 1);
if (i < end)
quicksort(ota, j + 1, end);
}
int reverse(o ota) {
for (int i = 0; i < ota->length / 2; i++) {
ota->table[i] = ota->table[ota->length - 1 - i] + ota->table[i];
ota->table[ota->length - 1 - i] = ota->table[i] - ota->table[ota->length - i - 1];
ota->table[i] = ota->table[i] - ota->table[ota->length - 1 - i];
}
return 0;
}
int insertInSort(o ota, int value,bool show=true) {
compare = 0;
move = 0;
int index = 0;
for (int i = 0; i < ota->length; i++,index=i) {
compare++;
if (ota->table[i] < value)
continue;
else {
index = i;
break;
}
}
for (int i = ota->length; i > index; i--,move++)
ota->table[i] = ota->table[i - 1];
ota->length++;
ota->table[index] = value;
if (show) {
Times(compare, true);
Times(move, false);
}
return 0;
}
int showOrdefTable(o ota) {
if (ota->length > 0) {
for (int i = 0; i < ota->length; i++)
printf("%d ", ota->table[i]);
printf("\n");
return 0;
}
printf("无法展示\n");
return -1;
}
o merge(o ota1, o ota2) {
o ota3 = getOrderTable();
for (int i = 0; i < ota1->length; i++)
if(!in(ota3,ota1->table[i]))
insertInSort(ota3, ota1->table[i],false);
for (int i = 0; i < ota2->length; i++)
if(!in(ota3,ota2->table[i]))
insertInSort(ota3, ota2->table[i],false);
return ota3;
}
int main(void) {
int parm,index;
printf("1.实现顺序表建立显示\n");
o ota = initOrdefTable();
printf("2.实现顺序表插入 请输入插入的参数和位置:example(1,2)\n");
warn = scanf("%d%d", &parm,&index);
insertOrderTable(ota, index, parm);
showOrdefTable(ota);
printf("3.实现顺序表查找 请输入查找的值(显示比较次数)\n");
warn = scanf("%d", &parm);
printf("查询到的位置为%d(若为-1表示未查询到)\n",searchOrderTable(ota, parm));
printf("4.实现顺序表删除(显示移动次数) 输入删除的位置,从1开始:\n");
warn = scanf("%d", &index);
deleteOrderTable(ota, index);
showOrdefTable(ota);
printf("5.选择排序和快速排序 显示比较次数和移动次数\n");
printf("选择排序:\n");
selectsort(ota);
printf("快速排序:\n");
compare = move = 0;
quicksort(ota, 0, ota->length - 1);
Times(compare, true);
Times(move, false);
showOrdefTable(ota);
printf("6.实现顺序表的折半查找(显示比较次数) 输入待查找的值\n");
warn = scanf("%d", &parm);
halfsearchOrderTable(ota, parm);
printf("7.实现顺序表的逆置\n");
reverse(ota);
showOrdefTable(ota);
printf("8.顺序表的有序插入 输入待插入的值\n");
quicksort(ota, 0, ota->length);
warn = scanf("%d", &parm);
insertInSort(ota, parm);
showOrdefTable(ota);
printf("9.高效率删除x到y的节点 输入x和y 从1开始\n");
warn = scanf("%d%d", &parm, &index);
deleteOTinHE(ota, parm, index);
showOrdefTable(ota);
printf("10.合并两个顺序表 此时首先新建两个顺序表\n");
o ota1 = initOrdefTable();
o ota2 = initOrdefTable();
showOrdefTable(merge(ota1, ota2));
printf("11.插入排序\n");
insertsort(ota);
showOrdefTable(ota);
printf("12.冒泡排序\n");
bubblesort(ota);
showOrdefTable(ota);
return 0;
}
c语言线性表的一系列操作
最新推荐文章于 2024-05-20 18:16:07 发布