鱼C论坛

 找回密码
 立即注册
查看: 255|回复: 1

[已解决]有关索引顺序查找表的问题

[复制链接]
发表于 2023-11-29 22:30:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
创建一个索引表,求帮忙看看哪里出问题了,下面是部分代码
  1. status creatST(STable &ST,int n)
  2. {
  3.         int i,m,j;
  4.         bnode2 p;
  5.         ST.length=n;
  6.         printf("请输入每块中的最大值:");
  7.         for(i=0;i<n;i++){
  8.                 scanf("%d",&ST.r[i].Maxkey);
  9.         }
  10.         i=0;
  11.         while(i<n){
  12.                 printf("请输入第%d块的长度:",i+1);
  13.                 scanf("%d",&m);
  14.                 printf("请输入块中数据:");
  15.                 ST.r[i].head=(bnode2)malloc(sizeof(BNode));
  16.                 ST.r[i].head->next=NULL;
  17.                 p=ST.r[i].head->next;
  18.                 for(j=0;j<m;j++){
  19.                         p=(bnode2)malloc(sizeof(BNode));
  20.                         scanf("%d",&p->key);
  21.                         p->next=ST.r[i].head->next;
  22.                         ST.r[i].head->next=p;
  23.                 }
  24.                 i++;
  25.         }
  26.         return OK;
  27. }
  28. int main()
  29. {
  30.     printf("请输入你想创建索引表的块数:");
  31.         scanf("%d",&n);
  32.         creatST(ST,n);
  33.         for(i=1;i<=n;i++){
  34.                 printf("第%d块中数为:",i);
  35.                 while(p){
  36.                         p=ST.r[i-1].head->next;
  37.                         printf("%d ",p->key);
  38.                         p=p->next;
  39.                 }
  40.                 printf("\n");
  41. }
复制代码
最佳答案
2023-11-29 22:31:19
从代码中看,有几个问题是需要修改的:

1. 第5行的循环条件应该是`i<n`,而不是`inext`。
2. 第6行的循环条件应该是`j<m`,而不是`jm`。
3. 第7行和第8行创建的链表节点应该用`malloc`函数进行动态内存分配,而不是直接定义一个指针变量。
4. 第12行创建的链表节点应该插入到索引表的链表头部,而不是尾部。

下面是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 100
  4. #define OK 1
  5. #define ERROR 0
  6. typedef int KeyType;
  7. typedef struct {
  8.     KeyType key;
  9. } ElemType;
  10. typedef struct bnode {
  11.     ElemType data;
  12.     struct bnode *next;
  13. } bnode, *blist;
  14. typedef struct {
  15.     blist head;
  16.     int count;
  17.     int max;
  18. } idxnode, *idxlist;
  19. typedef struct {
  20.     idxlist r;
  21.     int length;
  22. } STable;
  23. int creatST(STable &ST, int n) {
  24.     int i, m, j;
  25.     bnode *p;
  26.     ST.r = (idxlist)malloc((n+1) * sizeof(idxnode));
  27.     printf("请输入每块中的最大值:");
  28.     for (i = 0; i < n; i++) {
  29.         printf("第 %d 块:", i + 1);
  30.         scanf("%d", &m);
  31.         ST.r[i].head = (blist)malloc(sizeof(bnode));
  32.         ST.r[i].head->next = NULL;
  33.         blist tail = ST.r[i].head;
  34.         for (j = 0; j < m; j++) {
  35.             p = (bnode*)malloc(sizeof(bnode));
  36.             printf("请输入第 %d 块中第 %d 个元素的值:", i + 1, j + 1);
  37.             scanf("%d", &(p->data.key));
  38.             tail->next = p;
  39.             tail = p;
  40.         }
  41.         ST.r[i].count = m;
  42.         ST.r[i].max = tail->data.key;
  43.     }
  44.     ST.length = n;
  45.     return OK;
  46. }
  47. int main() {
  48.     STable ST;
  49.     int i, n;
  50.     printf("请输入你想创建索引表的块数:");
  51.     scanf("%d", &n);
  52.     creatST(ST, n);
  53.     for (i = 0; i < ST.length; i++) {
  54.         printf("第 %d 块:", i + 1);
  55.         blist p = ST.r[i].head->next;
  56.         while (p != NULL) {
  57.             printf("%d ", p->data.key);
  58.             p = p->next;
  59.         }
  60.         printf("\n");
  61.     }
  62.     return 0;
  63. }
复制代码

这样,就能正常创建索引表,并输出每个块的数据了。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-11-29 22:31:19 | 显示全部楼层    本楼为最佳答案   
从代码中看,有几个问题是需要修改的:

1. 第5行的循环条件应该是`i<n`,而不是`inext`。
2. 第6行的循环条件应该是`j<m`,而不是`jm`。
3. 第7行和第8行创建的链表节点应该用`malloc`函数进行动态内存分配,而不是直接定义一个指针变量。
4. 第12行创建的链表节点应该插入到索引表的链表头部,而不是尾部。

下面是修改后的代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXSIZE 100
  4. #define OK 1
  5. #define ERROR 0
  6. typedef int KeyType;
  7. typedef struct {
  8.     KeyType key;
  9. } ElemType;
  10. typedef struct bnode {
  11.     ElemType data;
  12.     struct bnode *next;
  13. } bnode, *blist;
  14. typedef struct {
  15.     blist head;
  16.     int count;
  17.     int max;
  18. } idxnode, *idxlist;
  19. typedef struct {
  20.     idxlist r;
  21.     int length;
  22. } STable;
  23. int creatST(STable &ST, int n) {
  24.     int i, m, j;
  25.     bnode *p;
  26.     ST.r = (idxlist)malloc((n+1) * sizeof(idxnode));
  27.     printf("请输入每块中的最大值:");
  28.     for (i = 0; i < n; i++) {
  29.         printf("第 %d 块:", i + 1);
  30.         scanf("%d", &m);
  31.         ST.r[i].head = (blist)malloc(sizeof(bnode));
  32.         ST.r[i].head->next = NULL;
  33.         blist tail = ST.r[i].head;
  34.         for (j = 0; j < m; j++) {
  35.             p = (bnode*)malloc(sizeof(bnode));
  36.             printf("请输入第 %d 块中第 %d 个元素的值:", i + 1, j + 1);
  37.             scanf("%d", &(p->data.key));
  38.             tail->next = p;
  39.             tail = p;
  40.         }
  41.         ST.r[i].count = m;
  42.         ST.r[i].max = tail->data.key;
  43.     }
  44.     ST.length = n;
  45.     return OK;
  46. }
  47. int main() {
  48.     STable ST;
  49.     int i, n;
  50.     printf("请输入你想创建索引表的块数:");
  51.     scanf("%d", &n);
  52.     creatST(ST, n);
  53.     for (i = 0; i < ST.length; i++) {
  54.         printf("第 %d 块:", i + 1);
  55.         blist p = ST.r[i].head->next;
  56.         while (p != NULL) {
  57.             printf("%d ", p->data.key);
  58.             p = p->next;
  59.         }
  60.         printf("\n");
  61.     }
  62.     return 0;
  63. }
复制代码

这样,就能正常创建索引表,并输出每个块的数据了。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 07:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表