直接上代码(小伙伴们根据我的函数写一个头文件就可以了)
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "归并排序.h"
//初始化
void Init_list(PList p)
{
assert(p != NULL);
p->data = (ELEMTYPE*)malloc(MAX_SIZE * sizeof(ELEMTYPE));
assert(p->data!=NULL);
p->length = 0;
p->List_size = MAX_SIZE;
}
//任意位置插入
bool Insert_pos(PList p, int pos, int val)
{
//断言
assert(p != NULL);
if (p == NULL)
{
return false;
}
//1、要判断插入位置是否合法
if (pos<0 || pos> p->length)
{
return false;
}
//2、插入操作要判满,满了就要relloc申请内存
if (IsFull( p))
{
Inc(p);
}
//3、腾位置
for ( int i = p->length - 1; i >= pos ; i--)
{// 最后一个有效数据节点下标为p->length - 1
p->data[i + 1] = p->data[i];
}
//4、插入
p->data[pos] = val;
p->length++;
return true;
}
// 任意位置删除
bool Del_pos(PList p, int pos)
{
//断言
assert(p != NULL);
if (p == NULL)
{
return false;
}
//1、判断删除位置是否合法
if (pos < 0 || pos > p->length)
{
return printf("不能删除此处!");
}
//2、判空
if (IsEmpty(p))
{
return false;
}
//3、找到待删除位置
for (int i = pos+1; i <=p->length-1; i++)
{
p->data[i-1] = p->data[i];
}
p->length--;
return true;
}
//按值删除
bool Del_val(PList p, int val)
{
//断言
assert(p != NULL);
if (p == NULL)
{
return false;
}
int DeleteNumber = Search(p, val);//用查找函数进行寻找
if (DeleteNumber == -1)
{
return false;
}
return Del_pos(p, DeleteNumber);//直接调用删除函数进行删除操作
}
// 判空
bool IsEmpty(PList p)
{
return p->length == 0;
}
// 判满
bool IsFull(PList p)
{
return p->length == p->List_size;
}
// 扩容
void Inc(PList p)
{
//realloc 需要两个参数 也就是在不足的空间上续上一段,所以要给出地址,和扩容的大小
ELEMTYPE* tmp = (ELEMTYPE*)realloc(p->data, (p->List_size * sizeof(ELEMTYPE)) * 2);
//断言
assert(tmp != NULL);
if (tmp == NULL)
{
printf("内存扩容失败\n");
return Destory( p);
}
p->data = tmp;
p->List_size *= 2;
}
// 打印
void Show(PList p)
{
for (int i = 0; i < p->length; i++)
{
printf("%d ", p->data[i]);
}
printf("\n");
}
// 销毁
void Destory(PList p)
{
free(p->data);
}
//查询
int Search(PList p, int val)
{
//断言
assert(p != NULL);
if (p == NULL)
{
return false;
}
for (int i = 0; i<p->length; i++)
{
if (p->data[i] = val)
{
return i;
}
}
return -1;
}
void MergeList_sqlist(PList La, PList Lb, PList Lc)
{
ELEMTYPE* pa = La->data;
ELEMTYPE* pb = Lb->data;
//将定义的pa,pb都指向数组的第一个有效元素的地址
Lc->List_size = Lc->length = La->length + Lb->length;
//随之LC的长度在归并后也要发生改变
ELEMTYPE* pc = Lc->data = (ELEMTYPE*)malloc(Lc->length * sizeof(ELEMTYPE));
//malloc动态申请堆空间
ELEMTYPE* pa_last = La->data + La->length - 1;
ELEMTYPE* pb_last= Lb->data + Lb->length - 1;
while (pa<=pa_last&&pb<=pb_last)
{
if (*pa <= *pb)
{
*pc++ = *pa++;//*pc=*pa; pc++;pa++;
}else
{
*pc++ = *pb++;//*pc=*pb; pc++;pb++
}
}
while (pa <= pa_last) { *pc++ = *pa++; }
while (pb <= pb_last) { *pc++ = *pb++; }
}
int main()
{
int ar[] = { 3,5,8,11 };
int an = sizeof(ar) / sizeof(ar[0]);
int br[] = { 2,6,8,9,11,15,20 };
int bn = sizeof(br) / sizeof(br[0]);
struct List La, Lb, Lc;
Init_list(&La);
Init_list(&Lb);
for (int i = 0; i < an; i++)
{
Insert_pos(&La, La.length, ar[i]);
}
for (int i = 0; i < bn; i++)
{
Insert_pos(&Lb, Lb.length, br[i]);
}
Show(&La);
Show(&Lb);
MergeList_sqlist(&La, &Lb, &Lc);
Show(&Lc);
Destory(&La);
Destory(&Lb);
Destory(&Lc);
}