This code works for at least some inputs:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int cof;
int exp;
struct node *link;
};
struct node *create(struct node *q);
struct node *insert(struct node *ptr, struct node *p);
void display(char const *tag, struct node *ptr);
void err_exit(char const *tag);
struct node *create(struct node *q)
{
int i, n;
printf("enter the number of nodes: ");
if (scanf("%d", &n) != 1)
err_exit("Read error (number of nodes)");
for (i = 0; i < n; i++)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == 0)
err_exit("Out of memory (1)");
printf("enter the coefficient and exponent respectively: ");
if (scanf("%d%d", &ptr->cof, &ptr->exp) != 2)
err_exit("Read error (coefficient and exponent)");
ptr->link = NULL;
q = insert(ptr, q);
display("after input", q);
}
return q;
}
struct node *insert(struct node *ptr, struct node *p)
{
struct node *temp, *b;
if (p == NULL)
p = ptr;
else
{
display("insert: p = ", p);
display("insert: ptr = ", ptr);
if (p->exp < ptr->exp)
{
ptr->link = p;
p = ptr;
}
else
{
temp = p;
while ((temp->link != NULL) && (temp->link->exp < ptr->exp))
display("insert: tmp = ", temp),
temp = temp->link;
display("insert: post loop", temp);
b = temp->link;
temp->link = ptr;
ptr->link = b;
}
}
return p;
}
void display(char const *tag, struct node *ptr)
{
struct node *temp;
const char *pad = "";
temp = ptr;
printf("%s: ", tag);
while (temp != NULL)
{
printf("%s%d x ^ %d", pad, temp->cof, temp->exp);
temp = temp->link;
pad = " + ";
}
putchar('\n');
}
int main(void)
{
printf("enter the first polynomial:\n");
struct node *p1 = NULL, *p2 = NULL;
p1 = create(p1);
printf("enter the second polynomial:\n");
p2 = create(p2);
display("p1", p1);
display("p2", p2);
return 0;
}
void err_exit(char const *tag)
{
fprintf(stderr, "%s\n", tag);
exit(1);
}
Fixes include:
- Testing for I/O errors
- Add tag to display function
- Use display function copiously
- Add error exit function and use it
- Primary fix: handle test in
while
loop in insert()
correctly:
- Test
temp->link
for nullness
- Use
&&
and not ||
in testing for validity
- Improve printing in
display()
(only output +
when it separates two terms; output newline at end)
- Don't leak memory in
main()
.
- Don't pass uninitialized memory to
create()
.
- Don't ignore return from
create()
.
Example run:
enter the first polynomial:
enter the number of nodes: 3
enter the coefficient and exponent respectively: 2 2
after input: 2 x ^ 2
enter the coefficient and exponent respectively: 3 1
insert: p = : 2 x ^ 2
insert: ptr = : 3 x ^ 1
insert: post loop: 2 x ^ 2
after input: 2 x ^ 2 + 3 x ^ 1
enter the coefficient and exponent respectively: 4 0
insert: p = : 2 x ^ 2 + 3 x ^ 1
insert: ptr = : 4 x ^ 0
insert: post loop: 2 x ^ 2 + 3 x ^ 1
after input: 2 x ^ 2 + 4 x ^ 0 + 3 x ^ 1
enter the second polynomial:
enter the number of nodes: 5
enter the coefficient and exponent respectively: 1 0
after input: 1 x ^ 0
enter the coefficient and exponent respectively: 2 1
insert: p = : 1 x ^ 0
insert: ptr = : 2 x ^ 1
after input: 2 x ^ 1 + 1 x ^ 0
enter the coefficient and exponent respectively: 4 6
insert: p = : 2 x ^ 1 + 1 x ^ 0
insert: ptr = : 4 x ^ 6
after input: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0
enter the coefficient and exponent respectively: 3 2
insert: p = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0
insert: ptr = : 3 x ^ 2
insert: tmp = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0
insert: tmp = : 2 x ^ 1 + 1 x ^ 0
insert: post loop: 1 x ^ 0
after input: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
enter the coefficient and exponent respectively: 9 3
insert: p = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
insert: ptr = : 9 x ^ 3
insert: tmp = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
insert: tmp = : 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
insert: tmp = : 1 x ^ 0 + 3 x ^ 2
insert: post loop: 3 x ^ 2
after input: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2 + 9 x ^ 3
p1: 2 x ^ 2 + 4 x ^ 0 + 3 x ^ 1
p2: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2 + 9 x ^ 3
There is room to think the ordering by exponent is not working correctly, but the code doesn't crash. Running with valgrind
spots no memory access errors; it leaks like the proverbial sieve, though.