数据结构基础(C++版)(张力译版)校正 之二 .
2012-11-20 11:29
615 查看
page 155--156
1. 很显然 155 页的倒数第二行的 if(!out[i]) 应改为:if(!out[j]);
2. 156 页的关于delete delnode; 因为在前面的出栈入栈操作时打乱了原来的链表所以通过此处循环释放内存不可取,
我想,咱们可以在出栈操作后释放内存。例如:
if(!top)break;
x=first[top->data];
ENode *t=top;
top=top->link;
delete t;
另外,不需入栈的节点也要释放:
if (!out[j])
{
cout<<","<<j;
out[j]=true;
ENode *y=x->link;
x->link=top;
top=x;
x=y;
}
else
{
ENode *t=x;
x=x->link;
delete t;
}
一下是我的代码供参考:
#include "ENode.h"
#include <iostream>
using namespace std;
void Equivalence()
{
cout<<"input the numbers of the pairs :"<<endl;
int n=0;
do
{
cin>>n;
if (n>0)break;
else
cout<<"input the numbers of the pairs(must bigger than 0):"<<endl;
} while (1);
ENode **first=new ENode*
;
bool *out=new bool
;
fill(out,out+n,false);
// fill(first,first+n,0);
memset(first,0,n*sizeof(ENode*));
cout<<"input the pairs :"<<endl;
int i;
int t1,t2;
while(cin>>t1>>t2)
{
first[t1]=new ENode(t2,first[t1]);
first[t2]=new ENode(t1,first[t2]);
}
//找等价类
for (i=0;i<n;i++)
{
if (!out[i])
{
cout<<endl<<"A new class :";
cout<<i;
out[i]=true;
ENode *x=first[i];
ENode *top=0;
while (1)
{
while (x)
{
int j=x->data;
if (!out[j])
{
cout<<","<<j;
out[j]=true;
ENode *y=x->link;
x->link=top;
top=x;
x=y;
}
else
{
ENode *t=x;
x=x->link;
delete t;
}
}
if(!top)break;
x=first[top->data];
ENode *t=top;
top=top->link;
delete t;
}
}
}
// for (i=0;i<n;i++)
// while (first[i])
// {
// ENode *delnode=first[i];
// first[i]=delnode->link;
// delete delnode;
// }
delete[] first;
delete [] out;
}
///示例:
input the numbers of the pairs :
5
input the pairs :
0 1
2 3
1 4
4 0
^Z
A new class :0,4,1
A new class :2,3
Press any key to continue
1. 很显然 155 页的倒数第二行的 if(!out[i]) 应改为:if(!out[j]);
2. 156 页的关于delete delnode; 因为在前面的出栈入栈操作时打乱了原来的链表所以通过此处循环释放内存不可取,
我想,咱们可以在出栈操作后释放内存。例如:
if(!top)break;
x=first[top->data];
ENode *t=top;
top=top->link;
delete t;
另外,不需入栈的节点也要释放:
if (!out[j])
{
cout<<","<<j;
out[j]=true;
ENode *y=x->link;
x->link=top;
top=x;
x=y;
}
else
{
ENode *t=x;
x=x->link;
delete t;
}
一下是我的代码供参考:
#include "ENode.h"
#include <iostream>
using namespace std;
void Equivalence()
{
cout<<"input the numbers of the pairs :"<<endl;
int n=0;
do
{
cin>>n;
if (n>0)break;
else
cout<<"input the numbers of the pairs(must bigger than 0):"<<endl;
} while (1);
ENode **first=new ENode*
;
bool *out=new bool
;
fill(out,out+n,false);
// fill(first,first+n,0);
memset(first,0,n*sizeof(ENode*));
cout<<"input the pairs :"<<endl;
int i;
int t1,t2;
while(cin>>t1>>t2)
{
first[t1]=new ENode(t2,first[t1]);
first[t2]=new ENode(t1,first[t2]);
}
//找等价类
for (i=0;i<n;i++)
{
if (!out[i])
{
cout<<endl<<"A new class :";
cout<<i;
out[i]=true;
ENode *x=first[i];
ENode *top=0;
while (1)
{
while (x)
{
int j=x->data;
if (!out[j])
{
cout<<","<<j;
out[j]=true;
ENode *y=x->link;
x->link=top;
top=x;
x=y;
}
else
{
ENode *t=x;
x=x->link;
delete t;
}
}
if(!top)break;
x=first[top->data];
ENode *t=top;
top=top->link;
delete t;
}
}
}
// for (i=0;i<n;i++)
// while (first[i])
// {
// ENode *delnode=first[i];
// first[i]=delnode->link;
// delete delnode;
// }
delete[] first;
delete [] out;
}
///示例:
input the numbers of the pairs :
5
input the pairs :
0 1
2 3
1 4
4 0
^Z
A new class :0,4,1
A new class :2,3
Press any key to continue
相关文章推荐
- 数据结构基础(C++版)(张力译版)校正 之三 .
- 数据结构基础(C++版)(张力译版)校正 之一
- 从零开始系列-R语言基础学习笔记之二 数据结构(一)
- 从零开始系列-R语言基础学习笔记之二 数据结构(二)
- 从零开始系列-R语言基础学习笔记之二 数据结构(二)
- [C++]数据结构:平衡的二叉搜索树之AVL树的结构特点与基础插入删除操作
- c++ 基础知识浅谈之二处理数据
- 从零开始系列-R语言基础学习笔记之二 数据结构(一)
- 数据结构基础:c++实现单向链表
- C++存储数据结构之二CArray
- 程序员面试宝典之数据结构基础----C++两个栈实现一个队列功能
- 算法与数据结构基础8:C++实现有向图——邻接表存储
- 数据结构基础——循环队列的C++实现
- 算法与数据结构基础4:C++二叉树实现及遍历方法大全
- c++基础(一):数据类型和结构
- C/C++基础知识02---数据结构
- 数据结构基础笔记(三)【严蔚敏】
- C数据结构基础
- 第六章:数据结构基础。第一部分
- 【C++基础学习】数据封装、构造函数