Home > Back-end >  C language data structure combined arithmetic polynomial
C language data structure combined arithmetic polynomial

Time:09-26

Hey hey, is there a big help to see this code error, why don't run after the input data out ah, thank you!
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; The malloc. H>

Struct polynode
{
Int exp.
Float coef.
Struct polynode * next;
};
Typedef struct polynode * polypointer;

Polypointer CreateList (int n) {
Polypointer head;
Polypointer p, pre;
int i;
The head=(polypointer) malloc (n * sizeof (struct polynode));
The head - & gt; next=NULL;
The pre=head;
for(i=1; i<=n; I++)
{
N * (p=(polypointer) malloc sizeof (struct polynode));
Printf (" please input index \ n ");
The scanf (" % d ", & amp; p-> Exp);
Printf (" please input coefficient \ n ");
The scanf (" % d ", & amp; p-> Coef);
The pre - & gt; Next=p;
The pre=p;
}
p-> next=NULL;
return head;
}
Void PrintList (polypointer h) {
Polypointer p;
P=h - & gt; next;
If (p!=NULL) {
Printf (" % f * X ^ % d ", p - & gt; Coef, p - & gt; Exp);
P=p - & gt; next;
printf("\n");
}
}

Int bubble_sort polypointer q, int o {
Polypointer pre, p, temp;
int i,j;
The pre=q - & gt; next;
P=pre - & gt; next;
for(i=1; i<=o - 1; I++)
For (j=2; j<=o; J++) {
If (pre> P) {
The pre - & gt; next=p-> next;
p-> Next=pre;
Temp=p;
P=the pre.
P=temp.
}
P=p - & gt; next;
The pre=p;
}
return 0;
}

Polypointer AddList (polypointer head1, polypointer head2) {
Polypointer p, q, c, head3;
int x;
P=head1 - & gt; next;
Q=head2 - & gt; next;
C=(polypointer) malloc (sizeof (struct polynode));
Head3=c;
C=head3 - & gt; next;
while(p!=NULL& & q!=NULL) {
If (p - & gt; Exp==q - & gt; Exp) {
X=p - & gt; Coef + q - & gt; Coef.
C - & gt; Coef=x;
C - & gt; Exp=p - & gt; Exp.
C=c - & gt; next;
P=p - & gt; next;
Q=q - & gt; next;
}
Else if ((p - & gt; Exp) & gt; (q - & gt; Exp)) {
C - & gt; Coef=q - & gt; Coef.
C - & gt; Exp=q - & gt; Exp.
Q=q - & gt; next;
C=c - & gt; next;
}
Else if ((p - & gt; Exp) & lt; (q - & gt; Exp)) {
C - & gt; Coef=p - & gt; Coef.
C - & gt; Exp=p - & gt; Exp.
P=p - & gt; next;
C=c - & gt; next;
}
}
C=head3;
Return the c;
}

Int main () {
Int m, n;
Polypointer a, b, c;
Printf (" please input A polynomial number of items: \ n ");
The scanf (" % d ", & amp; M);
A=CreateList (m);
Printf (" please enter a polynomial number of items: B \ n ");
The scanf (" % d ", & amp; N);
B=CreateList (n);
Bubble_sort (a, m);
Bubble_sort (b, n);
C=AddList (a, b);
PrintList (c);
return 0;
}

CodePudding user response:

/* AddList functions do not modify the original poster on the */
1. Create node memory allocation problem
2. Enter game coefficient of receiving format has a problem when
3. The problem of output function judgment conditions have
4. Nodes comparing conditions there is a problem, sort function there is something wrong with the exchange between
/* the following programs have make correction to the above questions remaining ADDList refer to program on the */


# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; Math. H>
# include & lt; The malloc. H>

Typedef struct polynode
{
Int exp.
Float coef.
Struct polynode * next;
} polynode, * polypointer;

Polypointer CreateList (polypointer Head, int n) {
Polypointer NewNode;
Polypointer pre.

The pre=Head;
for(int i=1; i<=n; I++)
{
NewNode=(polypointer) malloc (sizeof (polynode));
NewNode - & gt; next=NULL;//create a nodes enter a data

Printf (" please input & lt; The first % d & gt; Index \ n ", I);
The scanf (" % d ", & amp; NewNode - & gt; Exp);
Printf (" please input coefficient: for example, 2.5 \ n ");
The scanf (" % f ", & amp; NewNode - & gt; Coef);
The pre - & gt; Next=NewNode;
The pre=NewNode;
}
Return the Head;
}
Void PrintList (polypointer h) {
Polypointer p;
P=h - & gt; next;
while(p!=NULL)//if statement will only perform a
{
Printf (" % 2 f * X ^ % d ", p - & gt; Coef, p - & gt; Exp);
P=p - & gt; next;
printf("\n");
}
}

Void bubble_sort (polypointer head, int o {
Polypointer prepre, pre, p, temp.
Prepre//position, the pre, p said Head, 1, 2,
Bubble sort/* */
for(int i=1; i<=o - 1; I++)
{
Prepre=head;
The pre=head - & gt; next;//every comparison, pointer to the list the first and the second node location
P=pre - & gt; next;
For (int j=2; j<=o; J++)
{//node values calculated using the power function
If (pow (pre - & gt; Coef, pre - & gt; Exp) & gt; Pow (p - & gt; Coef, p - & gt; Exp))//comparison should be nodes internal storage node values rather than itself
{
/* if precursor nodes is greater than the subsequent exchange position */
Prepre - & gt; Next=p;
Temp=p - & gt; next;
p-> Next=pre;
The pre - & gt; Next=temp;
}
Prepre=pre;//pointer back
The pre=p;
P=p - & gt; next;
}
}

}

Polypointer AddList (polypointer head1, polypointer head2) {
Polypointer p, q, c, head3;
int x;
P=head1 - & gt; next;
Q=head2 - & gt; next;
C=(polypointer) malloc (sizeof (struct polynode));
Head3=c;
C=head3 - & gt; next;
while(p!=NULL& & q!=NULL)
{
If (p - & gt; Exp==q - & gt; Exp)
{
X=p - & gt; Coef + q - & gt; Coef.
C - & gt; Coef=x;
C - & gt; Exp=p - & gt; Exp.
C=c - & gt; next;
P=p - & gt; next;
Q=q - & gt; next;
}
Else if ((p - & gt; Exp) & gt; (q - & gt; Exp))
{
C - & gt; Coef=q - & gt; Coef.
C - & gt; Exp=q - & gt; Exp.
Q=q - & gt; next;
C=c - & gt; next;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related