One-way linked list for polynomial addition and multiplication--writing data structure by itself

Source: Internet
Author: User

Using a single necklace table to implement the polynomial data structure and code as follows (due to the time-factor polynomial multiplication function is not implemented, the reader can be perfected in itself):

The header file that holds the structure polylist.h

#ifndef _h_polylist_#define _h_polylist_typedef struct _poly_node{    int ratio;  coefficient     int power;  Power        struct _poly_node* next;       } node,*pnode;typedef struct _poly_list{    struct _poly_node* next;   } Polylist,*ppolylist;ppolylist init_list_header (void);p node Poly_add_node (ppolylist plist,int ratio,int power); int Poly_delete_node (ppolylist plist,int power); int Print_poly (ppolylist plist); int Poly_bubble_sort (ppolylist plist); Ppolylist Poly_addcat (ppolylist pfir,ppolylist psec);pP olylist poly_add (ppolylist pfir,ppolylist psec); #endif

The


Data structure function implements the file polylist.c as follows:

/************************************** Time: 2014.12.11xiao_ping_ping content: A one-way list represents the addition and multiplication of a polynomial: learning to write data structures **************** /#include <string.h> #include <stdlib.h> #include "polylist.h"/* Create a polynomial-linked header */        Ppolylist Init_list_header (void) {ppolylist phead;    Phead = (Polylist *) malloc (sizeof (polylist));        Phead->next = NULL;     return phead;        }/* Add items to the polynomial */pnode Poly_add_node (ppolylist plist,int ratio,int power) {Pnode ptail,pre,p;    Ptail = (node *) malloc (sizeof (node));    Ptail->ratio = ratio;    Ptail->power = power;        Ptail->next = NULL;               if (NULL = = plist->next) {plist->next = Ptail;                 printf ("Polynomial list insertion point: coefficient =%d, Power value =%d\n", ptail->ratio,ptail->power);     return ptail;    } pre = plist->next;        while (pre) {p = pre;    Pre = pre->next;    } p->next = Ptail;         printf ("Polynomial list insertion point: coefficient =%d, Power value =%d\n", ptail->ratio,ptail->power); return ptail; }/* Delete the item */int poly_delete_node (ppolylist plist,int power) {Pnode pre,p, corresponding to the power value in the polynomial;         if (null = = Plist->next) {printf ("The polynomial list is empty");           return-1;    } pre = plist->next;    p = Pre;        while (NULL! = P->next) {pre = P;             if (Power = = Pre->next->power) {pre = pre->next;                          P->next = pre->next;              Free (pre);                          printf ("Delete the item with a power of%d in the polynomial list \ n", power);     } p = p->next;                } if (power = = plist->next->power) {p = plist->next;                Plist->next = plist->next->next;        Free (p);                     printf ("Delete the item with a power of%d in the polynomial list \ n", power); } return 0;        }/* Printing polynomial */int print_poly (ppolylist plist) {Pnode p;        p = plist->next; if (null = = P) {printf ("The polynomial is empty.          \ n ");                  return-1;      } printf ("Traversal output polynomial: \ n");    printf ("Y =");   while (NULL! = p) {printf ("%d x%d +", p->ratio,p->power);              p = p->next; } return 0;}    /* Polynomial sort */int poly_bubble_sort (ppolylist plist) {int tpower,tratio;        Pnode p,pre,ptemp;         if (null = = Plist->next) {printf ("The polynomial list is empty \ n");       return-1;         } if (NULL = = Plist->next->next) {printf (the "polynomial list has only one item \ n");              Return-2;    } p = plist->next;    Pre = P;        while (p) {pre = P;            while (Pre->next) {//ptemp = pre;                                                if (Pre->next->power = = p->power) {ptemp = pre->next;                P->ratio + = pre->next->ratio;                                Pre->next = Pre->next->next;                Free (ptemp);                if (NULL = = Pre->next) {break; }} else if (pre-&Gt;next->power < P->power) {tpower = p->power;                Tratio = p->ratio;                P->power = pre->next->power;                P->ratio = pre->next->ratio;                Pre->next->power = Tpower;                Pre->next->ratio = Tratio;                Ptemp = p;                                p = pre->next;                Pre->next = ptemp; } else {} pre = Pre->next                  ;         } p = p->next;     }}/* merges two polynomial and adds all nodes of the second polynomial to the first polynomial */ppolylist poly_addcat (ppolylist pfir,ppolylist psec) {polylist *p = Pfir;        Ptotal = (Polylist *) malloc (sizeof (polylist));        Node *pre = pfir->next;     p = pfir->next;             if (NULL = = Pre) {Pfir->next = psec->next;   } else {while (pre->next)     {//pre = p;                Pre = pre->next;    } Pre->next = psec->next;           } free (PSEC); return pfir;    }/* polynomial addition */ppolylist poly_add (ppolylist pfir,ppolylist psec) {polylist *p = Pfir;        Ptotal = (Polylist *) malloc (sizeof (polylist));    p = poly_addcat (pfir,psec);    Poly_bubble_sort (P);       Return Poly_bubble_sort (Poly_addcat (pfir,psec));            return p;  }

The


Test file test.c is as follows

#include <string.h> #include <conio.h> #include "polylist.h" int main () {ppolylist polyone,polytwo,polyadd;    printf ("Polynomial one: \ n");      PolyOne = Init_list_header ();      Poly_add_node (polyone,2,11);      Poly_add_node (polyone,3,7);    Poly_add_node (polyone,23,11);    Poly_add_node (polyone,1,13);    Poly_add_node (polyone,32,45);    Poly_add_node (polyone,23,32);    Poly_add_node (polyone,3,343);    Poly_add_node (polyone,34,414);    Poly_add_node (polyone,23,7);    Poly_add_node (polyone,12,44);      Print_poly (PolyOne);    printf ("\ n");    printf ("\ n");    printf ("Polynomial two: \ n");      Polytwo = Init_list_header ();      Poly_add_node (polytwo,2,11);      Poly_add_node (polytwo,44,13);    Poly_add_node (polytwo,23,34);    Poly_add_node (polytwo,43,32);    Poly_add_node (polytwo,32,45);    Poly_add_node (polytwo,23,32);    Poly_add_node (polytwo,3,343);    Poly_add_node (polytwo,34,44);    Poly_add_node (polytwo,23,7);    Poly_add_node (polytwo,12,464);     Print_poly (Polytwo); printf ("\ n");       printf ("\ n");       Polyadd = Init_list_header ();    Polyadd = Poly_addcat (polyone,polytwo);      Polyadd = Init_list_header ();    printf ("Merge polynomial one and two: \ n");        Polyadd = Poly_add (polyone,polytwo);       Print_poly (Polyadd);        Poly_bubble_sort (PolyOne);    Poly_delete_node (polyone,2);    Poly_delete_node (polyone,4);    Poly_delete_node (polyone,2);   Poly_delete_node (polyone,3); Getch ();}


The results of the above file operation are as follows:

One-way linked list for polynomial addition and multiplication--writing data structure by itself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.