|
|
发表于 2023-11-29 22:31:19
|
显示全部楼层
从代码中看,有几个问题是需要修改的:
1. 第5行的循环条件应该是`i<n`,而不是`inext`。
2. 第6行的循环条件应该是`j<m`,而不是`jm`。
3. 第7行和第8行创建的链表节点应该用`malloc`函数进行动态内存分配,而不是直接定义一个指针变量。
4. 第12行创建的链表节点应该插入到索引表的链表头部,而不是尾部。
下面是修改后的代码:
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXSIZE 100
- #define OK 1
- #define ERROR 0
- typedef int KeyType;
- typedef struct {
- KeyType key;
- } ElemType;
- typedef struct bnode {
- ElemType data;
- struct bnode *next;
- } bnode, *blist;
- typedef struct {
- blist head;
- int count;
- int max;
- } idxnode, *idxlist;
- typedef struct {
- idxlist r;
- int length;
- } STable;
- int creatST(STable &ST, int n) {
- int i, m, j;
- bnode *p;
- ST.r = (idxlist)malloc((n+1) * sizeof(idxnode));
- printf("请输入每块中的最大值:");
- for (i = 0; i < n; i++) {
- printf("第 %d 块:", i + 1);
- scanf("%d", &m);
- ST.r[i].head = (blist)malloc(sizeof(bnode));
- ST.r[i].head->next = NULL;
- blist tail = ST.r[i].head;
- for (j = 0; j < m; j++) {
- p = (bnode*)malloc(sizeof(bnode));
- printf("请输入第 %d 块中第 %d 个元素的值:", i + 1, j + 1);
- scanf("%d", &(p->data.key));
- tail->next = p;
- tail = p;
- }
- ST.r[i].count = m;
- ST.r[i].max = tail->data.key;
- }
- ST.length = n;
- return OK;
- }
- int main() {
- STable ST;
- int i, n;
- printf("请输入你想创建索引表的块数:");
- scanf("%d", &n);
- creatST(ST, n);
- for (i = 0; i < ST.length; i++) {
- printf("第 %d 块:", i + 1);
- blist p = ST.r[i].head->next;
- while (p != NULL) {
- printf("%d ", p->data.key);
- p = p->next;
- }
- printf("\n");
- }
- return 0;
- }
复制代码
这样,就能正常创建索引表,并输出每个块的数据了。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 |
|