顺序表的基本操作
顺序表的初始化
空间分配/表长与容量初始化
Status InitList_Sq(SqList& L) {
L.elems = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
if (!L.elems) exit(OVERFLOW);//若分配空间失败
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
释放顺序表
释放L.elem/表长与容量赋0
void DestroyList_Sq(SqList& L) {
free(L.elems);
L.length = 0;
L.listsize = 0;
}
插入操作
向表L中第i个位置插入元素e
Status ListInsert_Sq(SqList& L, int i, ElemType e) {
if (i<1 || i>L.length + 1) return ERROR;//判断插入位置不合法
if (L.length == L.listsize) {//判断表是否填满
ElemType* nnew;
nnew = (ElemType*)malloc(sizeof(ElemType) * (L.listsize + LISTINCREMENT));
if (!nnew) exit(OVERFLOW);//分配空间不成功
L.elems = nnew;
L.listsize += LISTINCREMENT;
}
//第i个位置 标号是i-1
ElemType* p = L.elems + i - 1;//第i个位置
ElemType* q = L.elems + L.length - 1;//最后一个元素的位置
while (q >= p) { *(q + 1) = *q; q--; }
*p = e;
L.length++;
return OK;
}
存取操作
直接取表L中第i个元素 并存在e中带回
void GetElem_Sq(SqList L, int i, ElemType& e) {//传e的引用
e = *(L.elems + i - 1);
}
返回顺序表长度
直接将成员变量L.length返回即可
int ListLength_Sq(SqList L) {
return L.length;
}
删除操作
删除表L中第i个元素 用e返回其值
int ListDelete_Sq(SqList& L, int i,ElemType &e) {
if (i<1 || i>L.length) return ERROR;
//删除第i个元素 下标i-1
ElemType* p = L.elems + i - 1;//*p=&L.elems[i-1];
ElemType* q = L.elems + L.length - 1;//最后一个元素
e = *p;
while (p < q) { *p = * (p + 1); p++; }
L.length--;
return OK;
}
合并两个有序顺序表
设两个指针分别遍历有序La Lb,将较小者插入到Lc的末尾
Status MergeList_Sq(SqList& La, SqList& Lb, SqList& Lc) {
if (Lc.listsize < (Lc.length + La.length + Lb.length)) {
ElemType* nnew;
nnew = (ElemType*)malloc(sizeof(ElemType) * (Lc.length + La.length + Lb.length));
if (!nnew) exit(OVERFLOW);
Lc.elems = nnew;
Lc.listsize = Lc.length + La.length + Lb.length;
}
ElemType* p, * q, * i;
i = Lc.elems + Lc.length;//最后一个元素的下一个
p = La.elems;
q = Lb.elems;
while (p < (La.elems + La.length) && q < (Lb.elems + Lb.length)) {
if (*p < *q) *i = *p, p++, i++;
else *i = *q, q++, i++;
}
while (p < (La.elems + La.length)) {
*i = *p;
p++; i++;
}
while (q < (Lb.elems + Lb.length)) {
*i = *q;
q++; i++;
}
Lc.length = Lc.length + La.length + Lb.length;;
return OK;
}
所有代码
#include <iostream>
using namespace std;
//顺序表存储结构定义
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR -1
typedef int ElemType;
typedef int Status;
//顺序表能访问随机 插入删除需大量移动数据 与数组类似
//结构体
typedef struct {
ElemType* elems;//存储空间基地址 顺序表要先开辟空间
int length; //表中元素个数
int listsize; //表容量大小
}SqList;
//空间分配/表长与容量初始化
Status InitList_Sq(SqList& L) {
L.elems = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
if (!L.elems) exit(OVERFLOW);//若分配空间失败
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
//释放L.elem/表长与容量赋0
void DestroyList_Sq(SqList& L) {
free(L.elems);
L.length = 0;
L.listsize = 0;
}
//向表L中第i个位置插入元素e
Status ListInsert_Sq(SqList& L, int i, ElemType e) {
if (i<1 || i>L.length + 1) return ERROR;//判断插入位置不合法
if (L.length == L.listsize) {//判断表是否填满
ElemType* nnew;
nnew = (ElemType*)malloc(sizeof(ElemType) * (L.listsize + LISTINCREMENT));
if (!nnew) exit(OVERFLOW);//分配空间不成功
L.elems = nnew;
L.listsize += LISTINCREMENT;
}
//第i个位置 标号是i-1
ElemType* p = L.elems + i - 1;//第i个位置
ElemType* q = L.elems + L.length - 1;//最后一个元素的位置
while (q >= p) { *(q + 1) = *q; q--; }
*p = e;
L.length++;
return OK;
}
//直接取表L中第i个元素 并存在e中带回
void GetElem_Sq(SqList L, int i, ElemType& e) {//传e的引用
e = *(L.elems + i - 1);
}
//直接将成员变量L.length返回即可
int ListLength_Sq(SqList L) {
return L.length;
}
//删除表L中第i个元素 用e返回其值
int ListDelete_Sq(SqList& L, int i,ElemType &e) {
if (i<1 || i>L.length) return ERROR;
//删除第i个元素 下标i-1
ElemType* p = L.elems + i - 1;//*p=&L.elems[i-1];
ElemType* q = L.elems + L.length - 1;//最后一个元素
e = *p;
while (p < q) { *p = * (p + 1); p++; }
L.length--;
return OK;
}
//设两个指针分别遍历有序La Lb,将较小者插入到Lc的末尾
Status MergeList_Sq(SqList& La, SqList& Lb, SqList& Lc) {
if (Lc.listsize < (Lc.length + La.length + Lb.length)) {
ElemType* nnew;
nnew = (ElemType*)malloc(sizeof(ElemType) * (Lc.length + La.length + Lb.length));
if (!nnew) exit(OVERFLOW);
Lc.elems = nnew;
Lc.listsize = Lc.length + La.length + Lb.length;
}
ElemType* p, * q, * i;
i = Lc.elems + Lc.length;//最后一个元素的下一个
p = La.elems;
q = Lb.elems;
while (p < (La.elems + La.length) && q < (Lb.elems + Lb.length)) {
if (*p < *q) *i = *p, p++, i++;
else *i = *q, q++, i++;
}
while (p < (La.elems + La.length)) {
*i = *p;
p++; i++;
}
while (q < (Lb.elems + Lb.length)) {
*i = *q;
q++; i++;
}
Lc.length = Lc.length + La.length + Lb.length;;
return OK;
}
int main()
{
SqList a, b, c;
InitList_Sq(a);
InitList_Sq(b);
InitList_Sq(c);
int t;
for (int i = 0; i < 5; i++) cin >> t, ListInsert_Sq(a, i+1, t);
for (int i = 0; i < 5; i++) cin >> t, ListInsert_Sq(b, i+1, t);
if (MergeList_Sq(a, b, c)) {
ElemType* p;
for (int i = 0; i < c.length; i++) cout << " " << c.elems[i];
}
return 0;
}