zoukankan      html  css  js  c++  java
  • 多项式链表多项式相加

    //多项式相加
    LinkList* add2Link(LinkList*L1, LinkList*L2)
    {
    	LinkList*p1, *p2;
    	LinkList*head , *pre, *p;
    	head = new LinkList;
    	head->next = NULL;
    	pre = head;
    	p1 = L1->next;
    	p2 = L2->next;
    	while (p1&&p2)
    	{
    		if (p1->exp == p2->exp)
    		{
    			p = new LinkList;
    			p->coef = p1->coef + p2->coef;
    			p->exp = p1->exp;
    			p1 = p1->next;
    			p2 = p2->next;
    		}
    		else if ((p1->exp) < (p2->exp))
    		{
    			p = new LinkList;
    			p->coef = p1->coef;
    			p->exp = p1->exp;
    			p1 = p1->next;
    		}
    		else
    		{
    			p = new LinkList;
    			p->coef = p2->coef;
    			p->exp = p2->exp;
    			p2 = p2->next;
    		}
    		pre->next = p;
    		pre = p;
    	}
    	if (p1&&!p2)
    	{
    		while (p1)
    		{
    			p = new LinkList;
    			p->coef = p1->coef;
    			p->exp = p1->exp;
    			p1 = p1->next;
    			pre->next = p;
    			pre = p;
    		}
    	}
    	else if (!p1&&p2)
    	{
    		while (p2)
    		{
    			p = new LinkList;
    			p->coef = p2->coef;
    			p->exp = p2->exp;
    			p2 = p2->next;
    			pre->next = p;
    			pre = p;
    		}
    	}
    	pre->next = NULL;
    	return head;
    }
    

      

    struct Node
    {
    	int data;
    	int exp;
    };
    
    list<Node>* add2list(list<Node>* l1, list<Node>*l2)
    {
    	list<Node>l3;
    	
    	while(!l1->empty()&&!l2->empty())
    	{
    		if(l1->back().exp==l2->back().exp)
    		{
    			Node n1=new Node;
    			n1.data=l1->back().data+l2->back().data;
    			n1.exp=l1->back().exp;
    			l3.push_front(n1);
    			l1->pop_back();
    			l2->pop_back();
    		}
    	     else
    		 {
    			 if(l1->back().exp>l2->back().exp)
    			 {
    				 Node n1=new Node;
    			     n1.data=l2->back().data;
    			     n1.exp=l2->back().exp;
    			     l3.push_front(n1);
    			     l2->pop_back();
    			 }
    			 else
    			 {
    				 Node n1=new Node;
    			     n1.data=l1->back().data;
    			     n1.exp=l1->back().exp;
    			     l3.push_front(n1);
    			     l1->pop_back();
    			 }
    		 }
    	}
    
         while(!l2->empty())
    	 {
    		 Node n1=new Node;
    		 n1.data=l2->back().data;
    		 n1.exp=l2->back().exp;
    		 l3.push_front(n1);
    		 l2->pop_back();
    	 }
    	 
    	 while(!l1->empty())
    	 {
    		 Node n1=new Node;
    		 n1.data=l1->back().data;
    		 n1.exp=l1->back().exp;
    		 l3.push_front(n1);
    		 l1->pop_back();
    	 }
    
    	 return *l3;
    }
    

      

  • 相关阅读:
    树与堆
    Python基础
    python基础
    Flask基础知识
    其他(MySQL)
    发生错误:请确认您的电脑是否安装了excel软件,并且您的浏览器是否允许远行excel!具体操作请查阅帮助.
    idea启动项目,报java.lang.OutOfMemoryError: PermGen space 和启动项目很慢的问题解决
    安装jdk,tomcat,oracle,PL/SQL的一些问题
    linux下安装jdk8,nginx
    mybatic中xml新增一条数据获取自增id
  • 原文地址:https://www.cnblogs.com/KennyRom/p/5887309.html
Copyright © 2011-2022 走看看