• 大小: 5KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-06-13
  • 语言: C/C++
  • 标签: 数据结构  

资源简介

使用链表实现多项式的加法和乘法,数据结构常见问题的C语言实现

资源截图

代码片段和文件信息

#include
#include
#include
typedef struct list
{
    int xishu;
    int power;
    struct list *next;
}list;

list* creat()
{
    list* head=(list*)malloc(sizeof(list));
    if(head==NULL)
    {
        printf(“malloc head node failed\n“);
        exit(-1);
    }
    head->xishu=0;
    head->power=0;
    head->next=NULL;
    int count;
    printf(“please input the count of the list:“);
    scanf(“%d“&count);
    printf(“please input %d xiangwith the power increase\n“count);
    while(count<=0)
    {
        printf(“the input is invalidplease input the count of the list again:“);
        scanf(“%d“&count);
    }
    while(count>0)
    {
        list *newnode=(list*)malloc(sizeof(list));
        if(newnode==NULL)
        {
            printf(“malloc newnode failed\n“);
            exit(-1);
        }
        printf(“please input xishu:“);
        scanf(“%d“&newnode->xishu);
        printf(“please input power:“);
        scanf(“%d“&newnode->power);
        newnode->next=head->next;
        head->next=newnode;
        --count;
    }
    return head;
}

void print(const list* lst)
{
    if(lst==NULL)
        printf(“the list is not exist\n“);
    if(lst->next!=NULL)
    {
        list *p=lst->next;
        while(p!=NULL)
        {
            if(p->power!=0&&p->xishu!=1&&p->power!=1)
                printf(“%dX^%d“p->xishup->power);
            else if(p->power!=0&&p->xishu!=1&&p->power==1)
                printf(“%dX“p->xishu);
            else if(p->power!=0&&p->xishu==1&&p->power!=1)
                printf(“X^%d“p->power);
            else if(p->power==1&&p->xishu==1)
                printf(“X“);
            else
                printf(“%d“p->xishu);
            p=p->next;
            if(p!=NULL)
                printf(“+“);
        }
        printf(“\n“);
    }

}

void list_free(list *a)
{
    if(a==NULL)
        return ;
    list *pa=a->next;
    while(pa!=NULL)
    {
        list *tmp=pa;
        pa=pa->next;
        free(tmp);
        tmp=NULL;
    }
    free(a);
    a=NULL;
}

list *list_copy(const list *src)
{
    if(src==NULL)
    {
        printf(“the list is not exist\n“);
        exit(-1);
    }
    const list *p=src->next;
    list* head=(list*)malloc(sizeof(list));
    if(head==NULL)
    {
        printf(“malloc head node failed\n“);
        exit(-1);
    }
    head->xishu=0;
    head->power=0;
    head->next=NULL;
    list* place=head;
    while(p!=NULL)
    {
        list *newnode=(list*)malloc(sizeof(list));
        if(newnode==NULL)
        {
            printf(“malloc newnode failed\n“);
            exit(-1);
        }
        newnode->xishu=p->xishu;
        newnode->power=p->power;

        

评论

共有 条评论