Answered step by step
Verified Expert Solution
Question
1 Approved Answer
g 2022CSCI 211Week 6Queues] - Main.java 3 6 7 8 9 Main.java Queues.java X Stacks.java X public class Main { H public static void
g 2022\CSCI 211\Week 6\Queues] - Main.java 3 6 7 8 9 Main.java Queues.java X Stacks.java X public class Main { H public static void main(String[] args) { B 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 8 32 A 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 A B B A B A B A B A A } //array declaration String[] cities = {"Philadelphia, PA", "Harrisburg, PA", "Pittsburgh, PA", "Cleveland, OH", "Toledo, OH", "Gary, IN", "Chicago, IL"}; //create stack class object Stacks stack = new Stacks(); System.out.println("Destination from Philadelphia to Chicago"); for (String city cities) { System.out.println(city); } //call push for (String city cities) { stack.push(city); } //call pop for (int i = 0; i < cities.length; i++) { cities[i] = stack.pop(); } //print the reverse array System.out.println(); System.out.println("Destination from Chicago to Philadelphia"); for (String city cities) { System.out.println(city); } //queue object //adding element to the queue Queues queue = new Queues (cities.length); System.out.println(" "); // adding elements to the queue for (int i=0; i < cities.length; i++) { queue.enqueue (cities[i]); System.out.println(cities[i]+" being added, queue size = "+queue.size()); } // removing elements from the queue System.out.println(" "); for(int i=0; i < cities.length; i++) { } queue.dequeue (); System.out.println(cities[i]+" removed, queue size = "+queue.size()); Main CH Q x A2 A Event Log 30:1 CRLF UTF-8 4 spaces g 2022\CSCI 211\Week 6\Queues] - Stacks.java 1 2 3 4 5 6 7 8 9 10 11 12 15 16 17 18 19 20 21 22 23 WWW.NNNNNNN 24 27 28 29 30 25 26 A 32 34 Main.java X Queues.java X Stacks.java /*Stacks create software for a stack as an object and create a simple stack class and then test it by using the stack to reverse the items in an array of Strings. Edited by V. Tuon Feb 28,2022 35 31 B 36 public class Stacks { # 37 B B 33 A A } public static final int MAX = 100; int top; String[] stringStack = new String [MAX]; //stack public Stacks() { this.top = -1; //set top to -1 every time new data adds } //push method to push element in stack public boolean push (String newEntry) { //check if stack is overflow if (top >= (MAX - 1)) { } } System.out.println("Stack Overflow!"); } else { return false; stringStack [++top] = newEntry; return true; //pop method to pop element in stack public String pop() { } //check if stack is empty if (top < 0) { return "Stack is Empty!"; } else { String entryPopped = stringStack [top--]; return entryPopped; Main C Q x A2 1 ^ V Event Log 6:14 CRLF UTF-8 4 spaces 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 60 61 Main.java X Queues.java Stacks.java X public class Queues 62 65 66 67 E A A A A A A # + + private String arr[]; // String array to store city names private int front; // points to front element of the queue private int rear; // points to the last element of the queue private int capacity; // maximum capacity private int count; // current size of the queue // Constructor to initialize the max size of the queue Queues (int size) { arr = new String[size]; } } // function to remove the first element from the queue public void dequeue () { capacity = size; front = 0; } rear = -1; count = 0; } // checking for queue underflow if (isEmpty()) { } // function to add the element to the queue public void enqueue (String item) { // checking for queue overflow if (isFull()) { System.out.println("UnderFlow Program Terminating..."); System.exit( status: 1); front = (front + 1) % capacity; count--; } System.out.println("OverFlow Program Terminating"); System.exit( status: 1); rear = (rear + 1) % capacity; arr[rear] = item; count++; // peek () will return front element of the queue public String peek() { if (isEmpty()) { System.out.println("UnderFlow Program Terminated"); System.exit( status: 1); } return arr[front]; // funtion to return the current size of the queue public int size() { return count; } // function to check if the queue is empty or not public Boolean isEmpty() { return (size() == 0); } // function to check if the queue is full or not public Boolean isFull() { return (size() == capacity); } A4 2 ^ V O Event Log 16:25 CRLF UTF-8 4 spaces
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