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
  • 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...

  • 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> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • #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,...

  • 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;...

  • 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....

  • // 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) {...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

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
ADVERTISEMENT