两个有序顺序表合并
2016-07-07 11:52
281 查看
这是在做数据结构考研题目时遇到的题。
在这里记录一下,其中有上网参考大家的代码。
题目如下:
/*
题目:编写算法,将两个非递减有序顺序表A和B合成一个新的非递减有序顺序表C。
已知顺序表A和B的元素个数分别为m,n。其中顺序表采用动态分配内存空间,其定义如下:
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量
}Sqlist;
*/
[code]#include
#include "stdlib.h"
#include
typedef int ElemType;//定义ElemType为int类型
#define LIST_INIT_SIZE 100
#define error 0
#define ok 1
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量
}Sqlist;
int InitList_sq(Sqlist &A)
{
A.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!A.elem)
return error;
A.length = 0;
A.listsize = LIST_INIT_SIZE;
return ok;
}
int load_sq(Sqlist &L)
{
int i;
if (L.length == 0)
printf("The List is empty!");
else
{
for (i = 0; i < L.length; i++)
printf("%d ",L.elem[i]);
}
printf("\n");
return ok;
}
void merge( Sqlist A,Sqlist B, Sqlist &C)
{
int i = 0, j = 0, k = 0; // A B C三个数组指针 i,j,k;
while (i < A.length&&j < B.length) //循环退出的条件
{
if (A.elem[i]B.elem[j])
{
C.elem[k] = B.elem[j];
j++;
k++;
}
}
while (i
在这里记录一下,其中有上网参考大家的代码。
题目如下:
/*
题目:编写算法,将两个非递减有序顺序表A和B合成一个新的非递减有序顺序表C。
已知顺序表A和B的元素个数分别为m,n。其中顺序表采用动态分配内存空间,其定义如下:
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量
}Sqlist;
*/
[code]#include
#include "stdlib.h"
#include
typedef int ElemType;//定义ElemType为int类型
#define LIST_INIT_SIZE 100
#define error 0
#define ok 1
typedef struct{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量
}Sqlist;
int InitList_sq(Sqlist &A)
{
A.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!A.elem)
return error;
A.length = 0;
A.listsize = LIST_INIT_SIZE;
return ok;
}
int load_sq(Sqlist &L)
{
int i;
if (L.length == 0)
printf("The List is empty!");
else
{
for (i = 0; i < L.length; i++)
printf("%d ",L.elem[i]);
}
printf("\n");
return ok;
}
void merge( Sqlist A,Sqlist B, Sqlist &C)
{
int i = 0, j = 0, k = 0; // A B C三个数组指针 i,j,k;
while (i < A.length&&j < B.length) //循环退出的条件
{
if (A.elem[i]B.elem[j])
{
C.elem[k] = B.elem[j];
j++;
k++;
}
}
while (i
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)