Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.LinkedList; public class LabQueue { LinkedList pool; public LabQueue() { pool = new LinkedList (); } public void clear() { pool.clear(); } public boolean

image text in transcribedimage text in transcribed

import java.util.LinkedList; public class LabQueue { LinkedList pool; public LabQueue() { pool = new LinkedList(); } public void clear() { pool.clear(); } public boolean isEmpty() { return pool.isEmpty(); } public T firstEl() { return pool.getFirst(); } public T dequeue() { return pool.removeFirst(); } public void enqueue(T el) { pool.add(el); } public String toString() { return pool.toString(); } }

import java.util.ArrayList; public class LabStack { ArrayList pool; public LabStack() { pool = new ArrayList(); } public void clear() { pool.clear(); } public boolean isEmpty() { return pool.isEmpty(); } public T topEl() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.get(pool.size()-1); } public T pop() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.remove(pool.size()-1); } public void push(T el) { pool.add(el); } public String toString() { return pool.toString(); } }
import java.util.Scanner; public class StacksTest { public static void main(String[] args) { LabStack s = new LabStack(); s.push("3"); s.push("5"); s.push("hi"); while(!s.isEmpty()) { System.out.print(s.pop() + " "); } s.clear(); //Empty the contents of the stack System.out.println(" Here's how I reverse a string: "); Scanner k = new Scanner(System.in); System.out.print("Enter a string> "); String input = k.nextLine(); for(int i = 0; i  

solve in java

King Fahd University of Petroleum \& Minerals 2. Write a program BalancedParentheses.java. The algorithm for evaluating parentheses is as College of Computer Science and Engineering follows: Information and Computer Science Department (a) Remove all non-parentheses from a mathematical expression. ICS 202 - Data Structures (c) Given a closing parenthesis, pop an opening parenthesis from the stack: Stacks and Queues (i) if the closing and opening parenthesis match, it is a successful match (ii) if the parentheses do not match, the expression is not balanced (iii) if the stack is empty, the expression is not balanced Objectives (d) if, at the end of the program, the stack is empty, then the expression is balanced. The objective of this lab is to design, implement and use Stacks and Queues. For example: [3+(24)+{(ab)}] is balanced, while [3+2(and{7+[ab}] are not Outcomes balanced. A sample session is shown here: After completing this Lab, students are expected to: - Design classes for Stacks and Queues. - Implement Stack and Queue classes using array and Linked Lists. - Developing applications that use stacks and Queues. Notes For the purpose of this lab, you may download the attached programs. Lab Exercises 1. Study the associated files LabStack.java, and LabQueue.java. Also study the class StacksTest.java. Download and run these programs. Note: To avoid confusion with builtip classes Stack and Queue, we use the names LabStack and LabQueue for our classes] A sample session is shown below: 3. Write a program to evaluate postfix expressions using a Labstack, Use the same algorithm as 4. Write a program to reverse the order of elements on stack s using a LabQueue. A sample presented in your slides. For example if the user input is: 32+4, then the output should show session is shown below: the contents of the stack after every operation and also the final result. Make your program interactive so the user can provide input. [Observe that there are spaces in the input to separate the integers. Optional: Check is the postfix expression is valid or not as shown in the sample session below]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Semantics In Databases Second International Workshop Dagstuhl Castle Germany January 2001 Revised Papers Lncs 2582

Authors: Leopoldo Bertossi ,Gyula O.H. Katona ,Klaus-Dieter Schewe ,Bernhard Thalheim

2003rd Edition

3540009574, 978-3540009573

More Books

Students also viewed these Databases questions

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago