请高手帮我解答关于单链表的创建和稀疏矩阵的哈希解法的问题

OnWayGo 2004-04-02 09:14:04
第一个问题:
向单链表插入新元素的sls_store中的参数start.last为何要定义成二级间址的指针,直接定义成一级间址的指针不也可以吗?
创立单链表时的函数如下:
struct address {
char name[40];
char street[40];
char city[20];
struct address *next; /* link to next address */
}info;

/*store in sorted order*/
void sls_store(struct address *i,/*new element to store*/
struct address **start,/*start of list*/
struct address **last) /*end of list*/
{
struct address *old,*p;
p=*start;
if(!*last){/*first element in list */
i->next=Null;
*last=i;
*start=i;
return;
}
old=null;
while(p){
if(strcmp(p->name,i->name)<0{
old=p;
p=p->next;
}
else{
if(old){/*goes in middle */
old->next=i;
i->next=p;
return;
}
i->next=p; /*new first element */
*start=i;
return;
}
}
(*last)->next=i; /*put on end*/
i->next=null;
*last=i;
}

第二个问题:
对于用散列(hashing)方法处理稀疏数组(矩阵)问题:
我觉得在函数store 中的if 条件中只应该有primary[h].index==-1这一个条件不应该有primary[h].index==loc这一个条件:
具体函数如下:
结构数组,主(primary)数组如下:
#define MAX 260
struct htype{
int index; /*logical index */
int val; /* actual value of the array element */
struct htype *next /* pointer to next value with same hash */
}primary[MAX];

/* Initialize the hash array */
void init(void)
{
register int i;
for(i=0;i<MAX;i++){
primary[i].index=-1;
primary[i].next=NULL; /*null chain */
primary[i].val=0;
}
}

/*Compute hash and store value */
void store(char *cell_name,int v)
{
int h,loc;
struct htype *p;
/*produce the hash value */
loc=*cell_name -'A'; /*colume*/
loc+=(atoi(&cell_name[1]-1)*26; /*rows*width + columns */
h=loc/10; /*hash*/

/*store in the location unless full or store there if logical indexes agree -i.e.,update. */
/*我就是在下面这个IF条件中有问题,我觉得只要第一个条件就可以了,不需要再或一个条件*/
if(primary[h].index==-1||primary[h].index==loc){
primary[h].index=locp;
primary[h].val=v;
return;
}
/*otherwise,create or add to collision list */
p=(struct htype *)malloc(sizeof(struct htype));
if(!p){
printf("Out of Memory \n");
return;
}
p->index=loc;
p->val=v;
slstore(p,&primary[h]);
}

/*add elements to the collision list,*/
void slstore(struct htype *i,struct htype *start)
{
struct htype *old,*p;
old=startp;
/*find end of list */
while(start){
old=start;
start=start->next;
}
/*link in new entry */
old->next=i;
i->next=NULL;
}
...全文
123 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
OnWayGo 2004-04-03
  • 打赏
  • 举报
回复
to aheadyes() :
谢谢,我去看看
OnWayGo 2004-04-03
  • 打赏
  • 举报
回复
对于第二个问题,我觉得应改第二个条件为创建冲突表的判断条件:即将以下内容:
/*store in the location unless full or store there if logical indexes agree -i.e.,update. */
/*我就是在下面这个IF条件中有问题,我觉得只要第一个条件就可以了,不需要再或一个条件*/
if(primary[h].index==-1||primary[h].index==loc){
primary[h].index=locp;
primary[h].val=v;
return;
}
/*otherwise,create or add to collision list */
p=(struct htype *)malloc(sizeof(struct htype));
if(!p){
printf("Out of Memory \n");
return;
}
p->index=loc;
p->val=v;
slstore(p,&primary[h]);
}

改成:
/*store in the location unless full or store there if logical indexes agree -i.e.,update. */
if(primary[h].index==-1){
primary[h].index=locp;
primary[h].val=v;
return;
}
/*otherwise,create or add to collision list */
if(primary[h].index==loc) {
//因为这就表明主数组已被占用了,应该在冲突表中放数据了
p=(struct htype *)malloc(sizeof(struct htype));
if(!p){
printf("Out of Memory \n");
return;
}
p->index=loc;
p->val=v;
slstore(p,&primary[h]);
}
}


我改得对吗?
ForestTraveler 2004-04-03
  • 打赏
  • 举报
回复
up
OnWayGo 2004-04-03
  • 打赏
  • 举报
回复
而原来start和last指向的元素的地址不变,变的是start和last内部保存的地址。
OnWayGo 2004-04-03
  • 打赏
  • 举报
回复
to aheadyes() :
我想明白了,如果是一级间址改变的是变量的具体值,即这里的初始首尾结构的内部数值,即name等的数值,而如果用二级间址改变的是start和last指针的内部地址信息,即指向的结构地址数值被改变,即使得start和last指向新的地址,即随着新插入的新元素的地址情况而响应改变。
我说的对吗?
aheadyes 2004-04-02
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2780/2780500.xml?temp=.3261988

有关一级,2级指针的讨论
OnWayGo 2004-04-02
  • 打赏
  • 举报
回复
to aheadyes() :
但是我觉得传递start,last一级间址的指针实际效果是一样的啊!
aheadyes 2004-04-02
  • 打赏
  • 举报
回复
第一题:
指针可以改变变量的值,指针也是变量.
当你要改变指针变量时,当然要用指向指针的指针了.如果在c++中可以用指针的引用

void sls_store(struct address *i,/*new element to store*/
struct address **start,/*start of list*/
struct address **last) /*end of list*/

可能会改变**start, **last.

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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