【问题标题】:Dot Product Calculation Link List Implementation点积计算链接列表实现
【发布时间】:2017-12-01 03:46:10
【问题描述】:

我正在尝试在下面的代码中将点积计算公式实现到链表实现中,但出现以下错误: 请求'B'中的成员'add_node',它是指针类型'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;
}

【问题讨论】:

  • 您的 node 结构没有成员函数。通常使用带有指针的 typedef 会导致悲伤。
  • 不得不说,这些老师肯定会想出新奇的方法来掩饰每天来这里20次的“链表作业”问题。使用链表计算点积?下一步是什么?
  • 您似乎试图在单个全局 headtail 上运行两个不同的链表。这行不通。我认为您想创建一个链表类来将两者分开。
  • 当我使用类时,我在主函数 truct node { int data; 中的 While 循环出现问题整数索引;节点*下一个; };类linked_list {私有:节点*head,*tail;公共:linked_list(){头=空;尾=空; } void add_node(int i,int n){} void display(node *head){}
  • 您应该问一个新问题,而不是对这个问题进行彻底的改变并使(当时正确)答案无效。我要恢复你上次的编辑。但是链表是正确的方法,而且你很接近。您要做的是遍历链表中的节点,而不是链表。您可以从列表中获取头节点并在 while 循环中执行您正在执行的操作。

标签: c++ vector linked-list


【解决方案1】:

这个错误告诉你你需要知道什么。 linked_list 是一个指针。您需要使用-&gt; 运算符,而不是点运算符。

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

【讨论】:

  • 除了,add_node 不是成员函数。错误是对的,您需要 -> 取消引用 A,但是一旦您这样做了,您将遇到一个新错误。
【解决方案2】:
#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

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 2015-07-23
    • 2018-07-26
    • 2020-12-17
    • 2011-06-07
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多