Home > database >  Vectors in linked lists
Vectors in linked lists

Time:03-16

#include <iostream>
#include <vector>
#include <iomanip>
#include <stdlib.h>


using namespace std;

struct Monomial
{
    int coefficient;
    int degree;
    Monomial *next;
};

class Polynomial
{
private:
    Monomial *first;
    Monomial *last;
public:
    Polynomial(){
        first = NULL;
        last = NULL;
    }
    ~Polynomial(){}

    void generate_polynomial(vector<vector<int>> V) {
        Monomial *new_node = new Monomial();
        new_node -> coefficient = V; 
        new_node -> degree = V;
        new_node -> next =  NULL;

        if(first == NULL){
            first = last = new_node;
        }
        else{        
            last->next = new_node;
            last = new_node;
        }


    }

    void print_polynomial(){

    }

    void derivative(){

    }

    void slope_at_point(int k){

    }

    void tangent_line_at_point(int k){

    }

};

int main()
{
    Polynomial P;
   // Polynomial D;
   // Polynomial T;

    vector<vector<int>> V = {{2, 6}, {-4, 5}, {1, 4}, {-9, 3}, {8, 2}, {5, 1}, {-6, 0}};

    P.generate_polynomial(V);
    //P.print_polynomial();
    //D = P.derivative();
    //D.print_polynomial();
    //cout << P.slope_at_point(7) << endl;
    //T = P.tangent_line_at_point(7);
    //T.print_polynomial();
    return 0;
}

I am having a problem with both lines 31 and 32, I know if we were to not be passing a vector and instead had

void generate_polynomial(int value){
       

lines 31 and 32 would read

new_node -> coefficient = value ;
new_node -> degree = value;

but since Im passing a 2D vector in as a parameter, im getting confused on what I should put it equal to. As of right now I get the error "expected primary expression before 'int'" for both lines 31 and 32.

CodePudding user response:

OK based on you answer - "I would like to make a Monmial for each entry"

void generate_polynomial(vector<vector<int>> V) {
    for (auto& outer : V) {
     
            Monomial* new_node = new Monomial();
            new_node->coefficient = inner;
            new_node->degree = inner;
            new_node->next = NULL;
            if (first == NULL) {
                first = last = new_node;
            }
            else {
               last->next = new_node;
               last = new_node;
             }
         }
     }
  }

I suspect this is wrong, in that you probably want coefficient to be inner[0] and degree to be inner[1]. But I can only go on what you said.

EDIT

void generate_polynomial(vector<vector<int>> V) {


    for (auto& entry : V) {

        Monomial* new_node = new Monomial();
        new_node->coefficient = entry[0];
        new_node->degree = entry[1];
        new_node->next = NULL;
        if (first == NULL) {
            first = last = new_node;
        }
        else {
            last->next = new_node;
            last = new_node;
        }
    }

}

CodePudding user response:

Here's a possible solution:

  • Passing a vector of monomials as std::vector<std::pair<int,int>>.
    std::vector<std::pair<int, int>> V{
        {2, 6},
        {-4, 5},
        {1, 4},
        {-9, 3},
        {8, 2},
        {5, 1},
        {-6, 0}
    };
  • Using smart pointers for the list of monomials within Polynomial.
struct Monomial {
    int coefficient{};
    int degree{};
    std::unique_ptr<Monomial> next{};
};

class Polynomial {
private:
    std::unique_ptr<Monomial> first{};
    Monomial* last{};
};
  • generate_polynomial just walks the list of monomials, creating a node from each coefficient and degree.
    void generate_polynomial(const std::vector<std::pair<int,int>>& V) {
        for (const auto& [coefficient, degree] : V) {
            auto new_node{std::make_unique<Monomial>(coefficient, degree)};

            if (first == nullptr) {
                first = std::move(new_node);
                last = first.get();
            }
            else {
                last->next = std::move(new_node);
                last = last->next.get();
            }
        }
    }

[Demo]

  •  Tags:  
  • c
  • Related