Lab 5
Data Structures!
Description
In this lab we use Data Structures to maintain a list of inputs. The user has the option using a list of 3 datatypes. The user also has the option to give the list a Size. that size will set the limit for the program to ask how many inputs to receive, but in theory unlike Arrays that are fix sized Data Structures in this Program have the potential to increased their size at run time. The program loops until the user inputs [4] to quit the program.
CODE:
//importing data structure types import java.util.LinkedHashSet; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; public class DataStructures { static Scanner input = new Scanner(System.in); static int DataType = 0; public static void main(String[] args) { do{ System.out.println("Please enter the data type:\n\t[1] Integer\n\t[2] Double\n\t[3] String\n\t[4] Exit"); DataType = input.nextInt(); if(DataType>0 && DataType<4){ System.out.println("How many elements?:"); int Elements = input.nextInt(); //initializing data structures int[] myArray1= new int[Elements]; Stack myStack1= new Stack(); PriorityQueue queue1= new PriorityQueue(); Set set1= new LinkedHashSet(); Double[] myArray2= new Double[Elements]; Stack myStack2= new Stack(); PriorityQueue queue2= new PriorityQueue(); Set set2= new LinkedHashSet(); String[] myArray3= new String[Elements]; Stack myStack3= new Stack(); PriorityQueue queue3= new PriorityQueue(); Set set3= new LinkedHashSet(); switch(DataType){ case 1: for(int i=0; i<myArray1.length; i++){ //for an order listing System.out.println("Element Number "+i); int Element1 = input.nextInt(); myArray1[i]=Element1; //for reverse order listing myStack1.push(Element1); //for ascending order listing queue1.offer(Element1); //for non repetitive listing set1.add(Element1); } break; case 2: for(int i=0; i<myArray1.length; i++){ //for an order listing System.out.println("Element Number "+i); double Element2 = input.nextDouble(); myArray2[i]=Element2; //for reverse order listing myStack2.push(Element2); //for ascending order listing queue2.offer(Element2); //for non repetitive listing set2.add(Element2); } break; case 3: for(int i=0; i<myArray3.length; i++){ //for an order listing System.out.println("Element Number "+i); String Element3 = input.nextLine(); myArray3[i]=Element3; //for reverse order listing myStack3.push(Element3); //for ascending order listing queue3.offer(Element3); //for non repetitive listing set3.add(Element3); } break; } //displays results switch(DataType){ case 1: //showing integers System.out.println("Original order using Arrays: "); for(int number:myArray1){ System.out.println(number);} System.out.println("Reverse order using Push and Pop Stacks: "); for( int i1=0; i1<Elements; i1++){ System.out.println(myStack1.pop());} System.out.println("Ascending order using Priority Queue: "); for( int i1=0; i1<Elements; i1++){ System.out.println(queue1.poll());} System.out.println("Listed only once using a Linked Hash Set: "); for(int Show1: set1){ System.out.println(Show1); } break; case 2: //showing doubles System.out.println("The list of doubles entered in original order using Arrays: "); for(double number:myArray2){ System.out.println(number);} System.out.println("The list of doubles entered in reverse order using Push and Pop Stacks: "); for( int i1=0; i1<Elements; i1++){ System.out.println(myStack2.pop());} System.out.println("The list of doubles entered in ascending order using Priority Queue: "); for( int i1=0; i1<Elements; i1++){ System.out.println(queue2.poll());} System.out.println("The list of doubles listed only once using a Linked Hash Set: "); for(Double Show2: set2){ System.out.println(Show2);} break; //showing strings case 3: System.out.println("The list of Strings entered in original order using Arrays: "); for(String number:myArray3){ System.out.println(number);} System.out.println("The list of Strings entered in reverse order using Push and Pop Stacks: "); for( int i1=0; i1<Elements; i1++){ System.out.println(myStack3.pop());} System.out.println("The list of Strings entered in ascending order using Priority Queue: "); for( int i1=0; i1<Elements; i1++){ System.out.println(queue3.poll());} System.out.println("The list of Strings listed only once using a Linked Hash Set: "); for(String Show3: set3){ System.out.println(Show3);} break; } } }while(DataType != 4); } }
OpenLap Often Crops the codes and deletes special characters In case this happens the full code can be downloaded by clicking this link
ScreenShot
Leave a Reply