【源代码】C++算法(二)单链表的基本操作+合并连接多项式运算

日常说明:首先博主也是菜鸟一枚,有错误欢迎大家指正。另外本博客所有的代码博主编写后均调试
通过。重要提醒!!!!博主使用的是VS2017,如果有低版本的小伙伴
最好新建空项目将此代码复制上去。

附加说明:多项式运算部分代码中参考了http://www.cnblogs.com/TenosDoIt/p/3666585.html 的几个函数的写法,增加了输出操作符重载等更为方便的函数。更多算法请关注我的算法专栏。
运行结果截图:这里写图片描述
这里写图片描述
Linklist.h

#pragma once
/**********************************
* algorithms.h :单链表的基本操作 *
* author : shilei                 *
* created : 2017.3.15             *
***********************************/
#include<iostream>
#include"malloc.h"
#include"Node.h"
#include <exception>
#include <cstdlib>
using namespace std;

template<class T>
class SinglyList
{
public:

    SinglyList();
    SinglyList(T value[], int n);
    ~SinglyList();
    Node<T>*head;//头指针,指向单链表的头结点
    int NodeNum();//返回单链表的长度(结点数)
    Node<T>*ListInsert(int i, T x);//插入x作为第i个结点,,返回插入结点的地址
    T ListDelet(int i);//删除第i个结点,返回被删除的元素
    void ListDeletAll();
    T&GetData(int i);//获取第i个结点的data
    friend ostream& operator<<<>(ostream&, SinglyList<T>&Linklist);//操作符重载,输出单链表的所有的元素。
    virtual void operator +=(SinglyList<T>&Linklist);//将Linklist链接在当前链表之后
};
template<class T>
SinglyList<T>::SinglyList()//创建空链表
{
    this->head = new Node<T>();
}
template<class T>
SinglyList<T>::SinglyList(T value[], int n)//用数组初始化单链表
{
    this->head = new Node<T>();
    Node<T>*tail = this->head;
    for (int i = 0; i < n; i++)
    {
        tail->next = new Node<T>(value[i]);
        tail = tail->next;
    }
    tail->next = NULL;

}
template<class T>
SinglyList<T>::~SinglyList()
{
    this->ListDeletAll();
}
template<class T>
ostream&operator<<<>(ostream& out, SinglyList<T>&Linklist)//操作符重载,输出链表的全部元素
{
    out << "(";
    for (Node<T>*p = Linklist.head->next;p!= NULL; p=p->next)
    {
        out << p->data;
        if (p->next!=NULL)
        {
            out << ",";
        }
    }
    out << ")\n";
    return out;
}
template<class T>
void SinglyList<T>::operator +=(SinglyList<T>&Linklist)//连接单链表到当前链表后
{
    Node<T>*front = this->head;
    while (front->next!=NULL)
    {
        front = front->next;
    }
    front->next = Linklist.head->next;
    Linklist.head->next = NULL;
    free(Linklist.head);
}
template<class T>
T&SinglyList<T>::GetData(int i)
{
    Node<T>*p = this->head->next;
    for (int j = 0; p!=NULL&&j < i; j++)
    {
        p = p->next;
    }
    if (i>=0&&p!=NULL)
    {
        return p->data;
    }
    throw out_of_range("i的取值超出范围");
}
template<class T>
Node<T>*SinglyList<T>::ListInsert(int i, T x)//插入操作
{
    Node<T>*p = this->head;
    int j = 0;
    while (p&&j<i-1)
    {
        p = p->next;
        ++j;
    }
    if (!p||j>i-1)
    {
        throw out_of_range("指针为空或者i的取值不合法");
    }
    Node<T>*s = new Node<T>();
    s->data = x;
    s->next = p->next;
    p->next = s;
    return 0;
}
template<class T>
T SinglyList<T>::ListDelet(int i)//删除操作
{
    Node<T>*p = this->head;
    int j = 0; T e;
    while (p&&j<i-1)
    {
        p = p->next;
        ++j;
    }
    if (!(p->next)||j>i-1)
    {
        throw out_of_range("第i个结点不存在或者i的取值不合法");
    }
    Node<T>*q = p->next;
    e = q->data;
    p->next = q->next;//尾删除
    free(q);
    return e;
}
template<class T>
int SinglyList<T>::NodeNum()
{
    Node<T>*p = this->head;
    int i = 0;
    if (p->next==NULL)
    {
        return i;
    }
    while (p->next!=NULL)
    {
        p = p->next;
        i++;
    }
    return i;
}
template<class T>
void SinglyList<T>::ListDeletAll()
{
    Node<T>*P = this->head;
    Node<T>*q = P->next;
    for (int i = 1; i <= NodeNum(); i++)
    {
        while (q != NULL)
        {
            q = q->next;
            ListDelet(i);
        }
        free(P);
    }
}



Node.h

#pragma once
/**********************************
* algorithms.h :单链表结点类 *
* author : shilei                 *
* created : 2017.3.15             *
***********************************/

template<class T>
class Node
{   
public:
    T data;
    Node<T> *next;
    Node()
    {
        this->next = NULL;
    }
    Node(T data,Node<T>*next = NULL)
    {
        this->data = data;
        this->next = next;
    }

};

test.h

#pragma once
/**********************************
* algorithms.h :多项式加法 *
* author : shilei                 *
* created : 2017.3.15             *
***********************************/
#include <iostream>  
#include <cstdlib>

using namespace std;



struct node
{
    float coef;
    int exp;
    node *next;
};

class poly
{
public:
    node * head;
    void CreatePoly();
    void SortPoly();
    void AddPoly(poly B);
    void printpoly();
    friend ostream& operator<<(ostream&, poly list);
};

void poly::CreatePoly()
{
    cout << "请输入多项式的项数:";
    int n;
    cin >> n;
    cout << endl;
    node *s, *r;
    head = new node;

    r = head;
    for (int i = 0; i < n; i++)
    {
        s = new node;
        cin >> s->coef;
        cin >> s->exp;
        s->next = 0;
        r->next = s;
        r = s;
    }
}

void poly::SortPoly()
{

    bool isChange = true;
    while ( head->next && isChange)
    {
        node *q = head;
        isChange = false;//标志当前这一轮中又没有发生元素交换,如果没有则表示数组已经有序  
        for (; q->next!=NULL; q = q->next)
        {
            if (q->exp > q->next->exp)
            {
                swap(q->exp, q->next->exp);
                swap(q->coef, q->next->coef);
                isChange = true;
            }
        }
    }
}

void poly::AddPoly(poly lb)
{
    node *pa, *pb, *qa, *qb;
    pa = head;
    pb = lb.head;
    qa = pa->next;
    qb = pb->next;
    float sum;
    while (qa != NULL && qb != NULL)
    {
        if (qa->exp < qb->exp)
        {
            pa = qa;
            qa = qa->next;
        }
        else if (qa->exp > qb->exp)
        {

            pb->next = qb->next;
            pa->next = qb;
            qb->next = qa;
            pa = qb;
            qb = pb->next;
        }
        else if (qa->exp == qb->exp)
        {
            sum = qa->coef + qb->coef;
            if (sum == 0)
            {
                pa->next = qa->next;
                delete qa;
                qa = pa->next;
                pb->next = qb->next;
                delete qb;
                qb = pb->next;
            }
            else
            {
                qa->coef = sum;
                pa = qa;
                qa = qa->next;
                pb->next = qb->next;
                delete qb;
                qb = pb->next;
            }

        }
    }
}
ostream& operator <<(ostream&out, poly list)
{
    node *q = list.head;
    while (q->next != NULL)
    {
        if (q->next->exp == 0)
        {
            out << q->next->coef<<"+";
        }
        else if(q->next->next == NULL)
        {
            out << q->next->coef << "X^" << q->next->exp;
        }
        else {
            out << q->next->coef << "X^" << q->next->exp << "+";
        }
        q = q->next;
    }

    cout << endl;
    return out;
}

Linklist.cpp

/**********************************
* algorithms.cpp :单链表的基本操作 *
* author : shilei                 *
* created : 2017.3.15             *
***********************************/
#include "Linklist.h"
#include <exception>
#include"Node.h"
//#include"poly_node.h"
#include"test.h"

int A[5];
int B[7];
void Two_Link_List()
{
    cout << "请输入5个非递减的整型数据元素:"; 
    for (int n = 0; n < 5; n++)
    {
        cin >> A[n];
    }
    cout << endl;
    cout << "请输入7个非递减的整型数据元素:";
    for (int n = 0; n < 7; n++)
    {
        cin >> B[n];
    }
    SinglyList<int>Link1(A, 5), Link2(B, 7);
    cout << endl;
    cout << "*****Link1的数据元素:" << Link1;
    cout << endl;
    cout << "*****Link2的数据元素:" << Link2;
    Link1 += Link2;
    cout << endl;
    cout << endl;
    cout << "*********************两个单链表连接操作**********************" << endl;
    cout << endl;
    cout << "Link1与Link2连接后为:" << Link1;
    cout << endl;
    cout << "Link1与Link2连接后结点数为:" << Link1.NodeNum() << endl;
    cout << endl;
    cout << endl;
    cout << "*********************两个单链表合并操作**********************" << endl;
    cout << endl;
    SinglyList<int>CLink1(A, 5), CLink2(B, 7);
    Node<int>*pa = CLink1.head->next;
    Node<int>*pb = CLink2.head->next;
    Node<int>*pc = CLink1.head;
    while (pa&&pb)
    {
        if (pa->data <= pb->data)
        {
            pc->next = pa;
            pc = pa;
            pa = pa->next;
        }
        else
        {
            pc->next = pb;
            pc = pb;
            pb = pb->next;
        }
    }
    while (pa!=NULL)/*两个while语句将其中一个剩下的链表元素连接到合并的链表后面。*/
    {
        pc->next = pa;
        pc = pa;
        pa = pa->next;
    }
    while (pb!=NULL)
    {
        pc->next = pb;
        pc = pb;
        pb = pb->next;
    }
    cout << "Link1与Link2合并后单链表的结点数:" << CLink1.NodeNum() << endl;
    cout << endl;
    cout << "Link1与Link2合并后单链表数据元素:" << CLink1 << endl;
    cout << endl;
    CLink1.ListDelet(3);
    cout<<"删除第3个结点后数据元素为:"<< CLink1 << endl;
    cout << endl;
    CLink1.ListInsert(5, 100);
    cout << "第5个结点插入数据100后数据元素为:" << CLink1 << endl;
    free(CLink2.head);
};
void poly_add()
{
    poly y1, y2;
    cout << "*****************多项式加法运算*********************";
    cout << endl;
    cout << endl;
    y1.CreatePoly();
    cout << endl;
    cout << "输入的多项式y1为:" << y1;
    y2.CreatePoly();
    cout << endl;
    cout << "输入的多项式y2为:" << y2;
    cout << endl;
    y1.SortPoly();
    y2.SortPoly();
    cout << "y1排序后的多项式为:" << y1;
    cout << endl;
    cout << "y2排序后的多项式为:" << y2;
    cout << endl;
    y1.AddPoly(y2);
    cout << "y1+y2后多项式为" << y1;
}
int main()
{

    poly_add();
    cout << endl;
    cout << endl;
    Two_Link_List();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/handoking/article/details/79632648