Question

2 - 35 pts) You are given the code (including the main function) for evaluating an expression in post-fix format using Stack.

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


class Node{
  
   private:
       int data;
       Node* nextNodePtr;
      
   public:
       Node(){}
      
       void setData(int d){
           data = d;
       }
      
       int getData(){
           return data;
       }
      
       void setNextNodePtr(Node* nodePtr){
               nextNodePtr = nodePtr;
       }  
      
       Node* getNextNodePtr(){
           return nextNodePtr;
       }
          
};

class Stack{

   private:
       Node *headPtr;
  
   public:
       Stack(){
           headPtr = new Node();
           headPtr->setNextNodePtr(0);
       }
  
       Node* getHeadPtr(){
           return headPtr;
       }
  
       bool isEmpty(){
          
           if (headPtr->getNextNodePtr() == 0)
               return true;
          
           return false;
       }
      
      
       void push(int data){
          
           Node* currentNodePtr = headPtr->getNextNodePtr();
           Node* prevNodePtr = headPtr;
          
           while (currentNodePtr != 0){
               prevNodePtr = currentNodePtr;
               currentNodePtr = currentNodePtr->getNextNodePtr();
           }
          
           Node* newNodePtr = new Node();
           newNodePtr->setData(data);
           newNodePtr->setNextNodePtr(0);
           prevNodePtr->setNextNodePtr(newNodePtr);                      
          
       }
          
      
       int peek(){
          
           Node* currentNodePtr = headPtr->getNextNodePtr();
           Node* prevNodePtr = headPtr;
          
           if (currentNodePtr == 0){
               return -1000000; // stack is empty
           }
          
           while (currentNodePtr->getNextNodePtr() != 0)
               currentNodePtr = currentNodePtr->getNextNodePtr();
          
           return currentNodePtr->getData();
          
       }

          
      
       int pop(){  
          
           Node* currentNodePtr = headPtr->getNextNodePtr();
           Node* prevNodePtr = headPtr;
          
           if (currentNodePtr == 0){
               return -1000000; // stack is empty
           }
          
           while (currentNodePtr->getNextNodePtr() != 0){
              
               prevNodePtr = currentNodePtr;
               currentNodePtr = currentNodePtr->getNextNodePtr();              

           }
          
           prevNodePtr->setNextNodePtr(0);
           return currentNodePtr->getData();
      
       }
      

      
};

int main(){

   Stack stack;

   string expression;
  
   cout << "Enter the expression to evaluate: ";
   getline(cin, expression);
  
   char* expressionArray = new char[expression.length()+1];
   strcpy(expressionArray, expression.c_str());
  
   char* cptr = strtok(expressionArray, ", ");
  
   while (cptr != 0){
      
       string token(cptr);
      
       bool isOperator = false;
      
       if ( (token.compare("*") == 0) || (token.compare("/") == 0) || (token.compare("+") == 0) || (token.compare("-") == 0) )
           isOperator = true;
      
       if (!isOperator){
           int val = stoi(token);
           stack.push(val);
       }
      
      
       if (isOperator){
          
           int rightOperand = stack.pop();
           int leftOperand = stack.pop();
          
           if (token.compare("*") == 0){
               int result = leftOperand * rightOperand;
               cout << "intermediate result: " << leftOperand << "*" << rightOperand << "=" << result << endl;
               stack.push(result);
           }          
           else if (token.compare("/") == 0){
               int result = leftOperand / rightOperand;
               cout << "intermediate result: " << leftOperand << "/" << rightOperand << "=" << result << endl;
               stack.push(result);
           }
           else if (token.compare("+") == 0){
               int result = leftOperand + rightOperand;
               cout << "intermediate result: " << leftOperand << "+" << rightOperand << "=" << result << endl;
               stack.push(result);
           }
           else if (token.compare("-") == 0){
               int result = leftOperand - rightOperand;
               cout << "intermediate result: " << leftOperand << "-" << rightOperand << "=" << result << endl;
               stack.push(result);
           }
          
       }
      
      
       cptr = strtok(NULL, ", ");
   }
  
   cout << "final result: " << stack.pop() << endl;
  
  
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Kindly post remaining questions separately Q1) Program import java.util.Scanner; class linked list node head, tail; clapublic void Insert (int new element) node newNodenew node (new element) ; if (newNodenuli) else return neWNode. next null; if

[10:00 AM, 3/7/2019] Narasimha: Editable code:

Q1)

Program

import java.util.Scanner;

class linked_list

{

node head,tail;

class node

{

int element;

node next;

node(int elem)

{

element = elem;

next = null;

}

}

void deleteElement(int deleteData)

{

node t= head, p= null;

if (t != null && t.element == deleteData)

{

head = t.next;

deleteElement(deleteData);

return;

}

while (t != null && t.element != deleteData)

{

p= t;

t = t.next;

}

if (t == null) return;

p.next = t.next;

deleteElement(deleteData);

}

public void Insert(int new_element)

{

node newNode = new node(new_element);

if (newNode == null)

return;

else

{

newNode.next = null;

if (head == null)

{

head = newNode;

tail = newNode;

}

else

{

tail.next = newNode;

tail = newNode;

}

}

}

public void printElements()

{

node tempnode = head;

while (tempnode != null)

{

System.out.print(tempnode.element+" ");

tempnode = tempnode.next;

}

System.out.print("");

}

public static void main(String[] args)

{

linked_list llist = new linked_list();

Scanner sc=new Scanner(System.in);

System.out.print("Enter the number of elements you want to insert : ");

int N;

N = sc.nextInt();

for(int i = 0 ; i < N; i++){

int M;

System.out.print("Enter element # " + i + " : ");

M = sc.nextInt();

llist.Insert(M);

}

System.out.print("\n Content of list ( before delete )");

llist.printElements();

System.out.print("\nEnter the number you want to delete : ");

int D;

D = sc.nextInt();

llist.deleteElement(D);

System.out.print("\n Content of list ( after delete ) :");

llist.printElements();

}

}

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are given the code for a singly linked list-based implementation of the List ADT. You...

    You are given the code for a singly linked list-based implementation of the List ADT. You will add a function called isUnique( ) to the List class such that the function will return true if all the integer elements in the List are unique and return false otherwise. The main function is written in such a way that it will create 1000 Lists (each of size listSize) of random integers in a range [1...maxValue] and will call the isUnique function...

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • A binary tree is a complete binary tree if all the internal nodes (including the root...

    A binary tree is a complete binary tree if all the internal nodes (including the root node) have exactly two child nodes and all the leaf nodes are at level 'h' corresponding to the height of the tree. Consider the code for the binary tree given to you for this question. Add code in the blank space provided for the member function checkCompleteBinaryTree( ) in the BinaryTree class. This member function should check whether the binary tree input by the...

  • In this assignment, you will use a Hashtable to determine the common elements in all the...

    In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • #include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...

    #include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace...

    // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace std; class LinkedList; class Node { private: string data; Node* next; public: Node(string s, Node* n); string getData() {return data;} Node* getNext() {return next;} } Node::Node(string s, Node* n) { data = s; next = n; } class LinkedList { private: Node* head; public: LinkedList(); bool insert(string s); friend ostream& operator<<(ostream& os, LinkedList l); }; LinkedList::LinkedList() : head(nullptr) { } bool LinkedList::insert(string s) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT