本文介绍了如何将矢量元素读入链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个程序,将2个不同向量的非零元素存储到2个不同的链接列表及其索引中。然后想要计算点积并显示结果。我正在努力将带有索引的矢量值读入链接列表。请建议



I would like to write a program that stores nonzero elements of 2 different vectors into 2 different linked lists with their indexes. Then would like t to calculate the dot product and displays the result. I am struggling to read in vector values with indexes into a link list. Please advice

#include <iostream>
#include <vector>
using namespace std;

  struct node{
    int index; /* original index of non-zero array element */
    int value ; /* integer non-zero value at index x */
    node *next;
    };

class linked_list
{
private:
    node *head,*tail;
public:
    linked_list()
    {
        head = NULL;
        tail = NULL;
    }

    void create (int x[], int n)
    {
        link head=0;
        for(int i=0; i<n;i++)
        head=new node (a[i],head);
    }


};


vector<int> x={0,0,7,0,5,0,0,8,0,4};
vector<int> y={0,0,0,5,6,0,0,0,0,5};


int dotproduct() /*Computes the stored nonzero values and returns the result to product*/
{
int product=0;
while(A!=0 && B!=0)
{
    if(A->index == B->index)
    {
        product = product + A->value * B->value;
        A=A->next;
        B=B->next;

    }
    else if(A->index < B->index)
    {
        A=A->next;
    }
    else
    {
        B=B->next;
    }
}
return product;
}

int main()
{
    generate_A_and_B() ;
    int r;
    r=dotproduct();
    cout<<"Dot Product = "<<r<<endl;
    return 0;
}





我的尝试:



我很难将给定的矢量读入链表中,其索引值为



What I have tried:

I am having hard time to read in the given vectors into the linked list with their index values

推荐答案


这篇关于如何将矢量元素读入链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:52