帮忙看看两个顺序表归并函数哪里错了,查了一个晚上了...

chaoliu1024 2010-11-20 10:15:57
是严蔚敏《数据结构》书上的一段代码,运行却得不到理想结果。。。查了一个晚上了。。。
函数如下:
void MergeList(SqList La, SqList Lb, SqList& Lc)
{
int i = 1, j = 1, k = 0; // i为La逻辑地址的计数器,j为Lb的逻辑地址计数器,k为Lc的逻辑地址计数器
int La_len, Lb_len;
ElemType ai, bj;

InitList_Sq(Lc); // 初始化顺序表Lc
La_len = ListLength_Sq(La); // 得到顺序表La的长度
Lb_len = ListLength_Sq(Lb); // 得到顺序表La的长度

while ((i <= La_len) && (j <= Lb_len)) // La和Lb均非空
{
GetElem_Sq(La, i, ai); // 应该是while里有错误。。。
GetElem_Sq(Lb, j, bj);
if (ai <= bj)
{
InsertElem_Sq(Lc, ++k, ai);
++i;
}
else
{
InsertElem_Sq(Lc, ++k, bj);
++j;
}
}
while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, j++, bj);
InsertElem_Sq(Lc, ++k, bj);
}
}

运行结果:

顺便问下:CodeBlock断点调试,单步跟踪在哪里呀?
...全文
361 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2010-11-21
  • 打赏
  • 举报
回复
两处……
改过来就应该没问题了

while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(Lb, j++, bj);/*mark*/
InsertElem_Sq(Lc, ++k, bj);
}



Status GetElem_Sq(SqList L, int i, ElemType& e)
{
if (i < 1 || i > L.length) /*mark*/
return ERROR;
e = L.elem[i-1];
return OK;
}
cranium 2010-11-20
  • 打赏
  • 举报
回复
运行着MZ,VS不可以调试了,只有开虚拟机运行DEV-C++。
cranium 2010-11-20
  • 打赏
  • 举报
回复
下面是我的测试代码,刚开始你只贴了函数,所以其他的代码我自己补了点,保持含义一致就OK:

#include <iostream>
#include <assert.h>
using namespace std;


typedef int ElemType;

class SqList
{
public:
SqList(int n)
{
m_parr = new ElemType[n];
memset(m_parr,0,sizeof(ElemType)*n);
m_len = n;
}

SqList(SqList& list)
{
m_len = list.getlen();
m_parr = new ElemType[m_len];
for (int i=0;i<m_len;i++)
{
m_parr[i] = list.get(i);
}
}

~SqList()
{
delete m_parr;
}

int getlen() const
{
return m_len;
}

ElemType get(int i) const
{
assert(i<m_len);
return m_parr[i];
}

void set(int i,ElemType& val)
{
assert(i<m_len);
m_parr[i] = val;
}

void Display()
{
for (int i=0;i<m_len;i++)
{
cout<<m_parr[i]<<' ';
}
cout<<endl;
}

private:
int m_len;
ElemType* m_parr;
};

void InitList_Sq(SqList)
{

}

int ListLength_Sq(SqList& list)
{
return list.getlen();
}

void GetElem_Sq(SqList& list,int pos,ElemType& elem)
{
elem = list.get(pos-1);
}

void InsertElem_Sq(SqList& list,int pos,ElemType& elem)
{
list.set(pos-1,elem);
}

void MergeList(SqList& La, SqList& Lb, SqList& Lc)
{
int i = 1, j = 1, k = 0; // i为La逻辑地址的计数器,j为Lb的逻辑地址计数器,k为Lc的逻辑地址计数器
int La_len, Lb_len;
ElemType ai, bj;

InitList_Sq(Lc); // 初始化顺序表Lc
La_len = ListLength_Sq(La); // 得到顺序表La的长度
Lb_len = ListLength_Sq(Lb); // 得到顺序表La的长度

while ((i <= La_len) && (j <= Lb_len)) // La和Lb均非空
{
GetElem_Sq(La, i, ai); // 应该是while里有错误。。。
GetElem_Sq(Lb, j, bj);
if (ai <= bj)
{
InsertElem_Sq(Lc, ++k, ai);
++i;
}
else
{
InsertElem_Sq(Lc, ++k, bj);
++j;
}
}
while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(Lb, j++, bj);
InsertElem_Sq(Lc, ++k, bj);
}
}

int main()
{
SqList listA(3);
ElemType temp = 1;
listA.set(0,temp);
temp = 2;
listA.set(1,temp);
temp = 3;
listA.set(2,temp);

listA.Display();

SqList listB(2);
temp = 1;
listB.set(0,temp);
temp = 5;
listB.set(1,temp);

listB.Display();

SqList listC(5);
listC.Display();

MergeList(listA,listB,listC);

listC.Display();

system("pause");
}


DEV-C++下的运行结果

1 2 3
1 5
0 0 0 0 0
1 1 2 3 5
请按任意键继续. . .
modyaj 2010-11-20
  • 打赏
  • 举报
回复
看了哈 觉得是位置变量控制出了问题 我vc出问题了 不能调试了
cranium 2010-11-20
  • 打赏
  • 举报
回复

while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, j++, bj);
InsertElem_Sq(Lc, ++k, bj);
}


第二个while中
GetElem_Sq(La, j++, bj);
应该更改为
GetElem_Sq(Lb, j++, bj);
2220728 2010-11-20
  • 打赏
  • 举报
回复
程序没报告错误啊 我运行结果是1 1 1 2 2
Csuxiaowu 2010-11-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始化分配
#define LISTINCREMENT 10 // 顺序表存储空间的分配增量
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType* elem; // 存储空间基址
int length; // 当前分配长度
int listsize; // 当前分配的存储容量(以sizeof(ElemType)为单位)
} SqList; // 创建顺序表
Status CreateList_Sq(SqList& L, int n); // 顺序表La、Lb按值非递减排列,归并两表得到Lc,Lc也按值非递减排列
void MergeList(SqList La, SqList Lb, SqList& Lc); // 用e返回顺序表L中第i个数据元素的值
Status GetElem_Sq(SqList L, int i, ElemType& e); // 构造一个空的顺序表L
Status InitList_Sq(SqList& L); // 在顺序表L第i个位置插入元素e
Status InsertElem_Sq(SqList& L, int i, ElemType e); // 遍历顺序表L
void PrintList_Sq(SqList L); // 返回L的元素个数,即顺序表的长度
int ListLength_Sq(SqList L);
int main(void)
{
SqList La, Lb, Lc;
int na, nb;
printf("输入要创建表La元素个数:");
scanf("%d", &na);
printf("输入La中数据元素,值按非递减排列:");
CreateList_Sq(La, na); printf("\n输入要创建表Lb元素个数:");
scanf("%d", &nb);
printf("输入Lb中数据元素,值按非递减排列:");
CreateList_Sq(Lb, nb);
printf("\n顺序表La中元素为:");
PrintList_Sq(La);
printf("\n顺序表Lb中元素为:");
PrintList_Sq(Lb);
printf("\n\n合并两表结果为:");
MergeList(La, Lb, Lc);
PrintList_Sq(Lc);
return 0;
}
Status CreateList_Sq(SqList& L, int n)
{
int i;
InitList_Sq(L);
for (i = 0; i < n; i++)
{
L.length++;
scanf("%d", &(L.elem[i]));
}
return OK;
}
void MergeList(SqList La, SqList Lb, SqList& Lc)
{
int i = 1, j = 1, k = 0; // i为La逻辑地址的计数器,j为Lb的逻辑地址计数器,k为Lc的逻辑地址计数器
int La_len, Lb_len;
ElemType ai, bj;
InitList_Sq(Lc); // 初始化顺序表Lc
La_len = ListLength_Sq(La); // 得到顺序表La的长度
Lb_len = ListLength_Sq(Lb); // 得到顺序表La的长度
while ((i <= La_len) && (j <= Lb_len)) // La和Lb均非空
{
GetElem_Sq(La, i, ai);
GetElem_Sq(Lb, j, bj);
if (ai <= bj)
{
InsertElem_Sq(Lc, ++k, ai);
++i;
}
else
{
InsertElem_Sq(Lc, ++k, bj);
++j;
}
}
while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(Lb, j++, bj);
InsertElem_Sq(Lc, ++k, bj);
}
}
Status GetElem_Sq(SqList L, int i, ElemType& e)
{
if (i < 0 || i >L.length)
return ERROR;
e = L.elem[i-1];
return OK;
}
Status InitList_Sq(SqList& L)
{
L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
exit(OVERFLOW);
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE;
return OK;
}
Status InsertElem_Sq(SqList& L, int i, ElemType e)
{
ElemType *newbase, *p, *q;
if (i < 0 || i > L.length + 1)
return ERROR; // i值不合法
if (L.length >= L.listsize) // 当前存储空间已满,增加分配
{
newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase) // 分配失败
exit(OVERFLOW);
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
q = &(L.elem[i-1]);
for (p = &(L.elem[i-i]); p >= q; --p)
*(p + 1) = *p; // 插入位置及之后的元素右移
L.elem[i-1] = e; // 插入元素e
++L.length; // 表长增1
return OK;
}
void PrintList_Sq(SqList L)
{
int i;
if (L.length == 0)
printf("该表为空!\n");
else
for (i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
}
int ListLength_Sq(SqList L)
{
return L.length;
}


可以了 不过 相同的也会一起放进去 这个问题应该好解决了
mstlq 2010-11-20
  • 打赏
  • 举报
回复
吃宵夜去……
明天帮楼主看看……
Csuxiaowu 2010-11-20
  • 打赏
  • 举报
回复

while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, j++, bj); //-------------这里的La 是有问题的
InsertElem_Sq(Lc, ++k, bj);
}
}
Status GetElem_Sq(SqList L, int i, ElemType& e)
{
if (i < 0 || i >L.length) //--------这里的 = 是有问题的
return ERROR;
e = L.elem[i-1];
return OK;
}
la_feng 2010-11-20
  • 打赏
  • 举报
回复
应该 是那个插入函数的逻辑问题,我就没看懂你要怎么弄呀,把数据复制过去不就行了?那个合并后的表Lc还需要移动什么数据,想不明白,初始化为空的,一个个复制过去,有那么复杂。。
luciferisnotsatan 2010-11-20
  • 打赏
  • 举报
回复
调试下
hk2305621_1 2010-11-20
  • 打赏
  • 举报
回复
应该是你的GetElem_Sq()函数的实现有问题

你的运行结果表示你每次通过GetElem_Sq(La, i, ai);取到的值都是链表的第一个元素的值
chaoliu1024 2010-11-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 la_feng 的回复:]
代码贴全吧,不要只贴你认为有问题的,或许这段根本没问题,你要是肯定这段有问题就只调试这段就出来了,干嘛还问,别人是看了,但只有片段不知道问题所在
[/Quote]全贴了,麻烦看看
la_feng 2010-11-20
  • 打赏
  • 举报
回复
代码贴全吧,不要只贴你认为有问题的,或许这段根本没问题,你要是肯定这段有问题就只调试这段就出来了,干嘛还问,别人是看了,但只有片段不知道问题所在
chaoliu1024 2010-11-20
  • 打赏
  • 举报
回复
完整代码
本来是分三个文件,现在一起合并了,比较长
麻烦大家看看阿,拜托了
#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE 100 // 顺序表存储空间的初始化分配
#define LISTINCREMENT 10 // 顺序表存储空间的分配增量

#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef int Status;

typedef struct
{
ElemType* elem; // 存储空间基址
int length; // 当前分配长度
int listsize; // 当前分配的存储容量(以sizeof(ElemType)为单位)
} SqList;

// 创建顺序表
Status CreateList_Sq(SqList& L, int n);
// 顺序表La、Lb按值非递减排列,归并两表得到Lc,Lc也按值非递减排列
void MergeList(SqList La, SqList Lb, SqList& Lc);
// 用e返回顺序表L中第i个数据元素的值
Status GetElem_Sq(SqList L, int i, ElemType& e);
// 构造一个空的顺序表L
Status InitList_Sq(SqList& L);
// 在顺序表L第i个位置插入元素e
Status InsertElem_Sq(SqList& L, int i, ElemType e);
// 遍历顺序表L
void PrintList_Sq(SqList L);
// 返回L的元素个数,即顺序表的长度
int ListLength_Sq(SqList L);

int main(void)
{
SqList La, Lb, Lc;
int na, nb;

printf("输入要创建表La元素个数:");
scanf("%d", &na);
printf("输入La中数据元素,值按非递减排列:");
CreateList_Sq(La, na);

printf("\n输入要创建表Lb元素个数:");
scanf("%d", &nb);
printf("输入Lb中数据元素,值按非递减排列:");
CreateList_Sq(Lb, nb);

printf("\n顺序表La中元素为:");
PrintList_Sq(La);
printf("\n顺序表Lb中元素为:");
PrintList_Sq(Lb);

printf("\n\n合并两表结果为:");
MergeList(La, Lb, Lc);
PrintList_Sq(Lc);

return 0;
}

Status CreateList_Sq(SqList& L, int n)
{
int i;
InitList_Sq(L);
for (i = 0; i < n; i++)
{
L.length++;
scanf("%d", &(L.elem[i]));
}
return OK;
}

void MergeList(SqList La, SqList Lb, SqList& Lc)
{
int i = 1, j = 1, k = 0; // i为La逻辑地址的计数器,j为Lb的逻辑地址计数器,k为Lc的逻辑地址计数器
int La_len, Lb_len;
ElemType ai, bj;

InitList_Sq(Lc); // 初始化顺序表Lc
La_len = ListLength_Sq(La); // 得到顺序表La的长度
Lb_len = ListLength_Sq(Lb); // 得到顺序表La的长度

while ((i <= La_len) && (j <= Lb_len)) // La和Lb均非空
{
GetElem_Sq(La, i, ai);
GetElem_Sq(Lb, j, bj);
if (ai <= bj)
{
InsertElem_Sq(Lc, ++k, ai);
++i;
}
else
{
InsertElem_Sq(Lc, ++k, bj);
++j;
}
}
while (i <= La_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, i++, ai);
InsertElem_Sq(Lc, ++k, ai);
}
while (j <= Lb_len) // 将La剩余元素插入到Lc中
{
GetElem_Sq(La, j++, bj);
InsertElem_Sq(Lc, ++k, bj);
}
}

Status GetElem_Sq(SqList L, int i, ElemType& e)
{
if (i < 0 || i >= L.length)
return ERROR;
e = L.elem[i-1];
return OK;
}

Status InitList_Sq(SqList& L)
{
L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
exit(OVERFLOW);
L.length = 0; // 空表长度为0
L.listsize = LIST_INIT_SIZE;

return OK;
}

Status InsertElem_Sq(SqList& L, int i, ElemType e)
{
ElemType *newbase, *p, *q;

if (i < 0 || i > L.length + 1)
return ERROR; // i值不合法
if (L.length >= L.listsize) // 当前存储空间已满,增加分配
{
newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType));
if (!newbase) // 分配失败
exit(OVERFLOW);
L.elem = newbase; // 新基址
L.listsize += LISTINCREMENT; // 增加存储容量
}
q = &(L.elem[i-1]);
for (p = &(L.elem[i-i]); p >= q; --p)
*(p + 1) = *p; // 插入位置及之后的元素右移
L.elem[i-1] = e; // 插入元素e
++L.length; // 表长增1
return OK;
}

void PrintList_Sq(SqList L)
{
int i;
if (L.length == 0)
printf("该表为空!\n");
else
for (i = 0; i < L.length; i++)
printf("%d ", L.elem[i]);
}

int ListLength_Sq(SqList L)
{
return L.length;
}
chaoliu1024 2010-11-20
  • 打赏
  • 举报
回复
大家帮我看看程序的错误啊...
火狐狸 2010-11-20
  • 打赏
  • 举报
回复
CodeBlocks下单步调试的方法:
1. new project->console application->选择c or c++ ->filename->有个debug要钩上->写程序,就ok了
主要是建一个project
而且project要放在英文目录下面。

2. 首先程序项目的目录里不能有中文。编译后使光标定位在程序某处,按F4使程序运行到光标处,再按F7就可以单步调试,按Shift+F7单步进入。
Csuxiaowu 2010-11-20
  • 打赏
  • 举报
回复
把程序贴全 好调试
csc_xixi 2010-11-20
  • 打赏
  • 举报
回复
断点F5,单步F10,进入函数F11
Csuxiaowu 2010-11-20
  • 打赏
  • 举报
回复
断点 F5 单步 F7

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧