-2

I made some polynomial code with a doubly-linked list. for example, if you write 1 and 2 then 1 is a degree and 2 is coefficient. 1x^2 insert to doubly linked list. the problem is that when I check my code, the Node head->degree is changing. if I write 1x^2 then head->degree is 1 next, I write 2x^1 then head-> degree should maintain 1 but head-> degree change to 2 I think there is some problem in the head pointer.

#include <stdio.h>
#include <stdlib.h>

// struct 
struct Node {
    int degree;
    int coefficient;
    struct Node* next;
    struct Node* prev;
};


// global variables
int de; // degree
int co; // coefficient
int flag;
Node** head = (Node**)malloc(sizeof(Node)); // 
Node** head1 = (Node**)malloc(sizeof(Node)); // 
Node** head2 = (Node**)malloc(sizeof(Node)); // 
Node** head3 = (Node**)malloc(sizeof(Node)); // 
Node* newNode = (Node*)malloc(sizeof(Node)); //


// function
Node* inputpoly(void);
void printNode(Node* inp);
Node* multiply(Node* a, Node* b);

// main
int main() {
    // head null
    (*head1) = NULL;
    (*head2) = NULL;
    (*head3) = NULL;


    while (1) {
        printf("Input (degree) (coefficient) : ");
        scanf_s("%d %d", &de, &co);
            
        if (de * co < 0) { continue; }
        if (de < 0 && co < 0) {
            printf("Done!\n");
            break;
        }
        *head = inputpoly();
    }
    printNode(*head);
        
    //multiply(*head1, *head2);

    free(head1);
    free(head2);
    free(head3);
    free(newNode);
    free(head);
}


Node* inputpoly(void) {
    // create Node
    newNode->degree = de;
    newNode->coefficient = co;
    newNode->next = NULL;
    newNode->prev = NULL;
    
    // case1 
    if (flag == 0) {
        // list 
        if ((*head1) == NULL) {
            *head1 = newNode;
        }
        // list x
        else {
            Node* horse = (*head1);
            // front of head
            //  ------------------There is some problem
            printf("%d\n", 1);
            printf("--%d\n", newNode->degree);
            printf("--%d\n", horse->degree);
            if (horse->degree > newNode->degree) {
                newNode->next = horse;
                horse->prev = newNode;
                *head1 = newNode;
            }
            // barward of head
            else {
                int num = 0;
                while (horse->next != NULL) {
                    
                    horse = horse->next;
                    if (horse->degree > newNode->degree) {
                        horse->prev->next = newNode;
                        newNode->next = horse;
                        newNode->prev = horse->prev;
                        horse->prev = newNode;
                        num = 1;
                        break;
                    }
                }
                // behind tail
                if (num == 0) {
                    horse->next = newNode;
                    newNode->prev = horse;
                }
            }
        }   
        return *head1;
    }

}

void printNode(Node* inp) {
    Node* horse = inp;
    if (horse == NULL) 
    { 
        return;
    }

    while (horse != NULL) {
        if (horse->prev == NULL) {
            if (horse->degree == 1) {
                printf("%d", horse->coefficient);
            }
            else {
                printf("%d x^%d", horse->coefficient, horse->degree);
            }
        }
        else {
            if (horse->degree == 1) {
                printf(" + %d", horse->coefficient);
            }
            else {
                printf(" + %d x^%d", horse->coefficient, horse->degree);
            }
        }
    }
    printf("\n");
}

YongHoon
  • 71
  • 3
  • I wouldnt advise to "fix" that code. Rather to read some documentations on linked list implementations (maybe some examples?) Because the way you are using them right here won't lead you far I'm afraid – Angevil Mar 04 '21 at 13:51
  • I should use doubly linked list and i think there is some head pointer problem, and if I fixed it I can this problem. so I want to maintain this code as possible. I want some advice or solution to my head pointer:) – YongHoon Mar 04 '21 at 14:03
  • 1
    This: `Node** head = (Node**)malloc(sizeof(Node));` will not compile as you are attempting make this call outside of any function. Same for all of the other similar statements. And [casting the return of malloc() in C is not recommended](https://stackoverflow.com/a/605858/645128). – ryyker Mar 04 '21 at 14:05
  • + the malloc(sizeof(Node)) is not correct to allocate a Node**, it should be sizeof(Node*) – Angevil Mar 04 '21 at 14:08
  • 1
    @YongHoon - _1 is degree and 2 is coefficient. 1x^2_ - no. In _1x^2_, _1_ is the coefficient and _2_ is the degree. – Armali Mar 04 '21 at 14:29
  • This doesn't address the problems you're having, but I *strongly* suggest using a simple array to represent polynomials - the array position corresponds to the degree, so a polynomial like `3.0x^2 - 1.0` would be stored as `{ -1.0, 0.0, 3.0 }`. You may still have to muck with dynamic memory to allocate and extend arrays as necessary, but you don't have to mess with pointers or insertion logic. – John Bode Mar 04 '21 at 15:43
  • Welcome to Stack Overflow! Please create a [mre] so that people don't have to guess at the missing parts of your code. – trent Mar 04 '21 at 16:55

1 Answers1

1

"i think there is some head pointer problem, and if I fixed it I can this problem. so I want to maintain this code as possible. I want some advice or solution to my head pointer"

The code posted in your example does not compile:

enter image description here

Before you can fix a head pointer problem the code must compile. This list of errors is detailing 2 things:

first, functions cannot be called outside of a function, eg:

Node** head = (Node**)malloc(sizeof(Node)); // 
Node** head1 = (Node**)malloc(sizeof(Node)); // 
Node** head2 = (Node**)malloc(sizeof(Node)); // 
Node** head3 = (Node**)malloc(sizeof(Node)); // 
Node* newNode = (Node*)malloc(sizeof(Node)); //

should be called from within main(void){...} or some other function.

second, every occurrence of Node should be prepended with struct. eg:

struct Node** head = malloc(sizeof(struct Node *));

(have also removed the cast, and modified the size of what you are creating memory for, i.e. a pointer)

Rather then fix these and other problems, here is an example of a doubly linked list that can demonstrate the structure of a simple working program. You can adapt the following to match your needs:

 struct Node {
    int deg;
    int coef;
    struct Node* next; // Pointer to next node in DLL
    struct Node* prev; // Pointer to previous node in DLL
};

void inputpoly(struct Node** head_ref, int deg, int coef)
{
    //allocate node
    struct Node *new_node = malloc(sizeof(*new_node));
 
    //assign data
    new_node->deg = deg;
    new_node->coef = coef;  
 
    //set next as new head and prev to null
    new_node->next = (*head_ref);
    new_node->prev = NULL;
 
    //change prev of head  to new */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    //point head to the new node */
    (*head_ref) = new_node;
}

void printList(struct Node* node)
{
    struct Node* last;
    printf("\nread forward\n");
    while (node != NULL) {
        printf(" %d,%d ", node->deg,node->coef);
        last = node;
        node = node->next;
    }
 
    printf("\nread reverse\n");
    while (last != NULL) {
        printf(" %d,%d ", last->deg,last->coef);
        last = last->prev;
    }   
}

int main(void)
{
    //start with empty list
    struct Node* head = NULL;
    //create and populate new nodes
    inputpoly(&head, 7, 2);
    inputpoly(&head, 1, 4);
    inputpoly(&head, 4, 6);
    //ouput list

    printList(head);
    getchar();
    return 0;
}

Note that this code is offered as a basic demonstration of creating doubly linked list, and illustrate how to traverse both directions. Because it does not free allocated memory, it is not recommended that it be used for any production purpose without addressing that omission.

ryyker
  • 22,849
  • 3
  • 43
  • 87