由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 定义 单链表 数组,会不会很奇怪
相关主题
这段C++程序有错吗?地址空间里的一个BYTE不能写入(是合法地址)
菜鸟读C++ STL源程序的疑问问个nontrivial Java问题
C & C++ mixing question大家新年好。 请教一个 c interview question
问个c++ struct和指针问题c ptr question
问一个关于C++指针的问题请教如何初始化这个C data structure Steque的object?
size不固定的struct怎么定义呀?good C++ open source project?
请教函数 INIT 怎么能free memorySTL里面#include的问题
初学C,对什么该free一直搞不明白don't understand this list (C++ STL)
相关话题的讨论汇总
话题: struct话题: node话题: clist话题: pvlist话题: temp
进入Programming版参与讨论
1 (共1页)
c**r
发帖数: 150
1
我想解决的问题是,比如有p个点,每个点都对应一个单链表,
struct node{
int value;
struct node *next;
}
struct node *plist=(struct node *)malloc(p*sizeof(struct node *));
错误总是出现,比如我要在第j个node往里面insert,程序返回的时候总是出错
首先我想问,以上定义是储存了,p个单链表的头指针吗?这是最好的储存方式吗?谢
谢。
b*******s
发帖数: 5216
2
应该是一个包含了p个单链表头指针的数组吧,你的insert是怎么做的?
og
发帖数: 227
3
delete the last *

【在 c**r 的大作中提到】
: 我想解决的问题是,比如有p个点,每个点都对应一个单链表,
: struct node{
: int value;
: struct node *next;
: }
: struct node *plist=(struct node *)malloc(p*sizeof(struct node *));
: 错误总是出现,比如我要在第j个node往里面insert,程序返回的时候总是出错
: 首先我想问,以上定义是储存了,p个单链表的头指针吗?这是最好的储存方式吗?谢
: 谢。

c**r
发帖数: 150
4

谢谢,我发现是我在这笔误了。谢谢

【在 og 的大作中提到】
: delete the last *
c**r
发帖数: 150
5

////////////////////////////////////////////////////////////////////
void node_insert(int Loc, struct node *pvlist, struct node *newvstr, int *
Clist)
//Clist记载了某个node对应的链表的长度
{
int i=1, clist=*Clist;
struct node *temp, *temp1;
temp=temp1=pvlist;

if(Loc==0){
if(temp->value==-1){
temp->value=newvstr->value;
}
else{
newvstr->next=temp->next;
temp=newvstr;
}
}
else{//问题出现在这!!
while(i temp1=temp1->next;
i++;
}
newvstr->next=temp1->next;
temp1->next=newvstr;
}
clist=clist+1;
*Clist=clist;
}
void test(struct vstr *vlist, int *clist){
// vlist=(struct node*)malloc(p*sizeof(struct node));
// clist=(int*)malloc(p*sizeof(int));
for(j=0;j ......
node_insert(loc, &vlist[j], newvstr, &clist[j])
......
}
}
当我想遍历view每个链表的时候,就会出现问题,就是会出现负数(绝对值很大的那种
),在不是首节点的位置。不知道是返回哪里错了。万分谢谢

【在 b*******s 的大作中提到】
: 应该是一个包含了p个单链表头指针的数组吧,你的insert是怎么做的?
G*****7
发帖数: 1759
6
if you could get by with STL, why reinvent the wheel?
vector> myVec(p);
myVec[p].push_back(3);

【在 c**r 的大作中提到】
: 我想解决的问题是,比如有p个点,每个点都对应一个单链表,
: struct node{
: int value;
: struct node *next;
: }
: struct node *plist=(struct node *)malloc(p*sizeof(struct node *));
: 错误总是出现,比如我要在第j个node往里面insert,程序返回的时候总是出错
: 首先我想问,以上定义是储存了,p个单链表的头指针吗?这是最好的储存方式吗?谢
: 谢。

c**r
发帖数: 150
7

谢谢,给了我关键字,STL。因为我不是学CS的,是统计的。大学的C++都忘得差不多了


【在 G*****7 的大作中提到】
: if you could get by with STL, why reinvent the wheel?
: vector> myVec(p);
: myVec[p].push_back(3);

l*********s
发帖数: 5409
8
for data processing? grap any scripting language instead of c/c++. It is
much more efficient for you.

【在 c**r 的大作中提到】
:
: 谢谢,给了我关键字,STL。因为我不是学CS的,是统计的。大学的C++都忘得差不多了
: 。

G*****7
发帖数: 1759
9
seeing that he's creating a variable-length linked list for every point p, i
fear his data processing task is not something uniform (matrix-like) or
structured (table-like). traversing that kind of data structure can be slow
with scripting languages. just think about for-looping cell arrays in matlab
...
i'd still recommend stl.

【在 l*********s 的大作中提到】
: for data processing? grap any scripting language instead of c/c++. It is
: much more efficient for you.

c**r
发帖数: 150
10

感谢回复。只是scripting languages,可能像python,我就更不会了~~

【在 l*********s 的大作中提到】
: for data processing? grap any scripting language instead of c/c++. It is
: much more efficient for you.

相关主题
size不固定的struct怎么定义呀?地址空间里的一个BYTE不能写入(是合法地址)
请教函数 INIT 怎么能free memory问个nontrivial Java问题
初学C,对什么该free一直搞不明白大家新年好。 请教一个 c interview question
进入Programming版参与讨论
c**r
发帖数: 150
11

i
slow
matlab
是的,就是因为不是uniform的,以为p很大的话建matrix会很浪费空间和搜索时间。但
是同学,可不可以再帮我看看我有什么问题,虽然我还是没用STL,不好意思哈,我有
点弱
之前的问题原因我找到了,因为我把newvstr free了,就出现了一系列问题。
然后我用对struct里面释放内存的问题就纠结了。这是我写的释放空间的函数
void vstr_destroy(struct node *pvlist)
{
struct node *temp;
temp=pvlist->next;

while(temp!=NULL){
pvlist->next=temp->next;
free(temp);
temp=pvlist->next;
}
free(pvlist);
// pvlist=NULL;
}
在主函数中,比如我先动态分配了空间,
struct node *pvlist=(struct node *)malloc(p*sizeof(struct node));
最后我调用以上函数想释放空间
for(i=0;i vstr_destroy(&pvlist[i]);
}
然后就出问题了,难道是vstr_destroy出现了问题?
----------------------------------------------------
还有每次在insert中,需要新建一个结点,使用了malloc,那为什么不需要在insert函
数中就free呢?不是malloc就要free嘛?但是free了链表就出现奇怪的数(我猜测是因
为free了就乱指了),所以在insert函数中不free是不出问题的吗?

【在 G*****7 的大作中提到】
: seeing that he's creating a variable-length linked list for every point p, i
: fear his data processing task is not something uniform (matrix-like) or
: structured (table-like). traversing that kind of data structure can be slow
: with scripting languages. just think about for-looping cell arrays in matlab
: ...
: i'd still recommend stl.

t****t
发帖数: 6806
12
...唉, 你这个差得太远了, 错误百出. 还是STL吧.

【在 c**r 的大作中提到】
:
: i
: slow
: matlab
: 是的,就是因为不是uniform的,以为p很大的话建matrix会很浪费空间和搜索时间。但
: 是同学,可不可以再帮我看看我有什么问题,虽然我还是没用STL,不好意思哈,我有
: 点弱
: 之前的问题原因我找到了,因为我把newvstr free了,就出现了一系列问题。
: 然后我用对struct里面释放内存的问题就纠结了。这是我写的释放空间的函数
: void vstr_destroy(struct node *pvlist)

c**r
发帖数: 150
13

哎,好吧~~,谢谢啊

【在 t****t 的大作中提到】
: ...唉, 你这个差得太远了, 错误百出. 还是STL吧.
G*****7
发帖数: 1759
14
debugging this could take longer than learning stl from scratch.
you just need to check out a few examples on std::vector and std::list.

【在 c**r 的大作中提到】
:
: 哎,好吧~~,谢谢啊

b***i
发帖数: 3043
15
同学,我还是那句话,你这个程序的目的是什么,说出来,然后再提你的解决方案 有
什么不对。要不然每人知道你的程序哪里不对,因为根本就不知道你的目的,也许你就
是想生成一个运行期的错误呢?

【在 c**r 的大作中提到】
:
: 哎,好吧~~,谢谢啊

c**r
发帖数: 150
16

谢谢,最后解决了。
void vstr_destroy(struct node *pvlist)
{
struct node *temp, *current;
current=pvlist->next;
pvlist->next=NULL;

while(current!=NULL){
temp=current->next;
free(current);
current=temp;
}
}
谢谢大家的回复和帮助!有什么问题我还会麻烦大家的

【在 b***i 的大作中提到】
: 同学,我还是那句话,你这个程序的目的是什么,说出来,然后再提你的解决方案 有
: 什么不对。要不然每人知道你的程序哪里不对,因为根本就不知道你的目的,也许你就
: 是想生成一个运行期的错误呢?

w*********u
发帖数: 392
17
这是个明白人

【在 b***i 的大作中提到】
: 同学,我还是那句话,你这个程序的目的是什么,说出来,然后再提你的解决方案 有
: 什么不对。要不然每人知道你的程序哪里不对,因为根本就不知道你的目的,也许你就
: 是想生成一个运行期的错误呢?

b*******s
发帖数: 5216
18
SGI的库直接有slist

【在 c**r 的大作中提到】
:
: 谢谢,最后解决了。
: void vstr_destroy(struct node *pvlist)
: {
: struct node *temp, *current;
: current=pvlist->next;
: pvlist->next=NULL;
:
: while(current!=NULL){
: temp=current->next;

c**r
发帖数: 150
19
我想解决的问题是,比如有p个点,每个点都对应一个单链表,
struct node{
int value;
struct node *next;
}
struct node *plist=(struct node *)malloc(p*sizeof(struct node *));
错误总是出现,比如我要在第j个node往里面insert,程序返回的时候总是出错
首先我想问,以上定义是储存了,p个单链表的头指针吗?这是最好的储存方式吗?谢
谢。
b*******s
发帖数: 5216
20
应该是一个包含了p个单链表头指针的数组吧,你的insert是怎么做的?
相关主题
c ptr questionSTL里面#include的问题
请教如何初始化这个C data structure Steque的object?don't understand this list (C++ STL)
good C++ open source project?auto_ptr_array.h 疑问
进入Programming版参与讨论
og
发帖数: 227
21
delete the last *

【在 c**r 的大作中提到】
: 我想解决的问题是,比如有p个点,每个点都对应一个单链表,
: struct node{
: int value;
: struct node *next;
: }
: struct node *plist=(struct node *)malloc(p*sizeof(struct node *));
: 错误总是出现,比如我要在第j个node往里面insert,程序返回的时候总是出错
: 首先我想问,以上定义是储存了,p个单链表的头指针吗?这是最好的储存方式吗?谢
: 谢。

c**r
发帖数: 150
22

谢谢,我发现是我在这笔误了。谢谢

【在 og 的大作中提到】
: delete the last *
c**r
发帖数: 150
23

////////////////////////////////////////////////////////////////////
void node_insert(int Loc, struct node *pvlist, struct node *newvstr, int *
Clist)
//Clist记载了某个node对应的链表的长度
{
int i=1, clist=*Clist;
struct node *temp, *temp1;
temp=temp1=pvlist;

if(Loc==0){
if(temp->value==-1){
temp->value=newvstr->value;
}
else{
newvstr->next=temp->next;
temp=newvstr;
}
}
else{//问题出现在这!!
while(i temp1=temp1->next;
i++;
}
newvstr->next=temp1->next;
temp1->next=newvstr;
}
clist=clist+1;
*Clist=clist;
}
void test(struct vstr *vlist, int *clist){
// vlist=(struct node*)malloc(p*sizeof(struct node));
// clist=(int*)malloc(p*sizeof(int));
for(j=0;j ......
node_insert(loc, &vlist[j], newvstr, &clist[j])
......
}
}
当我想遍历view每个链表的时候,就会出现问题,就是会出现负数(绝对值很大的那种
),在不是首节点的位置。不知道是返回哪里错了。万分谢谢

【在 b*******s 的大作中提到】
: 应该是一个包含了p个单链表头指针的数组吧,你的insert是怎么做的?
G*****7
发帖数: 1759
24
if you could get by with STL, why reinvent the wheel?
vector> myVec(p);
myVec[p].push_back(3);

【在 c**r 的大作中提到】
: 我想解决的问题是,比如有p个点,每个点都对应一个单链表,
: struct node{
: int value;
: struct node *next;
: }
: struct node *plist=(struct node *)malloc(p*sizeof(struct node *));
: 错误总是出现,比如我要在第j个node往里面insert,程序返回的时候总是出错
: 首先我想问,以上定义是储存了,p个单链表的头指针吗?这是最好的储存方式吗?谢
: 谢。

c**r
发帖数: 150
25

谢谢,给了我关键字,STL。因为我不是学CS的,是统计的。大学的C++都忘得差不多了


【在 G*****7 的大作中提到】
: if you could get by with STL, why reinvent the wheel?
: vector> myVec(p);
: myVec[p].push_back(3);

l*********s
发帖数: 5409
26
for data processing? grap any scripting language instead of c/c++. It is
much more efficient for you.

【在 c**r 的大作中提到】
:
: 谢谢,给了我关键字,STL。因为我不是学CS的,是统计的。大学的C++都忘得差不多了
: 。

G*****7
发帖数: 1759
27
seeing that he's creating a variable-length linked list for every point p, i
fear his data processing task is not something uniform (matrix-like) or
structured (table-like). traversing that kind of data structure can be slow
with scripting languages. just think about for-looping cell arrays in matlab
...
i'd still recommend stl.

【在 l*********s 的大作中提到】
: for data processing? grap any scripting language instead of c/c++. It is
: much more efficient for you.

c**r
发帖数: 150
28

感谢回复。只是scripting languages,可能像python,我就更不会了~~

【在 l*********s 的大作中提到】
: for data processing? grap any scripting language instead of c/c++. It is
: much more efficient for you.

c**r
发帖数: 150
29

i
slow
matlab
是的,就是因为不是uniform的,以为p很大的话建matrix会很浪费空间和搜索时间。但
是同学,可不可以再帮我看看我有什么问题,虽然我还是没用STL,不好意思哈,我有
点弱
之前的问题原因我找到了,因为我把newvstr free了,就出现了一系列问题。
然后我用对struct里面释放内存的问题就纠结了。这是我写的释放空间的函数
void vstr_destroy(struct node *pvlist)
{
struct node *temp;
temp=pvlist->next;

while(temp!=NULL){
pvlist->next=temp->next;
free(temp);
temp=pvlist->next;
}
free(pvlist);
// pvlist=NULL;
}
在主函数中,比如我先动态分配了空间,
struct node *pvlist=(struct node *)malloc(p*sizeof(struct node));
最后我调用以上函数想释放空间
for(i=0;i vstr_destroy(&pvlist[i]);
}
然后就出问题了,难道是vstr_destroy出现了问题?
----------------------------------------------------
还有每次在insert中,需要新建一个结点,使用了malloc,那为什么不需要在insert函
数中就free呢?不是malloc就要free嘛?但是free了链表就出现奇怪的数(我猜测是因
为free了就乱指了),所以在insert函数中不free是不出问题的吗?

【在 G*****7 的大作中提到】
: seeing that he's creating a variable-length linked list for every point p, i
: fear his data processing task is not something uniform (matrix-like) or
: structured (table-like). traversing that kind of data structure can be slow
: with scripting languages. just think about for-looping cell arrays in matlab
: ...
: i'd still recommend stl.

t****t
发帖数: 6806
30
...唉, 你这个差得太远了, 错误百出. 还是STL吧.

【在 c**r 的大作中提到】
:
: i
: slow
: matlab
: 是的,就是因为不是uniform的,以为p很大的话建matrix会很浪费空间和搜索时间。但
: 是同学,可不可以再帮我看看我有什么问题,虽然我还是没用STL,不好意思哈,我有
: 点弱
: 之前的问题原因我找到了,因为我把newvstr free了,就出现了一系列问题。
: 然后我用对struct里面释放内存的问题就纠结了。这是我写的释放空间的函数
: void vstr_destroy(struct node *pvlist)

相关主题
大家学习STL都是看SGI的文档么?菜鸟读C++ STL源程序的疑问
c++ stl里面有hash table吗?C & C++ mixing question
这段C++程序有错吗?问个c++ struct和指针问题
进入Programming版参与讨论
c**r
发帖数: 150
31

哎,好吧~~,谢谢啊

【在 t****t 的大作中提到】
: ...唉, 你这个差得太远了, 错误百出. 还是STL吧.
G*****7
发帖数: 1759
32
debugging this could take longer than learning stl from scratch.
you just need to check out a few examples on std::vector and std::list.

【在 c**r 的大作中提到】
:
: 哎,好吧~~,谢谢啊

b***i
发帖数: 3043
33
同学,我还是那句话,你这个程序的目的是什么,说出来,然后再提你的解决方案 有
什么不对。要不然每人知道你的程序哪里不对,因为根本就不知道你的目的,也许你就
是想生成一个运行期的错误呢?

【在 c**r 的大作中提到】
:
: 哎,好吧~~,谢谢啊

c**r
发帖数: 150
34

谢谢,最后解决了。
void vstr_destroy(struct node *pvlist)
{
struct node *temp, *current;
current=pvlist->next;
pvlist->next=NULL;

while(current!=NULL){
temp=current->next;
free(current);
current=temp;
}
}
谢谢大家的回复和帮助!有什么问题我还会麻烦大家的

【在 b***i 的大作中提到】
: 同学,我还是那句话,你这个程序的目的是什么,说出来,然后再提你的解决方案 有
: 什么不对。要不然每人知道你的程序哪里不对,因为根本就不知道你的目的,也许你就
: 是想生成一个运行期的错误呢?

w*********u
发帖数: 392
35
这是个明白人

【在 b***i 的大作中提到】
: 同学,我还是那句话,你这个程序的目的是什么,说出来,然后再提你的解决方案 有
: 什么不对。要不然每人知道你的程序哪里不对,因为根本就不知道你的目的,也许你就
: 是想生成一个运行期的错误呢?

b*******s
发帖数: 5216
36
SGI的库直接有slist

【在 c**r 的大作中提到】
:
: 谢谢,最后解决了。
: void vstr_destroy(struct node *pvlist)
: {
: struct node *temp, *current;
: current=pvlist->next;
: pvlist->next=NULL;
:
: while(current!=NULL){
: temp=current->next;

l*******b
发帖数: 2586
37
看着做得挺好呀

【在 c**r 的大作中提到】
:
: 谢谢,最后解决了。
: void vstr_destroy(struct node *pvlist)
: {
: struct node *temp, *current;
: current=pvlist->next;
: pvlist->next=NULL;
:
: while(current!=NULL){
: temp=current->next;

s*w
发帖数: 729
38
不是以练习编程为目的的话,自己设计基础数据结构很没道理。有这 debug 内存管理
的功夫,用 matlab, r, python 早完成了整个 project 了
1 (共1页)
进入Programming版参与讨论
相关主题
don't understand this list (C++ STL)问一个关于C++指针的问题
auto_ptr_array.h 疑问size不固定的struct怎么定义呀?
大家学习STL都是看SGI的文档么?请教函数 INIT 怎么能free memory
c++ stl里面有hash table吗?初学C,对什么该free一直搞不明白
这段C++程序有错吗?地址空间里的一个BYTE不能写入(是合法地址)
菜鸟读C++ STL源程序的疑问问个nontrivial Java问题
C & C++ mixing question大家新年好。 请教一个 c interview question
问个c++ struct和指针问题c ptr question
相关话题的讨论汇总
话题: struct话题: node话题: clist话题: pvlist话题: temp