C++链表实现多项式相加

#ifndef POLYNOMOAL
#define POLYNOMOAL
#include <stdio.h>
#include <stdlib.h>

typedef struct NODE
{
    int num;
    int power;
    struct NODE* next;
}Node;
typedef struct LIST
{
    Node* head;
    int len;

}List;

typedef void(PRINT)(void*);

List* InitList();

void AddList(List* list1, List* list2);

void InsertList(List* list, int pos, int num, int power);

void PrintList(List* list);

void Add(List* list, Node* newnode, int pos);

void SetNum(Node* node, int num);

void FreeList(List* list);

#endif

#include "polynomial.h"

List* InitList()
{
    List* list = (List*)malloc(sizeof(List));
    list->len = 0;
    list->head = (Node*)malloc(sizeof(Node));
    list->head->next = NULL;
    return list;
}

void AddList(List* list1, List* list2)
{
    int count = 0;
    Node* tail =NULL;
    Node* pCurrent1 = list1->head->next;
    Node* pCurrent2 = list2->head->next;
    while ( pCurrent1 && pCurrent2 )
    {
        //printf("11111");
        if ( pCurrent1->power < pCurrent2->power )
        {
            Add(list1, pCurrent2, count+1);
        }
        else if (pCurrent1->power == pCurrent2->power)
        {
            SetNum(pCurrent1, pCurrent1->num + pCurrent2->num);
        }
        else
        {
            Add(list1, pCurrent2, count);
        }
        if ( tail == NULL &&  pCurrent1->next->next == NULL)
        {
            tail = pCurrent1->next;
        }
        pCurrent1 = pCurrent1->next;
        pCurrent2 = pCurrent2->next;
        count++;
    }
    if ( list1->len < list2->len )
    {
        tail->next = pCurrent2;
    }
    else 
    {
        return;
    }
    
}

void InsertList(List* list, int pos,int num, int power)
{
    if (list == NULL)
    {
        return;
    }
    if (pos < 0 || pos > list->len)
    {
        return;
    }
    Node* pCurrent = list->head;
    Node* newnode = (Node*)malloc(sizeof(Node));
    newnode->num = num;
    newnode->power = power;
    for (int i = 0; i < pos; i++)
    {
        pCurrent = pCurrent->next;
    }
    newnode->next = pCurrent->next;
    pCurrent->next = newnode;    
    list->len++;
}

void Add(List* list, Node* newnode, int pos)
{
    if (list == NULL)
    {
        return;
    }
    if (pos < 0 || pos > list->len)
    {
        return;
    }
    Node* pCurrent = list->head;
    for (int i = 0; i < pos; i++)
    {
        pCurrent = pCurrent->next;
    }
    newnode->next = pCurrent->next;
    pCurrent->next = pCurrent;
    list->len++;
}

void SetNum(Node* node, int num)
{
    node->num = num;
}

void PrintList(List* list)
{
    Node* pCurrent = list->head->next;
    //printf("11111");
    while ( pCurrent != NULL )
    {
        //printf("11111");
        printf("%d  %d\n", pCurrent->num, pCurrent->power);
        pCurrent = pCurrent->next;
    }
}

void FreeList(List* list)
{
    Node* pCurrent = list->head;
    Node* Del = pCurrent;
    while ( pCurrent != NULL )
    {
         Del = pCurrent;
        pCurrent = pCurrent->next;
        free(Del);
    }
}

#include "polynomial.h"
int main()
{
    List* list1 = InitList();
    List* list2 = InitList();
    InsertList(list1, 0, 1, 1);
    InsertList(list1, 1, 2, 2);
    InsertList(list1, 2, 3, 3);
    InsertList(list1, 3, 4, 4);
    InsertList(list1, 4, 5, 5);

    InsertList(list2, 0, 1, 1);
    InsertList(list2, 1, 2, 2);
    InsertList(list2, 2, 3, 3);
    InsertList(list2, 3, 4, 4);
    InsertList(list2, 4, 5, 5);

    AddList( list1, list2 );

    PrintList(list1);
    FreeList(list1);
    FreeList (list2);
    system("pause");
}

猜你喜欢

转载自blog.csdn.net/qq_40729286/article/details/81636335