Question

In: Computer Science

Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT...

Please use C++:

Data Abstraction, Bags and Stacks:

  1. Define a class DoublyLinkedBag that implements the ADT BagInterface by using a doubly linked chain, as shown in Figure 4-10 of your textbook. You will also need to define the class Node described in Excercise 10 of Chapter 4. Your solution to this problem requires the creation/development of four files: Node.h, Node.cpp, DoublyLinkedBag.h and DoublyLinkedBag.cpp. This repository already contains BagInterface.h that contains the declaration of the BagInterface needed by this problem.

  2. Convert the following infix expressions to postfix form by using the algorithm given in Chapter 6.

    1. a - b + c
    2. a / (b * c)
    3. (a + b) * c
    4. a / b / c - (d + e) * f
    5. a - (b + c * d) / e
  3. Create a new file named question4.h and write a function named isPalindrome that returns a bool value and takes one parameter by constant reference of type std::string. This function must use an instance of ArrayStack to test whether the given string is a palindrome. It shall return true if the given string is a palindrome and false otherwise.

  4. Discuss the advantages and disadvantages of an array-based implementation of the ADT stack as compared to a link-based implementation.

Solutions

Expert Solution

PREFIX TO POSTFIX
#include <iostream> 
#include <stack> 

using namespace std; 
bool isOperator(char x) {    
switch (x) {       
   case '+':      
   case '-':      
   case '/':       
​​​​​   case '*':      
 return true;  
  }    
return false; 
} 

string convertToPostfix(string prefix) {    
stack<string> expression;    
int length = prefix.size();  
  for (int i = length - 1; i >= 0; i--) {      
 if (isOperator(prefix[i])) {          string op1 = expression.top();          expression.pop();          string op2 = expression.top();          expression.pop();          string temp = op1 + op2 + prefix[i];          expression.push(temp);      
 }      
 else          expression.push(string(1, prefix[i]));   
 }   
 return expression.top();
 }

 int main() {    
string prefix = "/+XY+NM";    cout<<"Prefix expression : "<<prefix<<endl;    
cout<<"Postfix expression : "<<convertToPostfix(prefix);    return 0; }

INFIX TO POSTFIX

#include<iostream>
#include<stack>
#include<locale>     
 //for function isalnum()
using namespace std;

int preced(char ch) {
   if(ch == '+' || ch == '-') {
      return 1;              //Precedence of + or - is 1
   }else if(ch == '*' || ch == '/') {
      return 2;            //Precedence of * or / is 2
   }else if(ch == '^') {
      return 3;            //Precedence of ^ is 3
   }else {
      return 0;
   }
}

string inToPost(string infix ) {
   stack<char> stk;
   stk.push('#');               //add some extra character to avoid underflow
   string postfix = "";         //initially the postfix string is empty
   string::iterator it;

   for(it = infix.begin(); it!=infix.end(); it++) {
      if(isalnum(char(*it)))
         postfix += *it;      //add to postfix when character is letter or number
      else if(*it == '(')
         stk.push('(');
      else if(*it == '^')
         stk.push('^');
      else if(*it == ')') {
         while(stk.top() != '#' && stk.top() != '(') {
            postfix += stk.top(); //store and pop until ( has found
            stk.pop();
         }
         stk.pop();          //remove the '(' from stack
      }else {
         if(preced(*it) > preced(stk.top()))
            stk.push(*it); //push if precedence is high
         else {
            while(stk.top() != '#' && preced(*it) <= preced(stk.top())) {
               postfix += stk.top();        //store and pop until higher precedence is found
               stk.pop();
            }
            stk.push(*it);
         }
      }
   }

   while(stk.top() != '#') {
      postfix += stk.top();        //store and pop until stack is not empty.
      stk.pop();
   }

   return postfix;
}

int main() {
   string infix = "a-b+c";
​​​   cout << "Postfix Form Is: " << inToPost(infix) << endl;
}

Postfix: ab-c+

Screenshot of implementation and output.

you run other expression and get postfix expression. if you don't manually enter just insert one line for user entry.

For more detail comment.


Related Solutions

In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in...
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in Chapter 7. Use an instance of the class ArrayList to contain a queues entries. Then write a driver/test program adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList. Deliverables: EmptyQueueException.java (given in Lab_7_StartUp folder) QueueInterface.java (given in Lab_7_StartUp folder) ArrayListQueue.java (need to create) Lab7.java (need to create) QueInterface: @author Frank M. Carrano @author Timothy M....
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square...
please solve using jupyter notebook . 10.9- (Square Class) Write a class that implements a Square shape. The class should contain a side property. Provide an __init__ method that takes the side length as an argument. Also, provide the following read-only properties: a) perimeter returns 4 × side. b) area returns side × side. c) diagonal returns the square root of the expression (2 × side2). The perimeter, area and diagonal should not have corresponding data attributes; rather, they should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT