指出并修改错误#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> typedef struct num { float a; //系数 int b; //指数 struct num* next; } *num; struct LinkList // 链表类型 { num head; // 分别指向线性链表中的头结点和最后一个结点 int len; // 指示线性链表中数据元素的个数 }; struct LinkList* init(struct LinkList* list) //创建空链表 { list = (struct LinkList*)malloc(sizeof(struct LinkList)); list->len = 0; list->head = (num)malloc(sizeof(struct num)); list->head->next = NULL; return list; }; void compare(struct LinkList* list, float a, int b) //比较指数 { int i = 0; num p = list->head; for (i; i <= list->len; i++) { if (b > p->b) p = p->next; else if (b == p->b) { p->a += a; break; } else { add(list, i, a, b); //插入 break; } } if (i > list->len) add(list, i, a, b); //添加到最后一个 }; void listadd(struct LinkList *list, int index, float a, int b) //添加新的指数项 { num p = list->head, *s; int i; for (i = -1; i < index - 1; i++) { p = p->next; } s = (num)malloc(sizeof(struct num)); s->a = a; s->b = b; s->next = p->next; p->next = s; list->len++;}; int main() { //指数升序查找 struct LinkList* lista, * listb; lista = init(lista); listb = init(listb); int n, b; float a; scanf("%d", &n); for (int i = 0; i < n; i++) //lista { scanf("%f%d", &a, &b); compare(lista, a, b); } scanf("%d", &n); for (int i = 0; i < n; i++) //listb { scanf("%f%d", &a, &b); compare(listb, a, b); } return 0; }
时间: 2024-01-24 11:01:39 浏览: 27
在函数`void listadd(struct LinkList *list, int index, float a, int b)`中,变量`s`是指向`num`类型的指针,所以在分配内存的时候应该使用`malloc(sizeof(struct num))`而不是`malloc(sizeof(num))`。因此需要将`s = (num)malloc(sizeof(num));`修改为`s = (num)malloc(sizeof(struct num));`。
相关问题
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #define PHONE_SIZE 50 #define NAME_SIZE 20 typedef struct lxr { char phone[PHONE_SIZE]; char name[NAME_SIZE]; }lxr; int main() { lxr a[10]; a[0].phone = "liujunjie"; printf("%s", a[0].phone); return 0; }
这段代码存在一些问题。首先,您不能直接将字符串赋值给字符数组。其次,如果您想将字符串复制到字符数组中,您应该使用strcpy函数而不是直接赋值运算符。另外,还需要注意将字符串赋值给字符数组时,字符数组的大小应该足够大以容纳整个字符串。以下是修改后的代码:
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PHONE_SIZE 50
#define NAME_SIZE 20
typedef struct lxr {
char phone[PHONE_SIZE];
char name[NAME_SIZE];
} lxr;
int main()
{
lxr a[10];
strcpy(a[0].phone, "liujunjie"); // 使用strcpy函数将字符串复制给字符数组
printf("%s", a[0].phone);
return 0;
}
```
这样,代码就能正常运行并输出"liujunjie"。
请将LinkList &L改成LinkList *L而不出bug:#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> typedef struct LNode { int data;//数据域 struct LNode *next;//指针域 }LNode,LinkList; bool InitList(LinkList &L)//初始化单链表 { L = (LNode)malloc(sizeof(LNode));//分配一个头节点 if (L == NULL) return false;//内存不足,分配失败 L->next = NULL;//头节点之后还没有节点 return true; }
将代码中的"LinkList &L"改为"LinkList *L"即可,修改后的代码如下:
```
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode {
int data;//数据域
struct LNode* next;//指针域
}LNode, * LinkList;
bool InitList(LinkList* L)//初始化单链表
{
*L = (LNode*)malloc(sizeof(LNode));//分配一个头节点
if (*L == NULL)
return false;//内存不足,分配失败
(*L)->next = NULL;//头节点之后还没有节点
return true;
}
```