8577 合并顺序表
时间限制:1000MS 代码长度限制:10KB
提交次数:5339 通过次数:2251
题型: 编程题 语言: G++;GCC
Description
顺序表的基本操作代码如下:
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef int Status;
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{ // 算法2.3
// 构造一个空的线性表L。
L.elem = (ElemType )malloc(LIST_INIT_SIZEsizeof(ElemType));
if (!L.elem) return OK; // 存储分配失败
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
Status ListInsert_Sq(SqList &L, int i, ElemType e)
{ // 算法2.4
// 在顺序线性表L的第i个元素之前插入新的元素e,
// i的合法值为1≤i≤ListLength_Sq(L)+1
ElemType *p;
if (i < 1 || i > L.length+1) return ERROR; // i值不合法
if (L.length >= L.listsize) { // 当前存储空间已满,增加容量
ElemType *newbase = (ElemType *)realloc(L.elem,
(L.listsize+LISTINCREMENT)*sizeof (ElemType));
if (!newbase) return ERROR; // 存储分配失败
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
ElemType *q = &(L.elem[i-1]); // q为插入位置
for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e
++L.length; // 表长增1
return OK;
} // ListInsert_Sq
Status ListDelete_Sq(SqList &L, int i, ElemType &e)
{ // 算法2.5
// 在顺序线性表L中删除第i个元素,并用e返回其值。
// i的合法值为1≤i≤ListLength_Sq(L)。
ElemType *p, *q;
if (i<1 || i>L.length) return ERROR; // i值不合法
p = &(L.elem[i-1]); // p为被删除元素的位置
e = *p; // 被删除元素的值赋给e
q = L.elem+L.length-1; // 表尾元素的位置
for (++p; p<=q; ++p) *(p-1) = *p; // 被删除元素之后的元素左移
–L.length; // 表长减1
return OK;
} // ListDelete_Sq
编写算法,将两个非递减有序顺序表A和B合并成一个新的非递减有序顺序表C。
输入格式
第一行:顺序表A的元素个数
第二行:顺序表A的各元素(非递减),用空格分开
第三行:顺序表B的元素个数
第四行:顺序表B的各元素(非递减),用空格分开
输出格式
第一行:顺序表A的元素列表
第二行:顺序表B的元素列表
第三行:合并后顺序表C的元素列表
输入样例
5
1 3 5 7 9
5
2 4 6 8 10
输出样例
List A:1 3 5 7 9
List B:2 4 6 8 10
List C:1 2 3 4 5 6 7 8 9 10
#include<cstdio>
#include <cstdlib>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int
typedef int KeyType;
typedef int Status;
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
void unique_link(SqList &L)
{
int i, j=1, sum=1;
while(j<L.length)
{
for(i=0; i<sum; i++)
{
if(L.elem[i]==L.elem[j])
break;
}
if(i==sum)
L.elem[sum++]=L.elem[j++];
else
j++;
}
L.length=sum;
}
void BubbleSort(SqList &L)
{
int temp;
for(int i=0;i<L.length-1;i++)
for(int j=0;j<L.length-i-1;j++)
{
if(L.elem[j]>L.elem[j+1])
{
temp=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=temp;
}
}
}
Status InitList_Sq(SqList &L)
{ // 算法2.3
// 构造一个空的线性表L。
L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem) return OK; // 存储分配失败
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
Status ListInsert_Sq(SqList &L, int i, ElemType e)
{ // 算法2.4
// 在顺序线性表L的第i个元素之前插入新的元素e,
// i的合法值为1≤i≤ListLength_Sq(L)+1
ElemType *p;
if (i < 1 || i > L.length+1) return ERROR; // i值不合法
if (L.length >= L.listsize) { // 当前存储空间已满,增加容量
ElemType *newbase = (ElemType *)realloc(L.elem,
(L.listsize+LISTINCREMENT)*sizeof (ElemType));
if (!newbase) return ERROR; // 存储分配失败
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
ElemType *q = &(L.elem[i-1]); // q为插入位置
for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e
++L.length; // 表长增1
return OK;
} // ListInsert_Sq
Status ListDelete_Sq(SqList &L, int i, ElemType &e)
{ // 算法2.5
// 在顺序线性表L中删除第i个元素,并用e返回其值。
// i的合法值为1≤i≤ListLength_Sq(L)。
ElemType *p, *q;
if (i<1 || i>L.length) return ERROR; // i值不合法
p = &(L.elem[i-1]); // p为被删除元素的位置
e = *p; // 被删除元素的值赋给e
q = L.elem+L.length-1; // 表尾元素的位置
for (++p; p<=q; ++p) *(p-1) = *p; // 被删除元素之后的元素左移
--L.length; // 表长减1
return OK;
} // ListDelete_Sq
int main()
{
int a;
scanf("%d",&a);
SqList la;
InitList_Sq(la);
for(int i=1;i<=a;i++)
{
int ta;
scanf("%d",&ta);
ListInsert_Sq(la,i,ta);
}
// unique_link(la);
BubbleSort(la);
int i;
if(la.length==0) printf("The List is empty!"); // 请填空
else
{
printf("List A:");
for(i=0;i<la.length-1;i++) printf("%d ",la.elem[i]);
printf("%d",la.elem[la.length-1]);// 请填空
}
printf("\n");
int b;
scanf("%d",&b);
SqList lb;
InitList_Sq(lb);
for(i=1;i<=a;i++)
{
int tb;
scanf("%d",&tb);
ListInsert_Sq(lb,i,tb);
}
// unique_link(lb);
BubbleSort(lb);
if(lb.length==0) printf("The List is empty!"); // 请填空
else
{
printf("List B:");
for(i=0;i<lb.length-1;i++) printf("%d ",lb.elem[i]); // 请填空
printf("%d",lb.elem[lb.length-1]);
}
printf("\n");
printf("List C:");
int n,m;//na,mb
n=m=0;
while(1)
{
if(n==la.length||m==lb.length)
{break;}
else if(la.elem[n]>lb.elem[m])
{
printf("%d ",lb.elem[m]);
m++;
}
else if(la.elem[n]<lb.elem[m])
{
printf("%d ",la.elem[n]);
n++;
}
else if(la.elem[n]==lb.elem[m])
{
printf("%d %d ",lb.elem[m],la.elem[n]);
n++;m++;
}
}
while(1) {
if (n == la.length ) {
printf("%d ", lb.elem[m]);
m++;
} else if (m == lb.length ) {
printf("%d ", la.elem[n]);
n++;
}
if(n==la.length&&m==lb.length)
{break;}
}
printf("\n");
return 0;
}