蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 | -->
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程 -->


    

蓝森林 http://www.lslnet.com 2006年8月16日 14:08

关于用指针的指针来实现数组操作的问题

有一纪录数据的文本文件,比如纪录的数据为若干位学生的信息。
当学生数目很大时,用链表搜索显然太费时。
因为要与我的处理模块配合,我用数组存储这些信息。
又因为并不能预先确定学生数量,所以我先创建学生信息的链表StudentList,链表创建完成后就可以知道学生数量N。(用链表,不管我取哪个数据,我都得从链表头开始搜索;而用数组则可以直接定位。我用的链表是为了生成数组,并不用链表来搜索。
)
由于N是程序运行后才知道的,所以不能建立数组Student[N];这时我考虑建立StudentNode **pstudentNode;
pstudentNode=(StudentNode **)malloc(N * sizeof(StudentNode *));
数据结构如下:
Student{
XXXXX;
}

StudentList{
Student *student;
Student *next;
}
信息存储的空间分配都在链表中实现。

....

得到链表:listhead,链表长度:N;
接着pstudent=(Student **)malloc(N * sizeof(Student *));使这个
内存快的没个元素都指向listhead中的一个Student,也是为了节省空间。如下:
StudentList *onestudent;
Student *pStu;
int i;
Student **pstudent;
pstudent=(Student **)malloc(N * sizeof(Student *));
onestudent = listhead;
i = 0;
while(onestudent){
pStu = (*pstudent) + i;
pStu = onestudent;
onestudent = onestudent->next;
}
.....

请个位发表一下看法。谢谢!



Re: 关于用指针的指针来实现数组操作的问题

好像没有什么实质性的改动。

如果使用数组的话,执行插入/删除操作怎么办?

Re: 关于用指针的指针来实现数组操作的问题

链表一生成,它的长度就不会有变化。所以数组也不涉及插入/删除操作。而内容可以在链表中修改,因为数组元素是指向链表的一个节点。
谢谢。

Re: 关于用指针的指针来实现数组操作的问题

难道是因为从文件中读取学生纪录,才使用链表吗?
为什么不能直接定义数组呢?难道不能够在文件开始增加一个存放长度的字段吗?

Re: 关于用指针的指针来实现数组操作的问题

可是这个文件并不提供长度。
所以我想先建立链表,根据链表长度创建数组。
谢谢!

Re: 关于用指针的指针来实现数组操作的问题

使用STL的sequence如何?

Re: 关于用指针的指针来实现数组操作的问题

STL我还不懂。:(

Re: 关于用指针的指针来实现数组操作的问题

研究一下吧,反正你在程序中不会用到太多地方。
在读文件,建立数组,查找,遍历时,sequence集数组和链表的优点,非常容易使用。

Re: 关于用指针的指针来实现数组操作的问题

得到链表:listhead,链表长度:N;
接着pstudent=(Student **)malloc(N * sizeof(Student *));使这个
内存快的没个元素都指向listhead中的一个Student,也是为了节省空间。如下:
StudentList *onestudent;
Student *pStu;
int i;
Student **pstudent;
pstudent=(Student **)malloc(N * sizeof(Student *));
onestudent = listhead;
i = 0;
while(onestudent){
pStu = (*pstudent) + i;
pStu = onestudent;
onestudent = onestudent->next;
}
.....


main(){
...
handle(pstudent);
....
}

这样似乎不稳定。传到handle()的pstudent,在(数组中)移动时,如:
Student *stu;
int i;
i = 0;
while(i<N){
stu = (*pstudent) + i;
i++;
}
这样有时不能取得stu里的信息。

Re: 关于用指针的指针来实现数组操作的问题

非常感谢你。
只是现在我正为这个急。暂时没时间去学

Re: 关于用指针的指针来实现数组操作的问题

while(onestudent){
pStu = (*pstudent) + i;
pStu = onestudent;
onestudent = onestudent->next;
}

应当改成:
whil (onestudent)
{
*(*pstudent + i) = onestudent;
onestudent = onestudent->next;
}

Re: 关于用指针的指针来实现数组操作的问题



#include <vector>

struct student
{
... ...
};

typedef std::vector<student> studentarray;

//----------------------------------

studentarray s1;

student x;
... ...
s1.push_back(x);
... ...

//use s1[index] to rand access student

// use s1.size() to get student count



Re: 关于用指针的指针来实现数组操作的问题

万分感激!
我竟然载在这里了。
再次谢过JamesLiuJiang


Re: 关于用指针的指针来实现数组操作的问题

pstudent=(Student **)malloc(N * sizeof(Student *)); 这句错了,应改为:pstudent=(Student **)malloc(N );
大意啊。

Re: 真的糊涂了。

Student{
char *name;
.......;
}

StudentList{
Student *student;
Student *next;
}
StudentList *onestudent;
int i;
Student **pstudent;
pstudent=(Student **)malloc(N );
onestudent = listhead;
i = 0;
while(onestudent){
(*pstudent) + i = onestudent;
onestudent = onestudent->next;
i++;
}
.....
似乎老是不稳定,得到的(*pstudent) + i)指向的name指针会发生改变?难道while(onestudent)有误?
另:
运行pstudent=(Student **)malloc(N );
后,*pstudent+i是什么?
而运行
pstudent=(Student **)malloc(N );
memset(pstudent,0,N);
后,*pstudent+i又是什么?
前后我看到的是一样的结果。
糊涂了。

Re: 真的糊涂了。

while循环内部,没有i++;

Re: 真的糊涂了。

抱歉,是打错了,while循环内已经有i++;

Re: 真的糊涂了。

pstudent=(Student **)malloc(N );
memset(pstudent,0,N); <===错误
onestudent = listhead;
i = 0;
while(onestudent){
(*pstudent + i)->name = onestudent->name;
(*pstudent + i)->... = onestudent->....;
.....
onestudent = onestudent->next;
i++;
}
.....
如果有memset(pstudent,0,N);,则while循环里的赋值是错误的。

Re: 真的糊涂了。

搞定了吗?

Re: 关于用指针的指针来实现数组操作的问题

Student **pstudent;
pstudent=(Student **)malloc(N * sizeof(Student *)); <==(1)
pstudent=(Student **)malloc(N); <==(2)

以上(1)、(2)哪个声明是正确的?

动态数组

你可以定义动态数组啊!

首先定义一个常数N=3000

Student *StudentList, *p;
int i=0,j=1;

StudentList = (Student *)malloc(N*sizeof(Student));
p = StudentList;

while(!EndRead)
{
p->xxxx1 = xxxx1;
p->xxxx2 = xxxx2;
p->xxxx3 = xxxx3;
p++;
i++;
if( i==j*N )
{
j++;
StudentList = (Student *)realloc(j*N*sizeof(Student));
p=StudentList[j*N];
}
}
StudentList[n] 就是 第n个加入进来的学生的数据

代码没有测试,大体就是这个样子。

Re: 关于用指针的指针来实现数组操作的问题

while(onestudent){
pStu = (*pstudent) + i; ===> pStu = *pstudent[ i ];
pStu = onestudent;
onestudent = onestudent->next;
}


Re: 关于用指针的指针来实现数组操作的问题

The first one is right.

你期望的结果是让pstudent指向的是一个存放Student *的数组。每一个Student *的占用空间应该是sizeof(Student *) = 4;
第2种做法,仅仅给每一个指针分配了一个字节的空间,这是不够的。
这样的话,如果N=400,那么在容纳了100个Student*之后,空间就用完了,然后再增加一个指针的话,Core dump。。

Re: 关于用指针的指针来实现数组操作的问题

是的,我一开始就这样声明,但当在我学的Student结构,N>=6时core dump!糊涂了。

Re: 动态数组

谢谢。
这种做法的可行性,我一直怀疑。因为我这里的N有时是几十,有时是上百万。

Re: 关于用指针的指针来实现数组操作的问题

while(onestudent){
pStu = (*pstudent) + i;
pStu = onestudent;
onestudent = onestudent->next;
}

应当改成:
whil (onestudent)
{
*(*pstudent + i) = onestudent;
onestudent = onestudent->next;
i++;
}

如你所讲的,修该后,说“incompatible types in assignment”若改为:
whil (onestudent)
{
*(*pstudent + i) = *onestudent;
onestudent = onestudent->next;
}
则编译通过。


Re: 关于用指针的指针来实现数组操作的问题

呵呵,没注意到变量类型的匹配。


但是你的改动,好像也有问题,运行会不会出问题?

>若改为:
>whil (onestudent)
>{
>*(*pstudent + i) = *onestudent;
>onestudent = onestudent->next;
>}
>则编译通过。

红色的一行,建议改成:
(*pstudent + i) = onestudent->student;//呵呵,看一下
//studentlist的定义


Re: 关于用指针的指针来实现数组操作的问题

如果是按(*pstudent + i) = onestudent->student;怎要重新设计Student结构,我曾这样做(*pstudent + i)->name = onestudent->name;但这样做的话,(*pstudent + i)并没有得到地址。

Re: 关于用指针的指针来实现数组操作的问题

别忘了onestudent的定义:
StudentList *onestudent;

你上面的程序离的太远了,看起来太费劲了。
等会儿,最好把相关的段落整理一下,重新贴一下。



Re: 关于用指针的指针来实现数组操作的问题

呵呵,抱歉,我自己都弄乱了。
是的,(*pstudent + i) = onestudent->student;我也曾试过,也不行!编译器是cc(非CC)。
过会我整理,重新发一个帖子。

建议你采用树形结构存储(平衡二杈树)

如果超过几十万的话,就一定要用链表来做了!

可是如果使用链表来做,你检索的速度就会很慢,所以建议你根据数据的ID,建立树形存储结构,提高检索速度

Re: 关于用指针的指针来实现数组操作的问题

依你的原意:

假设得到链表list, 长度n; 构造数组pstudent, 使pstudent中的每个元素分别指向list中的Student


Student **pstudent, **pp;
StudentList *q;

pstudent = (Student **)malloc(n * sizeof(Student *));

for (pp = pstudent, q = list; q; q = q->next)
*pp++ = q->student;


Re: 关于用指针的指针来实现数组操作的问题

这样应该是可以了。



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com