Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HOW DO I MODIFY THIS CODE WITHOUT USING THE LIBRARY ARRAYLIST??????? import java.io.*; import java.util.Scanner; import java.util.ArrayList; public class StackQueue{ public static void main(String[] args){

HOW DO I MODIFY THIS CODE WITHOUT USING THE LIBRARY "ARRAYLIST"???????

import java.io.*; import java.util.Scanner; import java.util.ArrayList;

public class StackQueue{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("1. Stack Operations"); System.out.println("2. Queue Operations"); System.out.print("Enter your choice: "); int mainChoice = sc.nextInt(); int stackChoice, queueChoice; ArrayList stack = new ArrayList<>(); ArrayList queue = new ArrayList<>(); if(mainChoice == 1){ doStackOperations(stack); }else if(mainChoice == 2){ doQueueOperations(queue); }else{ System.out.println("Please ener correct choice."); } }

public static void printStackMenu(){ System.out.println("********* Stack Operations *********"); System.out.println("1. Add element to stack - push"); System.out.println("2. Remove element from stack and return it - pop"); System.out.println("3. Return the top of the stack - top"); System.out.println("4. Print the elements of stack"); System.out.println("5. Print the size of stack"); System.out.println("6. EXIT"); System.out.print("Enter your choice: "); }

public static void printQueueMenu(){ System.out.println("********* Queue Operations *********"); System.out.println("1. Add element to queue - enqueue"); System.out.println("2. Remove element from queue and return it - dequeue"); System.out.println("3. Return the front of the stack - first"); System.out.println("4. Print the elements of queue"); System.out.println("5. Print the size of queue"); System.out.println("6. EXIT"); System.out.print("Enter your choice: "); }

public static void doStackOperations(ArrayList stack){ Scanner sc = new Scanner(System.in); printStackMenu(); int choice = sc.nextInt(); while(choice != 6){ switch(choice){ case 1: System.out.print("Enter number: "); int num = sc.nextInt(); stack.add(num); break; case 2: if(stack.size() == 0){ System.out.println("Stack is empty."); }else{ System.out.println("Top element is: "+stack.get(stack.size() - 1)); stack.remove(stack.size() - 1); } break; case 3: if(stack.size() == 0){ System.out.println("Stack is empty."); }else{ System.out.println("Top element is: "+stack.get(stack.size() - 1)); } break; case 4: System.out.print("Stack Elements: "); for(int i=stack.size() - 1;i>=0;i--){ System.out.print(""+stack.get(i)+" "); } System.out.println(); break; case 5: System.out.println("Size of the stack: "+stack.size()); break; default: System.out.println("Enter valid choice"); break; } System.out.print("Enter your choice: "); choice = sc.nextInt(); } }

public static void doQueueOperations(ArrayList queue){ Scanner sc = new Scanner(System.in); printQueueMenu(); int choice = sc.nextInt(); while(choice != 6){ switch(choice){ case 1: System.out.print("Enter number: "); int num = sc.nextInt(); queue.add(num); break; case 2: if(queue.size() == 0){ System.out.println("Queue is empty."); }else{ System.out.println("Front element is: "+queue.get(0)); queue.remove(0); } break; case 3: if(queue.size() == 0){ System.out.println("Queue is empty."); }else{ System.out.println("Front element is: "+queue.get(0)); } break; case 4: System.out.print("Queue Elements: "); for(int i=0;i

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

More Books

Students also viewed these Databases questions

Question

7. Discuss the key features of the learning organization.

Answered: 1 week ago