f

来源:互联网 发布:北洋价签打印软件 编辑:程序博客网 时间:2024/06/02 17:52
#include "stdio.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 100
//#define LIST_INCREMENT 10
typedef struct 
{
int * ele; //the head pointer of the list
int length; //the length of the list
int sizeOfList;//current size of the list
}SqList;
void InitList_Sq(SqList &list)
{
list.ele = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
if (!list.ele)
{
exit(1);
}
list.length = 0;
list.sizeOfList = LIST_INIT_SIZE;
}


void mergeSqlist(SqList list1, SqList list2, SqList &list3){
int *p1, *p2;
int i, j, k;
i = 0;
j = 0;
k = 0;
p1 = list1.ele;
p2 = list2.ele;
list3.length = list2.length + list1.length;
list3.sizeOfList = list3.length;
list3.ele = (int*)malloc(list3.sizeOfList*sizeof(int));
while(i<list1.length && j<list2.length){
if (p1[i] > p2[j])
{
list3.ele[k++] = p2[j++];
}
else{
list3.ele[k++] = p1[i++];
}
}
if (i == list1.length)
{
while(j<list2.length)
list3.ele[k++] = p2[j++];
}
else{
while(i<list1.length)
list3.ele[k++] = p1[i++];
}
}
int main(int argc, char const *argv[])
{
SqList list1, list2, list3;
int i;
InitList_Sq(list1);
InitList_Sq(list2);
printf("please input the length of the list1 and list2: ");
scanf("%d %d", &list1.length, &list2.length);
printf("please input the elements of list1: ");
for (i = 0; i < list1.length; ++i)
{
scanf("%d", list1.ele+i);
}
printf("please input the elements of list2: "); 
for (i = 0; i < list2.length; ++i)
{
scanf("%d", list2.ele+i);
}
mergeSqlist(list1, list2, list3);
printf("The result of merge is: ");
for (i = 0; i < list3.length; ++i)
{
printf("%d ", *(list3.ele+i));
}
return 0;
}
0 0
原创粉丝点击