点产品计算链接列表实现
Dot Product Calculation Link List Implementation
我试图在我的下面代码上实现一个点产品计算公式到链接的列表实现中,我有以下错误:请求会员'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
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 如何理解C++中链表的自由列表实现?
- 单链接列表实现,规则为 3
- 使用邻接列表C++实现 Dijkstra
- 内部源代码 - 在链接列表实现中插入元素上的删除会破坏整个应用程序
- 点产品计算链接列表实现
- 跳过 cpp 中的列表实现
- 使用链接列表实现stack类来验证XML文件
- 带有列表实现的逻辑模拟器c++
- C 链接列表实现
- 使用链接列表实现堆栈,调试断言失败
- C++跳过列表实现-返回的值已损坏,内存泄漏
- 链接数组列表实现
- 自定义列表实现迭代器无法访问最后一个元素 (c++)
- 链接列表C++实现
- 使用列表C++实现选择排序
- 为什么头部不改变 - 链接列表实现
- boost 如何将类型列表实现为类的"options"?
- 不同类的成员列表实现不起作用
- 自定义STL列表实现问题