Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this project you will create your own stack and name it MyStack. Requirements for MyStack: Use an array of Strings as the underlying data
In this project you will create your own stack and name it MyStack.
Requirements for MyStack:
Use an array of Strings as the underlying data structure. Just an array of string not a generic data
type
Top should store the index of the last element. Pay attention that in the example of the textbook
top pointed to the empty space after the last element. But in MyStack top should point the last
element the first element to be popped For instance, if the following is the current state of the
stack, top should be
Top
For an empty stack problem: prevent the user to pop from an empty stack and print a message.
For a full stack problem: if the stack size is n and the user attempts to push the element number
n the first element should be removed. One way to implement it is copying the old array in a
new one without the first element. But you should NOT use this approach because it is too
expensive. With this approach, after the stack is full, for each push you need On time. You must
use a circular array to implement it with the cost of O for every operation.
There is a circular array implementation in your textbook chapter in this implementation
the stack will be full if n space of the array is taken. But with your implementation stack should
be considered as full if all the elements are full. So with an array of size n textbook example
holds maximum n values but your implementation will hold n values.
In the driver class you should let the user test your stack. The following output is part of the test:
What is the size of the stack?
Choose from the menu:
Push.
Pop.
Peak.
Exit.
Enter an integer to push into the stack:
Elements in the stack:
Choose from the menu:
Push.
Pop.
Peak.
Exit.
Enter an integer to push into the stack:
Elements in the stack:
Choose from the menu:
Push.
Pop.
Peak.
Exit.
Enter an integer to push into the stack:
Elements in the stack:
Choose from the menu:
Push.
Pop.
Peak.
Exit.
Enter an integer to push into the stack:
Elements in the stack:
Choose from the menu:
Push.
Pop.
Peak.
Exit.
is removed from the stack.
Elements in the stack:
Choose from the menu:
Push.
Pop.
Peak.
Exit.
Elements in the stack:
As you see on the above example, a stack with size is created. Then user four times selected
the push operator and tried to push and When was pushed, was remove and
became the top of the stack. So the fifth operation was pop, which popped Your code should
print the elements of the stack after each operation.
Then create a class that converts an infix expression to a postfix one InfixToPostfixjava and
create another class that evaluates a postfix expression to find the result PostfixEvaluatorjava
In the same driver class, after giving the chance for the user to test your stack. Prompt the user
to enter an infix expression. Your application should show the postfix form of the input and the
result. Then ask if the user wants to enter another infix expression. You should use MyStack,
InfixToPostfix and PostfixEvaluator classes for this part of the project.
Sample output for the second part of the project:
Enter an infix expression:
The postfix form of the expression is:
Result
Do you want to continue?
yes
Enter an infix expression:
The postfix form of the expression is:
Result
Do you want to continue?
no
Your program must accept a string and your code should separate it to the tokens. The user can
enter a string that contains four main operators operands number that can have more
than one digit and parentheses.
Important: If your program doesnt accept multiple digit numbers, when I test it will give an error.
Here is the algorithm for converting an infix expression to a postfix expression:
While there is a character in the postfix string
Read a token. A token can be a number as operand an open parenthesis, a close
parenthesis, or an operator
If token is push it into the stack
else If token is pop and save them into the output array one by one until is popped
else If token is operand
save it in the output array
else token is operator,
pop all elements in the stack and save them into the output array one by one until
the top element in the stack is lower or equal priority than the token.
push the token onto the stack
Pop all elements in the stack and save them into the output array one by one.
Create an array of strings to store the tokens.
Class names should be:
Tester.java for the driver class with the main method
MyStack.java
InfixToPostfx
PostfixEvaluator.java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started