Question

In: Computer Science

****C++, put both of them in to one main method, make sure to start with main...

****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well.

thank you

(3) Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in terms of the number of digits for the two operands of each function?

(4) Implement a program that can input an expression in postfix notation and output its value.

Solutions

Expert Solution

3)

//This program will add, substract and multiply two lists
//Lists are defined inside program

class LinkedList
{
    static Node head1, head2;
    static class Node {

        int data;
        Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    /* Adds contents of two linked lists and return the head node of resultant list */
    Node addTwoLists(Node first, Node second) {
        Node res = null; // res is head node of the resultant list
        Node prev = null;
        Node temp = null;
        int carry = 0, sum;

        while (first != null || second != null) //while both lists exist
        {

            sum = carry + (first != null ? first.data : 0)
                    + (second != null ? second.data : 0);

            // update carry for next calulation
            carry = (sum >= 10) ? 1 : 0;

            // update sum if it is greater than 10
            sum = sum % 10;

            // Create a new node with sum as data
            temp = new Node(sum);

            // if this is the first node then set it as head of
            // the resultant list
            if (res == null) {
                res = temp;
            } else // If this is not the first node then connect it to the rest.
            {
                prev.next = temp;
            }

            // Set prev for next insertion
            prev = temp;

            // Move first and second pointers to next nodes
            if (first != null) {
                first = first.next;
            }
            if (second != null) {
                second = second.next;
            }
        }

        if (carry > 0) {
            temp.next = new Node(carry);
        }

        // return head of the resultant list
        return res;
    }

    //function to subtract given lists
    Node subtract(Node first, Node second, int borrow) {
        Node result = new Node(borrow);
        int value = borrow;

        //return null when both list are null
        if (first == null && second == null && borrow == 0)
            return null;

        if (first.data >= second.data) {
            value += first.data - second.data;
            borrow = 0;
        } else {
            if (first.next != null) {
                value += (first.data) + 10 - second.data;
                borrow = -1;
            } else {
                value += (first.data) + 10 - second.data;
                value = value * (-1);
                borrow = 0;
            }
        }
        result.data = value;

        //Recurse
        if (first.next != null || second.next != null || borrow < 0) {
            Node more = subtract(first.next != null ? first.next : null,
                    second.next != null ? second.next : null,
                    borrow < 0 ? -1 : 0);

            result.next = more;
        }
        return result;
    }

    //function to convert list into number
    static int getNumber(Node list) {
        int number = 0;
        while (list != null) {
            number = 10 * number + list.data;
            list = list.next;
        }
        return number;
    }

    //function to traverse a list
    static Node process(Node list) {
        Node head = list;
        int carry = 0, i = 0;

        while (head != null && head.next != null) {
            i = head.data;
            head.data = (carry + i) % 10;
            carry = (i + carry) / 10;
            head = head.next;
        }
        head.data = head.data + carry;
        return reverse(list);
    }

    //function to reverse the given list
    static Node reverse(Node list) {
        if (list == null)
            return null;

        Node current = list, previous = null, forward;
        while (current != null) {
            forward = current.next;
            current.next = previous;
            previous = current;
            current = forward;
        }
        return previous;
    }

    static Node multiply(Node first, Node second) {
        //When both lists are null, return null
        if (first == null || second == null)
            return null;

        int number = getNumber(first);  //convert one list into number

        //traverse the second list and multiply the number with the current element of the list and store in the new list.
        Node current = second;
        Node result = null;
        while (current != null) {
            if (result == null) {
                result = new Node(current.data * number);
            } else {
                //multiply current element with the "number" and store in the new list node
                Node temp = new Node(current.data * number);
                temp.next = result;
                result = temp;
            }
            current = current.next;
        }
        return process(result);
    }

    /*function to print a linked list */
    void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();

        // creating first list
        list.head1 = new Node(7);
        list.head1.next = new Node(5);
        list.head1.next.next = new Node(9);
        list.head1.next.next.next = new Node(4);
        list.head1.next.next.next.next = new Node(6);
        System.out.print("First List is ");
        list.printList(head1);

        // creating seconnd list
        list.head2 = new Node(8);
        list.head2.next = new Node(4);
        list.head2.next.next = new Node(8);
        list.head2.next.next.next = new Node(4);
        list.head2.next.next.next.next = new Node(4);
        System.out.print("Second List is ");
        list.printList(head2);

        // add the two lists and see the result
        Node add = list.addTwoLists(head1, head2);
        System.out.print("\r\nResultant List after Addition is : ");
        list.printList(add);

        Node sub = list.subtract(head1, head2, 0);
        System.out.print("\r\nResultant List after Substraction is : ");
        list.printList(sub);

        Node multiply = list.multiply(head1, head2);
        System.out.print("\r\nResultant List after Multiplication is : ");
        list.printList(multiply);
    }
}

I have pasted the output below

4)

#include<iostream>
#include "stack.h"

using namespace std;

int postfixEval(const char *postfix) {
   Stack s;
   int i = 0, a, b, results;
  

   while(postfix[i] != '\0') {
       if(postfix[i] >= '0' && postfix[i] <= '9')
           s.push(postfix[i] - '0');
       else
           if (postfix[i] == '+') {
               b = s.peek();
               s.pop();
               a = s.peek();
               s.pop();
               s.push(a + b);
           }
           else if (postfix[i] == '-') {
               b = s.peek();
               s.pop();
               a = s.peek();
               s.pop();
               s.push(a - b);
           }
           else if (postfix[i] == '*') {
               b = s.peek();
               s.pop();
               a = s.peek();
               s.pop();
               s.push(a * b);
           }
           else if (postfix[i] == '/') {
               b = s.peek();
               s.pop();
               a = s.peek();
               s.pop();
               s.push(a / b);
           }
       i++;
   }

   return s.peek();
}

int main() {
   string s;
   cout << "Enter postfix expression: ";
   cin >> s;
   cout << "result: " << postfixEval(s.c_str()) << endl;
}

Input:
12+
output:
3

Input:
321+*
output:
9


Related Solutions

****C++, put both of them in to one main method, make sure to start with main...
****C++, put both of them in to one main method, make sure to start with main class, if I get a screenshot of the output as well. thank you (1) A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the...
Write one page report about Telecommunicating Services? Make sure that you put a bibliography in the...
Write one page report about Telecommunicating Services? Make sure that you put a bibliography in the paper?
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial...
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial and Transcendental Functions.
I am down to one last attempt. Please make sure both are correct. 1) Solve the...
I am down to one last attempt. Please make sure both are correct. 1) Solve the given system of differential equations by systematic elimination. (D + 1)x + (D − 1)y = 2 3x + (D + 2)y = −1 (x(t),y(t))=? 2) Solve the given system of differential equations by systematic elimination. D2x − Dy = t (D + 6)x + (D + 6)y = 5 (x(t),y(t))=?
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure...
(MUST BE DONE IN C (NOT C++)) In this task, you will have to make sure you understood the concept of “the dot” in programming. Go ahead and declare an array of characters with a default length (whichever length you want, it's fine). Next, you want to ask the user for a phrase and store the characters in your array, but before you do that, ask the user for an estimated length of the array. If the length given is...
Show full work: Please make sure to start the comparison with -infinity and NO NOT COUNT...
Show full work: Please make sure to start the comparison with -infinity and NO NOT COUNT SWAPS! Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1} using Insertion Sort and determine the total number of comparisons made (do not count swaps)
Options Homework Graph a call option.  Make sure you put the price of the stock on the...
Options Homework Graph a call option.  Make sure you put the price of the stock on the x axis and the change in wealth on the y axis.  There is no such thing as a negative stock price so the y axis does not split the x axis into positive and negative.  Here are the variables to use:  Stock price is currently at $13; stock strike price is $18; the premium is $20.  Make sure you label the change in wealth for the buyer and...
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
Demand and Supply Graph each question separately. Make sure to start with equilibrium and then shift...
Demand and Supply Graph each question separately. Make sure to start with equilibrium and then shift the supply or demand curve to answer the question. Using the information bellow, Draw the Demand and supply Curves for Candy Bars to find the initial equilibrium price and quantity.                                                                                                                        Demand                                            P                           Q                          Pt.                                            $1.25                   1                           A                                            $100                    2                           B                                            $0.75                   3                           C                                            $0.50                   4                           D                                            $0.25                   5                           E                                                           Supply P                                                                      Q                          Pt....
How do I go back to the start of the main method with the given code?...
How do I go back to the start of the main method with the given code? Hello I am making this java program and there is a point in the code where if the user types the character y it will start the program back at the beginning of the main method. How can I do this without drastically changing the code? For instance, if I type y, the program will go back to line 1 and repeat the entire...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT