Hi there. I am trying to code a program to add or subtract two polynomials using linked lists. For ex. (+4x^2-2x^1) + (+2x^12-5x^5). So far I have a read function which takes in a string and uses substrings to take the coefficient and exponents and insert it to a node. For example, it reads the string "+4x^3+3x^1", takes the coefficient of 4, turns it into an int, and saves it in the coefficient of struct Node, takes the exponent of 3, and saves it in the exponent variable of that same node, and then points to a new node, which is then filled with coefficient of 3 and exponent of 1. I have tested my code and it works. But now I want to be able to write a function in which I can look at the first node of the Original Polynomial, and a Secondary Polynomial, and add or subtract them and put it into a node of a Result Polynomial, and then keep on moving through the nodes. I was thinking of writind a function void Polynomial::Result(Polynomial Original, Polynomial Second){ but I do not know how to do that. Here is my code:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct Node
    int coefficient;    // the coefficient of the term
    int exponent;       // the exponent of the term
    Node* next;         // NULL if there is no next node    

    Node(){                     //initialize the Node
        coefficient = 0;
        exponent = 0;
        next = NULL;
    Node(int c, int e){         //Takes two integer and stores them in 
        coefficient = c;        //the coefficients and exponents
        exponent = e;
        next = NULL;

class Polynomial{
        Node* head;     
        string list;

        Polynomial();           //Constructor
        ~Polynomial();          //Destructor
        void Read();            //Reads the user-submitted string 
        void Print();           //outputs the current nodes in polynomial form
        //void Add(Polynomial, Polynomial);
        int strtoint(string);
void PrintMenu(){                               //Prints Menu to the screen
    cout << "---------------" << endl;
    cout << "1: Read" << endl;
    cout << "2: Print" << endl;
    cout << "3. Add" << endl;
    cout << "4. Subtract" << endl;
    cout << "0: exit" << endl;
    cout << "---------------" << endl;
int main(){
    Polynomial Original;                    //create the first linked list named Original
    Polynomial Second;                      //create the second linked list named second, 
                                                //used to do operations on the first linked list
    Polynomial Result;                      //Final linked list that has the result of the operations
                                                //of Original and Second LinkedLists

    int MenuAction;                         //Stores the action the user wants the program to perform
    PrintMenu();                            //Prints the menu to the screen
    cin >> MenuAction;

    while (MenuAction != 0){                //Enter 0 to exit menu
        case 1:
        case 2:
        case 3:
            Second.Read();                  //Second is ready to be added to Original
        case 4:
            Second.Read();                  //Second is ready to be subtracted to Original
        if (MenuAction == 0)

        PrintMenu();                        //After a menu selection, ask the user what he/she 
                                            //would like to do next
        cin >> MenuAction;

    return 0;

Polynomial::Polynomial(){                   //Constructor method
    head = NULL;
    list = "";

Polynomial::~Polynomial(){                  //Destructor Method
    Node* temp;

    while (head != NULL){
        temp = head;
        head = head -> next;
        delete temp;

void Polynomial::Read(){
    int coeficient;                                                     //Initialize coefficient to be used in the Node
    int exponent;                                                       //Initialize exponent to be used in the Node
    head = NULL;
    cout << "Please enter a polynomial (ex. +4x^3-2x^0)" << endl;
    cin >> list;

    string tempString = list.substr(0);                                 //Make a substring named tempString
                                                                        //that is an exact copy of string list
    //loop starts here
    while(tempString != ""){
    bool negative = tempString.at(0) == '-';                            //if the sign is -, the bool negative == true
    coeficient = strtoint(tempString.substr(1,tempString.find('x')));   //next element in string will be the coefficient
    tempString = tempString.substr(tempString.find('^') +1);
    if (tempString.find('-') < tempString.find('x')){                    //if the index of '-' is left of 'x'
        exponent = strtoint(tempString.substr(0,tempString.find('-'))); //the exponent = the numbers between '^' and '-'
        tempString = tempString.substr(tempString.find('-'));           //set tempString = the remainder of the string
    }else if (tempString.find('+') < tempString.find('x')){              //if the index of '+' is left of 'x'
        exponent = strtoint(tempString.substr(0,tempString.find('+'))); //the exponent = the numbers between '^' and '+'
        tempString = tempString.substr(tempString.find('+'));           //set tempString = the remainder of the string
        exponent = strtoint(tempString);                                //case to exit the loop when we reach the end of the string
        tempString = "";
    if (negative)                                                       //if bool coefficient == true, make it negative
        coeficient *= -1;
    if (head == NULL)
        head = new Node(coeficient,exponent);                           //send coefficient and exponent to the first node
        for (Node *temp = head; temp->next != NULL; temp = temp->next)  
            temp->next = new Node(coeficient, exponent);
    //loop ends here

int Polynomial::strtoint(string str){                               //Function that takes in a string with numbers and converts
    int integer;                                                    //it to an integer
    std::istringstream(str) >> integer;
    return integer;

void Polynomial::Print(){                                           //Prints the LinkedList to the screen
    for (Node* temp = head; temp != NULL; temp = temp->next){
        cout << temp->coefficient << "x^" << temp->exponent << endl;

/*void Polynomial::Add(Polynomial Original, Polynomial Second){
    int holdcoefficient;
    int holdexponent;
    for (Node* temp = head; temp != NULL; temp = temp->next){

Any ideas are appreciated :)

Forgot to add:
1. All terms in a polynomial are entered in the decreasing order of exponents.
2. If the coefficient of a term is positive, the user always enters character + before the coefficient.
3. All coefficients are integers
4. The input is error-free.

Hence why there's no type checking haha

This article has been dead for over six months. Start a new discussion instead.