Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:23:06+00:00 2026-05-25T00:23:06+00:00

I’m working on a problem which requires me to use the STL linked list

  • 0

I’m working on a problem which requires me to use the STL linked list class to represent a polynomials. I’ve made a good start on getting the class definition, however I’m a little confused as to where to go next (novice programmer – please excuse my potential ignorance).

class Polynomial
{
    public:
        Polynomial(); //Default constructor
        Polynomial(pair<double,int>); //Specified constructor
        void add(Polynomial);
        Polynomial multiply(Polynomial);
        void print();
    private:
        list<int> order_terms;
        list<double> coeffs;
};

I have two questions:

1) It seems more elegant to store the terms and coefficients as a pair – however I’m unsure how to get that working using the STL list.

2) Regarding the add member function, I’m unsure how to implement it such that I can define a Polynomial and then add terms to it like this:

Polynomial test(pair<3.14,0>);
Polynomial test_2(pair<2,1>);
test.add(test_2);

The main thing I’m having issues with understanding how to access the terms stored in the other object and linking it to the first Polynomial.

Any help greatly appreciated.

EDIT: Code for the add() function – currently not working

void Polynomial::add(const Polynomial& rhs)
{
//Perform some sort of sort here to make sure both lists are correctly sorted
//Traverse the list of terms to see if there's an existing nth order
//term in the list on the left-hand-side polynomial.
list<int>::iterator itr;
list<int>::iterator itl;
for(itr=rhs->terms.begin(); itr!=rhs->terms.end(); itr++)
{
    bool match=0;
    //See if there's an existing terms, if so add to it
    for(itl=terms.begin(); itl!=terms.end(); itl++)
    {
        if(*itl->second)==*itr->second)
        {
            *itl->first+=*itr->first;
            match = 1;
         }      
    }

    //If not, this is the first nth order term so just push it onto the list
    if(!match){ terms.push_back(*itr); //Perform the sort again }         
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:23:06+00:00Added an answer on May 25, 2026 at 12:23 am

    Others have explained list<pair<double, int> > (and I like shelleybutterfly’s suggestion to derive Polynomial from the list, except that I’d make it protected, not public, so that outside code is not too free to mess with the contents of the list).

    But the add function is a little tricky, because adding two polynomials doesn’t generally mean concatenating them or adding their terms together. The operation is actually more like merging— and you’ll soon see that the lists must be sorted. (In fact, it’s more natural to represent polynomials as vectors, but I guess that’s not the assignment.)

    I suggest you implement Polynomial::add(pair<double, int>), first, then implement the other one (add(Polynomial &)) in terms of that.

    I don’t want to spell it out too much, since this looks like homework. Is this enough to point you in the right direction?

    EDIT:
    Your new code looks correct (albeit inefficient) if you fix a couple of bugs:

    void Polynomial::add(const Polynomial& rhs)
    {
      // Don't forget to implement the sorting routine.
    
      // The iterators must be of the correct type. And itr must be const,
      // since you have declared rhs to be a const reference. The compiler
      // will not allow you to have an iterator with the power to alter
      // a const thing.
    
      list<pair<double,int> >::const_iterator itr;
      list<pair<double,int> >::iterator itl;
    
      for(itr=rhs->terms.begin(); itr!=rhs->terms.end(); itr++)
        {
          bool match=false;
          for(itl=terms.begin(); itl!=terms.end(); itl++)
            {
              // You have an extra parenthesis here, and too much dereferencing.
              if(itl->second == itr->second)
                {
                  itl->first+=itr->first;
                  match = true;
                }
            }
          if(!match)
            { terms.push_back(*itr); //Perform the sort again
            } // Be careful not to catch the closing brace in a comment
        }
      }
    

    Once it is working, you can think about ways to make it cleaner and more efficient. For example, if you insert the new term in the right place, the list will always be in the right order and there will be no need for a sort routine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.