首页 > 其他 > 详细

一元多项式(具有非负次幂)的链表实现

时间:2014-03-09 10:53:49      阅读:523      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
/* list_poly.h */

#ifndef _LIST_POLY_H
#define _LIST_POLY_H

struct node;

typedef struct node *ptr_to_node;
typedef struct node *position;
typedef struct node *list;

list create_list();
void insert(int coef, int exp, list l, position p);
void insert_to_head(int coef, int exp, list l);
void insert_to_tail(int coef, int exp, list l);
position find_previous(int exp, list l);
int is_last(position p, list l);
void delete(int exp, list l);
void printl(list l);
void add_poly(const list poly1, const list poly2, list polysum);

#endif
bubuko.com,布布扣
bubuko.com,布布扣
/* list_poly.c */

#include "list_poly.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

struct node {
    int coef;
    int exp;
    ptr_to_node next;
};

list 
create_list()
{
    list l;
    l = malloc(sizeof(struct node));
    if(l == NULL)
    {
        perror("malloc error");
        exit(1);
    }
    l->next = NULL;
    
    return(l);
}

void 
insert(int coef, int exp, list l, position p)
{
    position newnode;
    newnode = malloc(sizeof(struct node));

    if(newnode == NULL)
    {
        perror("malloc error");
        exit(1);
    }
    newnode->coef = coef;
    newnode->exp = exp;
    newnode->next = p->next;
    p->next = newnode;
}
void
insert_to_head(int coef, int exp, list l)
{
    insert(coef, exp, l, l);
}
void
insert_to_tail(int coef, int exp, list l)
{
    ptr_to_node nptr;

    nptr = l;
    while(nptr->next != NULL)
        nptr = nptr->next;
    insert(coef, exp, l, nptr);
}
position
find_previous(int exp, list l)
{
    position p;
    
    p = l;
    while(p->next != NULL && p->next->exp != exp)
        p = p->next;

    return(p);
}
int
is_last(position p, list l)
{
    return(p->next == NULL);
}
void
delete(int exp, list l)
{
    position p, tmpp;

    p = find_previous(exp, l);

    if( !is_last(p, l))
    {
        tmpp = p->next; 
        p->next = tmpp->next;
        free(tmpp);
    }
}
void printl(list l)
{
    position p;
    
    p = l->next;    /* because we have a list_head */
    while(p != NULL)
    {
        if(!is_last(p, l))
        {
            if(p->coef != 1)
                printf("%dx^%d + ", p->coef, p->exp);
            else
                printf(" x^%d + ", p->exp);
        }
        else
        {
            if(p->coef != 1)
                printf("%dx^%d\n", p->coef, p->exp);
            else
                printf(" x^%d\n", p->exp);
        }
        p = p->next;    
    }
}

void 
add_poly(const list poly1, const list poly2, list polysum)
{
    ptr_to_node p1, p2;
    p1 = poly1->next;    /* because we have a list_head */
    p2 = poly2->next;
    
    while(p1 != NULL || p2 != NULL)
    {
        if(p1 != NULL && p2 != NULL)
        {
            if(p1->exp > p2->exp)
            {        
                insert_to_tail(p1->coef, p1->exp, polysum);
                p1 = p1->next;
            }
            else if(p1->exp < p2->exp)
            {
                insert_to_tail(p2->coef, p2->exp, polysum);
                p2 = p2->next;
            }
            else
            {
                insert_to_tail(p1->coef + p2->coef, p1->exp, polysum);
                p1 = p1->next;
                p2 = p2->next;
            }
        }
        else if(p1 != NULL && p2 == NULL)
        {
            for(; p1 != NULL; p1 = p1->next)
            {
                insert_to_tail(p1->coef, p1->exp, polysum);
            }
        }
        else if(p1 == NULL && p2 != NULL)
        {
            for(; p2 != NULL; p2 = p2->next)
            {
                insert_to_tail(p2->coef, p2->exp, polysum);
            }
        }
        
    }
    
    
}
bubuko.com,布布扣
bubuko.com,布布扣
/* list_poly_test.c */

#include "list_poly.h"
#include <stdio.h>

int
main(void)
{    
    list poly1, poly2, polysum;
    poly1 = create_list();
    poly2 = create_list();
    polysum = create_list();

    insert_to_tail(5, 3, poly1);
    insert_to_tail(1, 2, poly1);
    insert_to_tail(2, 1, poly1);
    insert_to_tail(3, 0, poly1);

    insert_to_tail(7, 4, poly2);
    insert_to_tail(4, 2, poly2);
    insert_to_tail(5, 1, poly2);
    insert_to_tail(6, 0, poly2);
    
    printf("poly1         = ");
    printl(poly1);
    printf("poly2         = ");
    printl(poly2);
    
    add_poly(poly1, poly2, polysum);
    printf("poly1 + poly2 = ");
    printl(polysum);
}
bubuko.com,布布扣

 

编译运行结果如下:

bubuko.com,布布扣

一元多项式(具有非负次幂)的链表实现,布布扣,bubuko.com

一元多项式(具有非负次幂)的链表实现

原文:http://www.cnblogs.com/nufangrensheng/p/3587521.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!