Question

Implementing Polynomials using Singly Linked List in C++

Implementing Polynomials using Singly Linked List in C++

Homework Answers

Answer #1

Greetings!!!!

Please find the implementation of polynomial using singly linked list in c++ with a sample run screen shot.

#include <iostream>
#include <malloc.h>
#include<conio.h>
using namespace std;

//Node of the polynomial linked list is implemented by below structure
struct node
{
    int exponent;
    int cofficient;
    struct node *next;
};

//Initially set start pointer to point to the NULL
struct node *start=NULL;

// Function declaration to create polynomial.
struct node *create_Polynomial(struct node *);

// Function declaration to display polynomial.
struct node *display_Polynomial(struct node *);


int main()
{
    // choice is read by user for creation of polynomial, display of polynomial, and exit from program.
    int choice;
    
    do
    {
        //Display the menu for polynomial.
        cout<<"\n\n\nmain menu";
        cout<<"\n1.Enter the polynomial";
        cout<<"\n2.Display the polynomial";
        cout<<"\n3.Exit";
        
        //Take input as choice for creation of polynomial, display of polynomial, and exit from program
        cout<<"\nEnter Your Choice: ";
        cin>>choice;
        
        switch(choice)
        {
            case 1:
                start=create_Polynomial(start);
                cout<<"\n Polynomial Created Successfully!!! \n";
                break;
            case 2:
                cout<<"\nPolynomial is displayed as below:\n";
                start=display_Polynomial(start);
        }
    }while(choice!=3);
    
    getch();
    return 0;
}

// Function to create polynomial.
struct node *create_Polynomial(struct node *start)
{
    cout<<"\n Enter exponent value in descending order"; 
    struct node *newNode,*ptr;
    int exponent,cofficient;
    cout<<"\nEnter the exponent: ";
    cin>>exponent;
    cout<<"\nEnter its cofficient: ";
    cin>>cofficient;
    
    while (exponent!=-1)
    {
        //Create new Node of polynomial
        newNode=new struct node();
        
        //Assigning cofficient and exponent to the term.
        newNode->exponent= exponent;
        newNode->cofficient= cofficient;
        
        // Setting last node to point to the NULL.
        newNode->next=NULL;
        
        //Check if it is first node of polynomial 
        if (start==NULL)
        {
            start=newNode;
        }
        else
        {
            ptr=start;
            while(ptr->next!=NULL)
            {
               ptr=ptr->next; 
            }
            
            //Adding new node to the last of linked list.
            ptr->next=newNode;
        }
        
        //Take input as exponent
        cout<<"\n Enter exponent or -1 to stop crating polynomial: ";
        cin>>exponent;
        
        //Check for -1, if wanted to end with cration of polynomial
        if (exponent==-1)
            break;
            
        //Take input as cofficient.    
        cout<<"\n Enter its cofficient: ";
        cin>>cofficient;
    }
    return start;
}


// Function to display polynomial
struct node *display_Polynomial(struct node *start)
{
    //Loop every term (Node) of polynomial.
    while(start!=NULL)
    {
        if(start->exponent!=0)
        {
            if(start->cofficient<0)
                cout<<start->cofficient<<"x^"<<start->exponent;
            else
                cout<<"+"<<start->cofficient<<"x^"<<start->exponent;
        }
        else
        {
            if(start->cofficient<0)
                cout<<start->cofficient;
            else
                cout<<"+"<<start->cofficient;
        }
        start=start->next;
    }
    return start;
}

Thank You

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent...
Implementing Polynomials using Singly Linked List in C++ The Term Class Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. • in the term 4X2, the coefficient is 4 and the exponent 2 • in -6X8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as...
Write a C++ recursive function that counts the number of nodes in a singly linked list....
Write a C++ recursive function that counts the number of nodes in a singly linked list. (a) Test your function using different singly linked lists. Include your code. (b) Write a recurrence relation that represents your algorithm. (c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.
PYTHON : Describe a fast recursive algorithm for reversing a singly linked list
PYTHON : Describe a fast recursive algorithm for reversing a singly linked list
JAVA QUESTION: A singly linked list method that will: -A method that will find the largest...
JAVA QUESTION: A singly linked list method that will: -A method that will find the largest number in the list -A method that will find the smallest number in the list THESE ARE TWO SEPARATE BASIC METHODS
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                                                                  void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref;...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does not have tail? Write the code. How much time does it take to Do it?
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does no have tail? Write the code. How much time does it take to do it? (java)
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
How would I go about implementing a function in C++ that swaps the front node and...
How would I go about implementing a function in C++ that swaps the front node and tNode with each other in a singly linked list that has more than two nodes, but I can't swap data? Also what would the Big-O complexity be for this?