📜  使用双链表的多项式的偏导数

📅  最后修改于: 2021-05-04 20:01:34             🧑  作者: Mango

给定一个由双链表表示的2变量多项式,任务是找到存储在双链表中的多项式的偏导数。

例子:

方法:按照以下步骤解决此问题:

  • 声明一个类或结构来存储的节点的内容,表示该系数,即数据,表示对其中X是提高了功率,POWER2表示对其中Y是所提出的功率和指向它的下一个前一个节点POWER1。
  • 声明函数以计算相对于x的导数,相对于y的导数以及相对于xy的导数。
  • 计算并打印获得的派生数。

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Structure of a node
struct node {
    node* link1 = NULL;
    node* link2 = NULL;
    int data = 0;
    int pow1 = 0;
    int pow2 = 0;
};
  
// Function to generate Doubly Linked
// List from given parametrs
void input_equation(node*& head, int d,
                    int p1, int p2)
{
    node* temp = head;
  
    // If list is empty
    if (head == NULL) {
  
        // Create new node
        node* ptr = new node();
        ptr->data = d;
        ptr->pow1 = p1;
        ptr->pow2 = p2;
  
        // Set it as the head
        // of the linked list
        head = ptr;
    }
  
    // If list is not empty
    else {
  
        // Temporarily store
        // address of the head node
        temp = head;
  
        // Traverse the linked list
        while (temp->link2 != NULL) {
  
            // Link to next node
            temp = temp->link2;
        }
  
        // Create new node
        node* ptr = new node();
        ptr->data = d;
        ptr->pow1 = p1;
        ptr->pow2 = p2;
  
        // Connect the nodes
        ptr->link1 = temp;
        temp->link2 = ptr;
    }
}
  
// Function to calculate partial
// derivative w.r.t. X
void derivation_with_x(node*& head)
{
    cout << "Partial derivatives"
         << " w.r.t. x: ";
  
    node* temp = head;
  
    // Traverse the Linked List
    while (temp != NULL) {
  
        if (temp->pow1 != 0) {
            temp->data = (temp->data)
                         * (temp->pow1);
            temp->pow1 = temp->pow1 - 1;
        }
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
  
        temp = temp->link2;
    }
  
    temp = head;
  
    cout << " " << temp->data
         << "(x^" << temp->pow1
         << " y^" << temp->pow2
         << ")";
    temp = temp->link2;
  
    while (temp != NULL) {
        cout << " + "
             << temp->data << "(x^"
             << temp->pow1 << " y^"
             << temp->pow2 << ")";
        temp = temp->link2;
    }
  
    cout << "\n";
}
  
// Function to calculate partial
// derivative w.r.t. Y
void derivation_with_y(node*& head)
{
    cout << "Partial derivatives"
         << " w.r.t. y: ";
  
    node* temp = head;
  
    // Traverse the Linked List
    while (temp != NULL) {
  
        if (temp->pow2 != 0) {
            temp->data = (temp->data)
                         * (temp->pow2);
            temp->pow2 = temp->pow2 - 1;
        }
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
  
        temp = temp->link2;
    }
  
    temp = head;
    cout << " "
         << temp->data
         << "(x^" << temp->pow1
         << " y^"
         << temp->pow2 << ")";
    temp = temp->link2;
  
    while (temp != NULL) {
        cout << " + "
             << temp->data << "(x^"
             << temp->pow1 << " y^"
             << temp->pow2 << ")";
        temp = temp->link2;
    }
    cout << "\n";
}
  
// Function to calculate partial
// derivative w.r.t. XY
void derivation_with_x_y(node*& head)
{
    cout << "Partial derivatives"
         << " w.r.t. x and y: ";
  
    node* temp = head;
  
    // Derivative with respect to
    // the first variable
    while (temp != NULL) {
        if (temp->pow1 != 0) {
  
            temp->data = (temp->data)
                         * (temp->pow1);
            temp->pow1 = temp->pow1 - 1;
        }
  
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
  
        temp = temp->link2;
    }
    temp = head;
  
    // Derivative with respect to
    // the second variable
    while (temp != NULL) {
  
        if (temp->pow2 != 0) {
            temp->data = (temp->data)
                         * (temp->pow2);
            temp->pow2 = temp->pow2 - 1;
        }
  
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
  
        temp = temp->link2;
    }
  
    temp = head;
    cout << " "
         << temp->data << "(x^"
         << temp->pow1 << " y^"
         << temp->pow2 << ")";
  
    temp = temp->link2;
  
    // Print the list after the
    // calculating the derivative
    while (temp != NULL) {
  
        cout << " + "
             << temp->data << "(x^"
             << temp->pow1 << " y^"
             << temp->pow2 << ")";
        temp = temp->link2;
    }
    cout << "\n";
}
  
// Driver Code
int main()
{
    node* head1 = NULL;
  
    // Creating doubly-linked list
    input_equation(head1, 2, 3, 4);
    input_equation(head1, 3, 5, 7);
    input_equation(head1, 1, 2, 6);
  
    // Function Call
    derivation_with_x(head1);
    derivation_with_y(head1);
    derivation_with_x_y(head1);
  
    return 0;
}


输出:
Partial derivatives w.r.t. x:  6(x^2 y^4) + 15(x^4 y^7) + 2(x^1 y^6)
Partial derivatives w.r.t. y:  24(x^2 y^3) + 105(x^4 y^6) + 12(x^1 y^5)
Partial derivatives w.r.t. x and y:  144(x^1 y^2) + 2520(x^3 y^5) + 60(x^0 y^4)

时间复杂度: O(N)
辅助空间: O(1)