Description: This lab is about creating a program using data structure that will ask the user to first choose the data type of the elements (string, double, or integer). Second the user should enter how many elements to process. by using “List” the elements will be displayed in the order they were entered, by using “Stack” the elements will be displayed in the reverse order, by using “queue” the elements will be displayed in the ascending order, and by using “setList” the elements will be displayed only once with no repetition.
Code:
import java.util.*; public class lab5 { private static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); double Number, choice; int counter = 0, num; String numb; ArrayList list = new ArrayList(); Stack stack = new Stack(); Queue queue = new PriorityQueue(); TreeSet setList = new TreeSet(); System.out .println("Please select the data type of the elements 1) double 2) interger 3) string"); choice = scanner.nextDouble(); if (choice == 1) { System.out .println("Please enter the number of elements to process: "); choice = scanner.nextDouble(); while (counter < choice) { System.out.println("Please enter a number: "); Number = scanner.nextDouble(); list.add(Number); stack.push(Number); queue.offer(Number); setList.add(Number); counter++; } System.out .println("\n The elements in the order they were entered: "); for (int i = 0; i 0) { System.out.print(queue.peek() + " "); queue.poll(); } System.out.println("\n The elements only once (no repetitions): "); while (!setList.isEmpty()) { System.out.print(setList.first() + " "); setList.remove(setList.first()); } } else if (choice == 2) { System.out .println("Please enter the number of elements to process: "); num = scanner.nextInt(); counter = 0; while (counter < num) { System.out.println("Please enter a number: "); num = scanner.nextInt(); list.add(num); stack.push(num); queue.offer(num); setList.add(num); counter++; } System.out .println("\n The elements in the order they were entered: "); for (int i = 0; i 0) { System.out.print(queue.peek() + " "); queue.poll(); } System.out.println("\n The elements only once (no repetitions): "); while (!setList.isEmpty()) { System.out.print(setList.first() + " "); setList.remove(setList.first()); } } else if (choice == 3) { System.out .println("Please enter the number of elements to process: "); num = scanner.nextInt(); counter = 0; while (counter < num) { System.out.println("Please enter a number: "); numb = scanner.next(); list.add(numb); stack.push(numb); queue.offer(numb); setList.add(numb); counter++; } System.out .println("\n The elements in the order they were entered: "); for (int i = 0; i 0) { System.out.print(queue.peek() + " "); queue.poll(); } System.out.println("\n The elements only once (no repetitions): "); while (!setList.isEmpty()) { System.out.print(setList.first() + " "); setList.remove(setList.first()); } if (choice > 3) { System.out.println("Invalid Choice "); } } else { System.out.println("Invalid Choice "); } } }
Screenshot: