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.
Merge two ordered singly linked lists of integers into one ordered list in C++
Merge two ordered singly linked lists of integers into one ordered list in C++
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
Implement a function that reverses a singly linked list in one pass of the list. The...
Implement a function that reverses a singly linked list in one pass of the list. The function accepts a pointer to the beginning of the list and returns a pointer to the beginning of the reversed list. The function must not create any new nodes. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!
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...
Singly Linked List In Javascript - How to implement when given functions and tests to pass...
Singly Linked List In Javascript - How to implement when given functions and tests to pass ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran in Node.js with the 'runTests()' command. Please help me generate the singly linked list that will work and pass the tests at the bottom. I will only use this as a reference so please add comments to explain what you...
Implement a function that detects if there is a “loop” in a singly linked list. A...
Implement a function that detects if there is a “loop” in a singly linked list. A “loop” is defined as a link from one node to itself or to another node that is before the node in the list. The function may only make one pass through the list. It must return 1 if a “loop” exists; 0 otherwise. Be sure to test your function! You may start with code that you have written for other labs. This is a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT