A question about the polynomial---impelemented with list

This is a discussion on A question about the polynomial---impelemented with list within the C Programming forums, part of the General Programming Boards category; Hi all.I wrote a program to impelement the add operation of polynomial with list.For example,I wanna do the add operation ...

  1. #1
    Registered User rpbear's Avatar
    Join Date
    Nov 2009
    Posts
    18

    A question about the polynomial---impelemented with list

    Hi all.I wrote a program to impelement the add operation of polynomial with list.For example,I wanna do the add operation on
    x^2+3x^4+6x^5 AND x-5x^4+x^5-4x^6
    I creat two list,the input should be:
    1 2
    3 4
    6 5
    EOF
    1 1
    -5 4
    1 5
    -4 6
    EOF
    my question is at Line 137~140.I haven't initialize the local variable P,however,It has a value!Why?I think it's the key to fix my program.
    Code:
    #include<stdio.h>
    #include<malloc.h>
    
    typedef struct Polynomial{
        int coe;
        int order;
        struct Polynomial *next;
    }Poly;
    
    int creat_list(Poly** head);
    void print_list(Poly *head);
    void insert_node(Poly *head,Poly *py);
    void del_node(Poly *head,Poly *py);
    void add_poly(Poly *h1,Poly *h2);
    void print_result(Poly *head);
    
    int main(){
    
        Poly *head1,*head2;
        creat_list(&head1);
    //printf("---------------------\n");
    //    print_list(head1);
        creat_list(&head2);
    //printf("---------------------\n");
    //    print_list(head2);
        add_poly(head1,head2);
    printf("---------------------\n");
        print_result(head1);
        return 0;
    }
    
    int creat_list(Poly** head){
        int coe,order;
        *head = (Poly*)malloc(sizeof(Poly));
        if(*head == NULL) return 0;
    
        Poly *p = *head;
        (*head)->next = NULL;
        printf("input the coe and order:\n");
        while(scanf("%d %d",&coe,&order) == 2){
    //The algorithm will be right only if
    //you enter the unique order in order.
            Poly *newnode = (Poly *)malloc(sizeof(Poly));
            newnode->coe = coe;
            newnode->order = order;
            newnode->next = NULL;
            p->next = newnode;
            p = newnode;
            printf("input the coe and order:\n");
        }
        return 1;
    }
    
    void insert_node(Poly *head,Poly *py){
        int flag = 0;
        Poly *pre = head->next,*aft = head;
        while(pre){
            if(pre->order < py->order){
                aft = pre;
                pre = pre->next;
                continue;
            }
            else if(pre->order > py->order){
                aft->next = py;
                py->next = pre;
                flag = 1;
                break;
            }
            else{
                if(pre->coe+py->coe){
                    pre->coe += py->coe;
                    flag = 1;
                    break;
                }
                else{
                    Poly p = *py;
                    del_node(head,&p);
                    flag = 1;
                    break;
                }
            }
        }
        if(!flag){
            aft->next = py;
            py->next = NULL;
        }
    }
    
    void del_node(Poly *head,Poly *py){
    
        Poly *pre = head->next,*aft = head;
        while(pre){
            if(pre->coe == py->coe &&\
                pre->order == py->order){
                aft->next = pre->next;
                //break;
                //ignore break in case of more than one matched
            }
            aft = pre;
            pre = pre->next;
        }
    
    }
    
    void add_poly(Poly *h1,Poly *h2){
        Poly *pre = h1->next,*aft = h1;
        Poly *p2 = h2->next;
    
        while(p2 && pre){
            if(pre->order < p2->order){
                aft = pre;pre = pre->next;
                continue;
            }
            else if(pre->order > p2->order){
                Poly p = *p2;
    
                insert_node(h1,&p);
                p2 = p2->next;
                continue;
            }
            else{
                if((pre->coe+p2->coe) == 0){
                    aft = pre;pre = pre->next;
                    del_node(h1,aft);
                    p2 = p2->next;
                }
                else{
                    pre->coe += p2->coe;
                    aft = pre;pre = pre->next;
                    p2 = p2->next;
                }
            }
        }
    
        if(pre == NULL){
            while(p2){
    print_list(h1);
                Poly p;//p = *p2;
                printf("&&%d %d\n",p.coe,p.order);
    print_list(h1);
                insert_node(h1,&p);
                p2 = p2->next;
            }
        }
    }
    
    void print_result(Poly *head){
        Poly *p = head->next;
        while(p){
            printf("%+dx^%d",p->coe,p->order);
            p = p->next;
        }
        printf("\n");
    }
    
    void print_list(Poly *head){
        Poly *p = head->next;
        while(p != NULL){
            printf("%d %d\n",p->coe,p->order);
            p = p->next;
        }
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    > I haven't initialize the local variable P,however,It has a value!Why?

    Well it has to have some value. Since you haven't initialized it with a value, the initial value is going to be whatever value that is at the memory location that the variable is bound to.

  3. #3
    Registered User rpbear's Avatar
    Join Date
    Nov 2009
    Posts
    18
    Quote Originally Posted by Memloop View Post
    > I haven't initialize the local variable P,however,It has a value!Why?

    Well it has to have some value. Since you haven't initialized it with a value, the initial value is going to be whatever value that is at the memory location that the variable is bound to.
    I understand.However,if I change the p's value,the value of the list will change,I don't know why

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21