点产品计算链接列表实现

Dot Product Calculation Link List Implementation

本文关键字:列表 实现 链接 计算      更新时间:2023-10-16

我试图在我的下面代码上实现一个点产品计算公式到链接的列表实现中,我有以下错误:请求会员'add_node'in'B',哪个是指指针类型'linked_list {aka node*}'(也许您打算使用' ->'?)我如何清除并制定工作代码?我不想使用课程

#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
    int data;
    int index;
    node *next;
};
typedef node* linked_list;
node *head = NULL;
node *tail = NULL;
void add_node(int i,int n)
{
    node *tmp = new node;
    tmp->index = i;
    tmp->data = n;
    tmp->next = NULL;
    if(head == NULL)
    {
        head = tmp;
        tail = tmp;
    }
    else
    {
        tail->next = tmp;
        tail = tail->next;
    }
}
void display(node *head)
{
    while(head!=0)
    {
        cout << head->index <<" ," << head->data << endl;
        display(head->next);
        break;
    }
}
int main()
{
    linked_list A;
    A.add_node(2,7);
    A.add_node(4,5);
    A.add_node(7,8);
    A.add_node(9,4);
    linked_list B;
    B.add_node(3,5);
    B.add_node(4,6);
    B.add_node(9,5);
    int product=0;
while(A!=0 && B!=0)
{
    if(A->index == B->index)
    {
        product = product + A->data * B->data;
        A=A->next;
        B=B->next;
    }
    else if(A->index < B->index)
    {
        A=A->next;
    }
    else
    {
        B=B->next;
    }
}
return product;
    return 0;
}

错误告诉您您需要知道什么。linked_list是指针。您需要使用->操作员,而不是点运算符。

此外,您的node结构不包含称为add_node()的方法。实际上,它根本不包含任何方法。

#include <iostream>
using namespace std;
struct node
{
    int data;
    int index;
    node *next;
};
class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }
    void add_node(int i,int n)
    {
        node *tmp = new node;
        tmp->index = i;
        tmp->data = n;
        tmp->next = NULL;
        if(head == NULL)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
    }
    node* gethead()
    {
        return head;
    }
};
     void display(node *head)
    {
        while(head!=0)
        {
            cout << head->index <<" ," << head->data << endl;
            display(head->next);
            break;
        }
    }
int main()
{
    linked_list A;
    A.add_node(2,7);
    A.add_node(4,5);
    A.add_node(7,8);
    A.add_node(9,4);
    linked_list B;
    B.add_node(3,5);
    B.add_node(4,6);
    B.add_node(9,5);
    display(A.gethead());
    display(B.gethead());
    int product=0;

    node *current_a = A.gethead();
    node *current_b = B.gethead();
    while(current_a != 0 && current_b!=0)
    {
        if(current_a->index == current_b->index)
        {
            product = product + current_a->data * current_b->data;
            current_a=current_a->next;
            current_b=current_b->next;
        }
        else if(current_a->index < current_b->index)
        {
            current_a=current_a->next;
        }
        else
        {
            current_b=current_b->next;
        }
    }
    cout<<"nDot Product : "<< product<<endl;
    return 0;
}
    enter code here