【链表应用】| 一元多项式的操作

90 阅读3分钟

目录

专栏推荐:写文章刚刚起步,各个专栏的知识点后续会补充完善,不断更新好文,希望大
家支持一下。

专栏名字
Elasticsearch专栏es
spring专栏spring开发
redis专栏redis学习笔记
项目专栏项目集锦
修bug专栏bug修理厂

在这里插入图片描述

一. 🦁 要求:

设有两个一元多项式:
p(x)=p0+p1x+p2x2+···+pnxn
q(x)=q0+q1x+q2x2+···+qmxm
多项式项的系数为实数,指数为整数,设计实现一元多项式操作的程序:

  1. 多项式链表建立:以(系数,指数)方式输入项建立多项式,返回所建立的链表的头结点;
  2. 多项式排序: 将所建立的多项式按指数非递减(从小到大) 进行排序
  3. 多项式相加: 实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头指针;
  4. 多项式的输出: 按照po+p1x+p2x2+···+pnxn 格式输出多项式;

二. 代码实现(Java & c)

1. Java实现

Java是一时兴起创作,有bug希望提出!

public class SingleLinkedList {

     class Node{
        private Integer cof;            //系数
        private Integer exp;            //指数
        private Node next;
        public Node(Integer cof,Integer exp,Node next){
            this.cof = cof;
            this.exp = exp;
            this.next = next;
        }

        public Integer getCof() {
            return cof;
        }

        public void setCof(Integer cof) {
            this.cof = cof;
        }

        public Integer getExp() {
            return exp;
        }

        public void setExp(Integer exp) {
            this.exp = exp;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }
    private Node head;      //存放头结点
    private int size;       //记录元素个数

    private Node getTail(){
        if (this.head == null) return null;
        Node node = this.head;
        while(node.next != null){
            node = node.next;
        }
        return node;
    }

    /**
     * 添加元素
     * @param cof
     * @param exp
     */
    public void add(Integer cof,Integer exp){
        Node tail = getTail();
        Node node = new Node(cof,exp,null);
        if (tail == null){
            this.head = node;
        }else {
            tail.next = node;
        }
        this.size++;
    }

    /**
     * 获取元素
     * @param index
     * @return
     */
    public Node getNode(int index){
        checkIndex(index);
        Node node = this.head;
        for (int i = 0;i<index;i++){
            node = node.next;
        }
        return node;
    }

    /**
     * 检查index合法性
     * @param index
     * @throws IndexOutOfBoundsException
     */
    private void checkIndex(int index) throws IndexOutOfBoundsException {
        if(index < 0 || index >= this.size){
            throw  new IndexOutOfBoundsException(index+"指针溢出");
        }
    }

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
    public void sort(){
        Node p,q;
        int temp1;
        int temp2;
        for(p=this.head;p != null;p = p.next){
            for (q = p.next;q != null;q = q.next){
                if(p.exp > q.exp){
                    temp1 = p.exp;
                    p.exp = q.exp;
                    q.exp = temp1;

                    temp2 = p.cof;
                    p.cof = q.cof;
                    q.cof = temp2;
                }
            }
        }
    }


    public Node addMethod(Node head1,Node head2){
        Node node = new Node(-1,-1,null);
        Node p = head1,q = head2 ,b = node;
        while (p != null && q != null){
            if (p.exp == q.exp){
                int res = p.cof+q.cof;
                Node node1 = new Node(res, p.exp, null);
                b.next = node1;
                b = node1;
                p = p.next;
                q = q.next;

            }else if (p.exp < q.exp){
                b.next = p;
                b = b.next;
                p = p.next;

            }else{
                b.next = q;
                b = b.next;
                q = q.next;
            }
        }
        if (p != null){
            b.next = p;
        }else {
            b.next = q;
        }
        return node.next;
    }


}

2.C语言实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct 
{
	float radio;				//系数
	int index;					//指数
}Data;


typedef struct node
{
	Data data;					//数据域
	struct Node* next;			//指针域
	struct Node* prev;
}Node;


static char scan[100];
char* SetScan(char* str)
{
	
	strcpy(scan, str);
	int i ;
	for (i = 0; i <= strlen(str) - 1; i++)
	{
		if (str[i] == '(' || str[i] == ')'||str[i]==',')
		{
			scan[i] = ' ';
		}
	}
	return scan;
}
void SetPol(Node ** head,int tindex,double tradio)
{
	
	Data temp;
	temp.index = tindex;
	temp.radio = tradio;
	Node* curr = NULL,*tail = *head,*prev = *head;
	if(*head)
		for (; tail->next; tail = tail->next)
		{
			prev = last->next;
		}
	if (*head != NULL)
	{
		curr= (Node*)malloc(sizeof(Node));
		curr->data = temp;
		curr->next = NULL;
		curr->prev = NULL;
		*head = curr;
	}
	else
	{
		curr = (Node*)malloc(sizeof(Node));
		curr->data = temp;
		curr->next = NULL;
		curr->prev = prev;
		tail->next = curr;
	}
	
}
void DeleteNode(Node **curr)
{
	if ((*now)->prev)
	{
		Node* temp = (*curr)->prev;
		temp->next = (*curr)->next;
		*curr = (*curr)->prev;

	}
	else
	{
		Node *temp = *curr;
		*curr = NULL;
		free(temp);
	}
	
}
void ArrPol(Node **head)
{
	Data temp;
	Node *p1 = *head;
	Node *p2 = *head; 
	for(p1 = *head;p1;p1=p1->next)
		for (p2 = p1; p2; p2 = p2->next)
		{
			if (p1->data.index > p2->data.index)
			{
				temp = p1->data;
				p1->data = p2->data;
				p2->data = temp;
			}
			else if (p1->data.index == p2->data.index&&p1!=p2)
			{
				p1->data.radio += p2->data.radio;
				DeleteNode(&p2);
			}
		}
}
Node* merge_pol(Node* head1,Node* head2,int op)
{
	Node* p1 = head1, * p2 = head2;
	static Node* head = NULL;
	head = NULL;
	while (p1 || p2)
	{
		if (p1)
		{
			if (!p2 || p1->data.index < p2->data.index)
			{
				set_pol(&head, p1->data.index, p1->data.radio);
				p1 = p1->next;
			}
			else if (p2 && p1->data.index == p2->data.index)
			{
				if (!op) SetPol(&head, p1->data.index, p1->data.radio + p2->data.radio);
				else SetPol(&head, p1->data.index, p1->data.radio - p2->data.radio);
				p1 = p1->next;
				p2 = p2->next;
			}
		}
		if (p2)
		{
			if (!p1 || p1->data.index > p2->data.index)
			{
				if (!op)	SetPol(&head, p2->data.index, p2->data.radio);
				else SetPol(&head, p2->data.index, -p2->data.radio);
				p2 = p2->next;
			}
			else if (p1 && p1->data.index == p2->data.index)
			{
				if (!op) SetPol(&head, p1->data.index , p1->data.radio + p2->data.radio);
				else SetPol(&head, p1->data.index, p1->data.radio - p2->data.radio);
				p1 = p1->next;
				p2 = p2->next;
			}
		}
	}
	return head;
}
void print(Node* head)
{
	Node* curr;
	int flag = 0;
	for (curr = head; curr; curr = curr->next)
	{
		if (curr->data.radio)
		{
			if (!flag)

			{
				printf("%.2g", curr->data.radio);
				flag = 1;
			}
			else
			{
				printf("%+.2g", now->data.radio);
			}
			if (now->data.index)
				printf("x%d", now->data.index);
		}
		
	}
}
int main()
{
	Node* head1=NULL, *head2=NULL, *head=NULL;
	printf("请按照“(系数,指数)”的形式输入第一个多项式,输入“()”以结束\n");
	float radio;
	int index;
	char str[100] = { 0 };
	while (scanf("%s", str) &&strcmp(str,"()"))
	{
		strcpy(scan,set_scan(str));
		sscanf(scan, "%lf %d", &radio, &index);
		SetPol(&head1, radio, index);
	}
	printf("请按照“系数 指数”的形式输入第二个多项式,输入“()”以结束\n");
	while (scanf("%s", str) && strcmp(str, "()"))
	{
		strcpy(scan, set_scan(str));
		sscanf(scan,"%lf %d", &radio, &index);
		set_pol(&head2, radio, index);
	}
	ArrPol(&head1);
	ArrPol(&head2);

	printf("多项式相加所得结果为\n");
	head = merge_pol(head1, head2, 0);
	print(head);
	printf("多项式相减所得结果为\n");
	head = merge_pol(head1, head2, 1);
	print(head);
	
	return 0;
}

在这里插入图片描述

三. 🦁 总结

一个简单的链表操作应用,希望你喜欢