使用了未初始化的s 请修改#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 = 0; i < index; 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 = NULL; struct LinkList listb = NULL; lista = init(lista); listb = init(listb); int n, b; float a; printf("请输入第一个多项式的项数->"); scanf("%d", &n); for (int i = 1; i <= n; i++) //lista { printf("输入第%d个系数、指数", i); scanf("%f %d", &a, &b); compare(lista, a, b); } printf("请输入第二个多项式的项数->"); scanf("%d", &n); for (int i = 1; i <= n; i++) //listb { printf("输入第%d个系数、指数", i); scanf("%f %d", &a, &b); compare(listb, a, b); } return 0; }
时间: 2024-01-24 10:02:46 浏览: 31
好的,我来修改一下代码,解决这个问题:
```c
#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 = 0; i < list->len; i++)
{
if (b > p->b)
{
p = p->next;
}
else if (b == p->b)
{
p->a += a;
break;
}
else
{
listadd(list, i, a, b); //插入
break;
}
}
if (i >= list->len)
{
listadd(list, i, a, b); //添加到最后一个
}
}
void listadd(struct LinkList* list, int index, float a, int b) //添加新的指数项
{
num* p = list->head, * s;
int i;
for (i = 0; i < index; 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 = NULL;
struct LinkList* listb = NULL;
lista = init(lista);
listb = init(listb);
int n, b;
float a;
printf("请输入第一个多项式的项数->");
scanf("%d", &n);
for (int i = 1; i <= n; i++) //lista
{
printf("输入第%d个系数、指数", i);
scanf("%f %d", &a, &b);
compare(lista, a, b);
}
printf("请输入第二个多项式的项数->");
scanf("%d", &n);
for (int i = 1; i <= n; i++) //listb
{
printf("输入第%d个系数、指数", i);
scanf("%f %d", &a, &b);
compare(listb, a, b);
}
return 0;
}
```
主要的修改如下:
1. `init` 函数的返回值改为了 `struct LinkList*` 类型。
2. `compare` 函数中的 `p` 改为了 `num*` 类型。
3. `listadd` 函数中,动态分配内存的语句修改为 `s = (num*)malloc(sizeof(struct num));`,同时对 `s` 进行了初始化。
4. `main` 函数中,定义了 `lista` 和 `listb` 为 `struct LinkList*` 类型,并且在调用 `init` 函数时传入了这两个变量的地址。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int n, p[100], a; void sort(int p[]); void insert(int p[], int a); int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &p[i]); } sort(p); insert(p, a); } void sort(int p[]) { for (int i = 0; i < strlen(p)-1; i++) { for (int q = i+1; q < strlen(p); q++) { if (p[i] > p[q]) { int t = p[q]; p[q] = p[i]; p[i] = t; } } } for (int i = 0; i < strlen(p); i++) { printf("%d", p[i]); } } void insert(int p[], int a) { if (a >= p[strlen(p) - 1])p[strlen(p)] = a; else if (a <= p[0]) { for (int i = strlen(p) - 1; i >= 0; i--) { p[i + 1] = p[i]; } p[0] = a; } else { for (int i = 0; i < strlen(p) - 1; i++) { if (p[i]a) { for (int o = strlen(p) - 1; o >= i; o--) { p[o + 1] = p[o]; } p[i] = a; } } for (int i = 0; i < strlen(p); i++)printf("%d", p[i]); } }
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)