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

OnWayGo OnWayGo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
第一个问题:
向单链表插入新元素的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;
}
...全文

相关推荐

数据结构期末考试题26.pdf

这些题目覆盖了数据结构的基础概念和实际应用,对于理解和掌握数据结构的原理与操作技巧具有重要意义。通过解答这些问题,学生可以检验自己对这些基本概念的理解程度,并提升解决实际问题的能力。

昨日θ青空

浙江大学计算机数据结构

单链表的表示与实现**- **定义**: 单链表是由一系列节点组成的数据结构,每个节点包含数据域和指向下一个节点的指针。- **实现**: - 创建节点类: 包含数据和指针。

qfly312

《数据结构》兰大2020年9月考试在线考核试题及答案.docx

单链表的结点组成- **单链表结点结构**: - 单链表中的每个结点通常由**数据域**和**指针域**两部分组成(选项D)。

爱我丶就要付出代价

2020-2021学年第二学期计算机科学与技术专业《数据结构(C语言)》期末考试题(试卷A).pdf

在一个具有 n 个结点的有序单链表中插入一个新结点并仍然有序的时间复杂度是 O(n)。4. 稀疏矩阵一般的压缩存储方法有两种,即三元组压缩和按行压缩。5.

m0_56368709

第二学期计算机科学与技术专业《数据结构(C语言)》期末考试题(试卷A).pdf

解答题部分涵盖了完全二叉树的性质、二叉树的遍历和重建、无向图的遍历和最小生成树、哈希表的构建和性能分析,以及几种排序算法(希尔排序、快速排序、堆排序)的第一趟结果。

筱筱笎琞
评论 8
收藏 已收藏
分享
返回顶部
OnWayGo
数据结构与算法 33,026 成员 35,334 内容