Q2: Polynomials using a linked list
題目說明二
請設計C語言程式,以此串列的結構 求出以下兩多項式A(X)+B(X)的最終結果。 • A=3X3+4X + 2 B = 6X3+ 8X² + 6X + 9

My Code:

not the most efficient code i would say:
#include <stdio.h>
#include <stdlib.h>
// Define the EquationA structure
struct EquationA {
int X_power;
int alpha;
struct EquationA* next;
};
// Define the EquationB structure
struct EquationB {
int X_power;
int alpha;
struct EquationB* next;
};
struct EquationC {
int X_power;
int alpha;
struct EquationC* next;
};
// Initialize the start pointers for both linked lists
struct EquationA* startA = NULL;
struct EquationB* startB = NULL;
struct EquationC* startC = NULL;
void invertEquationA() {
struct EquationA* current = startA;
struct EquationA* previous = NULL;
struct EquationA* next = NULL;
while (current != NULL) {
next = current->next; // Store the next node
current->next = previous; // Change the next of the current node
// Move pointers one position ahead
previous = current;
current = next;
}
startA = previous; // Update the start to the last node
}
void invertEquationB() {
struct EquationB* current = startB;
struct EquationB* previous = NULL;
struct EquationB* next = NULL;
while (current != NULL) {
next = current->next; // Store the next node
current->next = previous; // Change the next of the current node
// Move pointers one position ahead
previous = current;
current = next;
}
startB = previous; // Update the start to the last node
}
void invertEquationC() {
struct EquationC* current = startC;
struct EquationC* previous = NULL;
struct EquationC* next = NULL;
while (current != NULL) {
next = current->next; // Store the next node
current->next = previous; // Change the next of the current node
// Move pointers one position ahead
previous = current;
current = next;
}
startC = previous; // Update the start to the last node
}
// Function to add an equation to the first linked list (EquationA)
void addEquationA() {
struct EquationA* eqnA = (struct EquationA*)malloc(sizeof(struct EquationA));
if (eqnA == NULL) {
printf("Memory allocation failed.\\n");
return;
}
printf("Enter X_power for EquationA: ");
scanf("%d", &eqnA->X_power);
printf("Enter Alpha for EquationA: ");
scanf("%d", &eqnA->alpha);
eqnA->next = startA;
startA = eqnA;
printf("EquationA added successfully.\\n");
}
// Function to add an equation to the second linked list (EquationB)
void addEquationB() {
struct EquationB* eqnB = (struct EquationB*)malloc(sizeof(struct EquationB));
if (eqnB == NULL) {
printf("Memory allocation failed.\\n");
return;
}
printf("Enter X_power for EquationB: ");
scanf("%d", &eqnB->X_power);
printf("Enter Alpha for EquationB: ");
scanf("%d", &eqnB->alpha);
eqnB->next = startB;
startB = eqnB;
printf("EquationB added successfully.\\n");
}
// Function to list the equations in the first linked list (EquationA)
void listEquationA() {
struct EquationA* current = startA;
if (current == NULL) {
printf("No data found in EquationA.\\n");
return;
}
while (current != NULL) {
if(current == startA){
printf("A=%dX^%d ", current->alpha, current->X_power);
}
if(current != startA and current->X_power != 0){
printf(" + %dX^%d ", current->alpha, current->X_power);
}
if(current->X_power == 0){
printf(" + %d \\n", current->alpha);
}
current = current->next;
}
}
// Function to list the equations in the second linked list (EquationB)
void listEquationB() {
struct EquationB* current = startB;
if (current == NULL) {
printf("No data found in EquationB.\\n");
return;
}
while (current != NULL) {
if(current == startB){
printf("B=%dX^%d ", current->alpha, current->X_power);
}
if(current != startB and current->X_power != 0){
printf(" + %dX^%d ", current->alpha, current->X_power);
}
if(current->X_power == 0){
printf(" + %d \\n", current->alpha);
}
current = current->next;
}
}
void listEquationC() {
struct EquationC* current = startC;
if (current == NULL) {
printf("No data found in EquationC.\\n");
return;
}
while (current != NULL) {
if(current == startC){
printf("C=%dX^%d ", current->alpha, current->X_power);
}
if(current != startC and current->X_power != 0){
printf(" + %dX^%d ", current->alpha, current->X_power);
}
if(current->X_power == 0){
printf(" + %d \\n", current->alpha);
}
current = current->next;
}
}
void addEquationC() {
struct EquationA* currentA = startA;
struct EquationB* currentB = startB;
while (currentA != NULL || currentB != NULL) {
struct EquationC* currentC = (struct EquationC*)malloc(sizeof(struct EquationC));
if (currentC == NULL) {
printf("Memory allocation failed for EquationC.\\n");
return;
}
currentC->alpha = 0;
currentC->X_power = 0;
if (currentA != NULL && currentB != NULL) {
if (currentA->X_power == currentB->X_power) {
currentC->X_power = currentA->X_power;
currentC->alpha = currentA->alpha + currentB->alpha;
currentA = currentA->next;
currentB = currentB->next;
} else if (currentA->X_power > currentB->X_power) {
currentC->X_power = currentA->X_power;
currentC->alpha = currentA->alpha;
currentA = currentA->next;
} else {
currentC->X_power = currentB->X_power;
currentC->alpha = currentB->alpha;
currentB = currentB->next;
}
} else if (currentA != NULL) {
currentC->X_power = currentA->X_power;
currentC->alpha = currentA->alpha;
currentA = currentA->next;
} else if (currentB != NULL) {
currentC->X_power = currentB->X_power;
currentC->alpha = currentB->alpha;
currentB = currentB->next;
}
if (currentC->alpha != 0) {
currentC->next = startC;
startC = currentC;
} else {
free(currentC);
}
}
}
int main() {
for (int i = 1; i <= 3; i++) {
addEquationA();
}
for (int i = 1; i <= 4; i++) {
addEquationB();
}
invertEquationA();
invertEquationB();
listEquationA();
listEquationB();
addEquationC();
invertEquationC();
listEquationC();
return 0;
}