数据结构顺序表
两个顺序表的合并操作出现问题,具体如下:
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct {
int* elem; //指向元素存储空间的基地址
int length; //当前长度
}Sqlist;
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
Status InitList(Sqlist& L) {
L.elem = new int[MAXSIZE];
if (!L.elem)
exit(OVERFLOW); //存储分配失败
L.length = 0; //空表长度为0
return OK;
}
Status ListInsert(Sqlist& L, int i, int e) {
if (i<1 || i>L.length + 1)
return ERROR; //i值不合法
if (L.length == MAXSIZE)
return ERROR; //当前存储空间已满
for (int j = L.length; j >= i; j--)
L.elem[j] = L.elem[j - 1];//插入位置及之后的元素后移
L.elem[i - 1] = e; //插入e
L.length++; //表长增1
return OK;
}
Status ListDelete(Sqlist& L, int i, int& e)
{
if (i<1 || i>L.length + 1)
return ERROR; //i值不合法
e = L.elem[i - 1]; //被删除的元素赋值给e
for (int j = i; j < L.length; j++)
L.elem[j - 1] = L.elem[j]; //后继元素前移
L.length --; //表长减1
return OK;
}
int LocateElem(Sqlist L, int e)
{
for (int i = 0; i < L.length; i++) {
if (L.elem[i] == e)
return i +1; //找到则返回其位序
}
return 0; //未找到则返回0
}
void ListTraverse(Sqlist L)
{
for (int i = 0; i < L.length; i++) {
cout << L.elem[i] << " ";
}
cout << endl;
}
void BubbleSort(Sqlist& L)
{
for (int i = 0; i < L.length - 1; i++) {
for (int j = 0; j < L.length - 1 - i; j++) {
if (L.elem[j] > L.elem[j + 1]) {
int temp = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = temp;
}
}
}
}
Status GetElem(Sqlist L, int i, int * e) {
if (i<1 || i>L.length) {
return 0;
}
*e = L.elem[i -1];
return 1;
}
void Union(Sqlist &La, Sqlist Lb) {
//将所有在线性表Lb中但不在La中的数据元素插入到La中
for (int i = 1; i <= Lb.length; i++)
{
int e;
GetElem(Lb, i, &e);
if (!LocateElem(La,e)) ListInsert(La, ++La.length, e);
//La中不存在和e相同的数据元素,则插入之
}
}
Status MergeList(Sqlist La, Sqlist Lb, Sqlist& Lc)
{
int i = 0, j = 0, k = 0;
while (i < La.length && j < Lb.length) {
if (La.elem[i] <= Lb.elem[j])
{
Lc.elem[k++] = La.elem[i++]; //La中小的元素放进Lc
}
else
Lc.elem[k++] = Lb.elem[j++]; //Lb中小的元素放进Lc
}
while(i<La.length)
Lc.elem[k++]= La.elem[i++]; //插入La中剩余的元素
while (i < Lb.length)
Lc.elem[k++]= Lb.elem[j++]; //插入Lb中剩余的元素
Lc.length = k;
return OK;
}
int main()
{
Sqlist L;
InitList(L);
ListInsert(L, 1, 5);
ListInsert(L, 2, 3);
ListInsert(L, 3, 2);
ListInsert(L, 4, 6);
ListInsert(L, 5, 4);
cout << "原始顺序表:";
ListTraverse(L);
int e;
ListDelete(L, 3, e);
cout << "删除第3个元素后:";
ListTraverse(L);
int loc = LocateElem(L, 6);
cout << "元素6位于第" << loc << "个位置。" << endl;
BubbleSort(L);
cout << "排序后的顺序表:";
ListTraverse(L);
Sqlist La, Lb, Lc;
InitList(La);
InitList(Lb);
InitList(Lc);
ListInsert(La, 1, 3);
ListInsert(La, 2, 8);
ListInsert(Lb, 1, 2);
ListInsert(Lb, 2, 6);
cout << "\nLa顺序表:";
ListTraverse(La);
cout << "Lb顺序表:";
ListTraverse(Lb);
MergeList(La, Lb, Lc);
cout << "归并后的顺序表:";
ListTraverse(Lc);
Union(La, Lb);
cout << "合并后的顺序表:";
ListTraverse(La);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:栈
利用栈的先进后出实现
递归
利用递归先遍历后输出
我可以为你提供关于顺序表合并的一些思路和代码示例,详见下文:
首先,一个顺序表通常是由一个数组和一个记录当前元素个数的变量组成的,因为数组有固定大小,所以在合并两个顺序表时需要开辟一个新的数组来存储合并后的表。
定义一个新的顺序表结构体,包含一个数组和一个记录当前元素个数的变量:
typedef struct SeqList {
int data[MAX_SIZE];
int length;
} SeqList;
其中,MAX_SIZE
是顺序表所能容纳的最大元素个数。
接下来,我们需要实现一个函数来合并两个顺序表,具体步骤如下:
首先判断两个顺序表是否都非空,若其中有一个为空则直接返回另一个表;
初始化一个新的表,表的长度为两个表长度之和;
依次将两个表中的元素拷贝到新表中,最后再按顺序排序;
下面是具体的代码实现:
SeqList merge(SeqList list1, SeqList list2) {
SeqList newList;
if (list1.length == 0) {
return list2;
}
if (list2.length == 0) {
return list1;
}
newList.length = list1.length + list2.length;
int i, j, k;
i = j = k = 0; // i、j 为两个表的计数器,k 为新表的计数器
while (i < list1.length && j < list2.length) {
if (list1.data[i] <= list2.data[j]) {
newList.data[k++] = list1.data[i++];
} else {
newList.data[k++] = list2.data[j++];
}
}
while (i < list1.length) {
newList.data[k++] = list1.data[i++];
}
while (j < list2.length) {
newList.data[k++] = list2.data[j++];
}
return newList;
}
以上就是合并顺序表的一个基本实现,你可以根据自己的需要进一步优化和完善。
#include <iostream>
using namespace std;
#define MAXSIZE 100
typedef struct {
int* elem; //指向元素存储空间的基地址
int length; //当前长度
}Sqlist;
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
Status InitList(Sqlist& L) {
L.elem = new int[MAXSIZE];
if (!L.elem)
exit(OVERFLOW); //存储分配失败
L.length = 0; //空表长度为0
return OK;
}
Status ListInsert(Sqlist& L, int i, int e) {
if (i<1 || i>L.length + 1)
return ERROR; //i值不合法
if (L.length == MAXSIZE)
return ERROR; //当前存储空间已满
for (int j = L.length - 1; j >= i - 1; j--)
L.elem[j + 1] = L.elem[j]; //插入位置及之后的元素后移
L.elem[i - 1] = e; //插入e
L.length++; //表长增1
return OK;
}
Status ListDelete(Sqlist& L, int i, int& e)
{
if (i<1 || i>L.length)
return ERROR; //i值不合法
e = L.elem[i - 1]; //被删除的元素赋值给e
for (int j = i; j < L.length; j++)
L.elem[j - 1] = L.elem[j]; //后继元素前移
L.length--; //表长减1
return OK;
}
int LocateElem(Sqlist L, int e)
{
for (int i = 0; i < L.length; i++) {
if (L.elem[i] == e)
return i + 1; //找到则返回其位序
}
return 0; //未找到则返回0
}
void ListTraverse(Sqlist L)
{
for (int i = 0; i < L.length; i++) {
cout << L.elem[i] << " ";
}
cout << endl;
}
void BubbleSort(Sqlist& L)
{
for (int i = 0; i < L.length - 1; i++) {
for (int j = 0; j < L.length - 1 - i; j++) {
if (L.elem[j] > L.elem[j + 1]) {
int temp = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = temp;
}
}
}
}
Status GetElem(Sqlist L, int i, int* e) {
if (i<1 || i>L.length) {
return ERROR;
}
*e = L.elem[i - 1];
return OK;
}
void Union(Sqlist& La, Sqlist Lb) {
//将所有在线性表Lb中但不在La中的数据元素插入到La中
for (int i = 1; i <= Lb.length; i++)
{
int e;
GetElem(Lb, i, &e);
if (!LocateElem(La, e))
ListInsert(La, ++La.length, e);
//La中不存在和e相同的数据元素,则插入之
}
}
Status MergeList(Sqlist La, Sqlist Lb, Sqlist& Lc)
{
int i = 0, j = 0, k = 0;
Lc.elem = new int[La.length + Lb.length];
if (!Lc.elem)
exit(OVERFLOW); //存储分配失败
while (i < La.length && j < Lb.length) {
if (La.elem[i] <= Lb.elem[j])
{
Lc.elem[k++] = La.elem[i++]; //La中小的元素放进Lc
}
else
Lc.elem[k++] = Lb.elem[j++]; //Lb中小的元素放进Lc
}
while (i < La.length)
Lc.elem[k++] = La.elem[i++]; //插入La中剩余的元素
while (j < Lb.length)
Lc.elem[k++] = Lb.elem[j++]; //插入Lb中剩余的元素
Lc.length = k;
return OK;
}
int main()
{
Sqlist L;
InitList(L);
ListInsert(L, 1, 5);
ListInsert(L, 2, 3);
ListInsert(L, 3, 2);
ListInsert(L, 4, 6);
ListInsert(L, 5, 4);
cout << "原始顺序表:";
ListTraverse(L);
int e;
ListDelete(L, 3, e);
cout << "删除第3个元素后:";
ListTraverse(L);
int loc = LocateElem(L, 6);
cout << "元素6位于第" << loc << "个位置。" << endl;
BubbleSort(L);
cout << "排序后的顺序表:";
ListTraverse(L);
Sqlist La, Lb, Lc;
InitList(La);
InitList(Lb);
InitList(Lc);
ListInsert(La, 1, 3);
ListInsert(La, 2, 8);
ListInsert(Lb, 1, 2);
ListInsert(Lb, 2, 6);
cout << "\nLa顺序表:";
ListTraverse(La);
cout << "Lb顺序表:";
ListTraverse(Lb);
MergeList(La, Lb, Lc);
cout << "归并后的顺序表:";
ListTraverse(Lc);
Union(La, Lb);
cout << "合并后的顺序表:";
ListTraverse(La);
delete[] L.elem;
delete[] La.elem;
delete[] Lb.elem;
delete[] Lc.elem;
return 0;
}