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

Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
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
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
please answer my question... Write a template class that implements an extended queue (use Linked List)....
please answer my question... Write a template class that implements an extended queue (use Linked List). Ex: ExtendedQueue<int> int_queue; ExtendedQueue<double> double_queue; ExtendedQueue<char> char_queue; –Write a program to test this template class in c++. the details of the ExtendedQueue: insert delete front rear queue size. the output will be: Inserting 2 Inserting 4 Inserting 6 Front element is: 2 Removing 2 Inserting 8 Queue size is 3 Removing 4 Removing 6 Removing 8 Queue Is Empty
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...
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...
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....
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows:...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows: There is a MAZE. There is a mouse inside the maze. The mouse must find the exit from the maze. When the user clicks the letter C or c a CAT is added to the maze. The cat will run through the maze and if it finds the mouse it should eat it and the game is over! User can add as many cats...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT