点产品计算链接列表实现

时间:2017-12-01 03:46:10

标签: c++ vector linked-list

我正在尝试将点积计算公式实现到我的下面代码的链表实现中,我遇到以下错误: 会员请求' add_node'在' 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;
}

2 个答案:

答案 0 :(得分:0)

错误告诉您需要知道的内容。 linked_list是一个指针。您需要使用->运算符,而不是点运算符。

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

答案 1 :(得分:0)

#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