Lab 5 – Data Structure

In this lab, students have to write the program with 3 options of the data types.
Users can choose the data type.

The result will be show as the following:
1) show the elements in order that users entered.
2) show the elements in reverse order that users entered.
3)show the elements in ascending order.
4) show the elements only once.


import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;

public class DataStructure {
	
	public static void main(String[] args) {
		
	int choice = 0;
	int i = 0;
	int j = 0;
	
	Scanner input = new Scanner(System.in);
	System.out.println("Choose Your Data Type: ");
	System.out.println("#1 - String ");
	System.out.println("#2 - Double ");
	System.out.println("#3 - Integer ");
	System.out.println("Enter the number for Data Type:");
	choice = input.nextInt();
	
	LinkedList listi = new LinkedList();
	LinkedList listj = new LinkedList();
	Stack order = new Stack();
	TreeSet tree = new TreeSet();
	
	
	if (choice == 1) {
		System.out.println("Enter number of items: ");
		i = input.nextInt();
		
		System.out.println("Enter the items: ");
		while (j < i) 
		{	
			String item = input.next();
			listi.addFirst(item);
			order.push(item);
			tree.add(item);
			
			j++;
		}
		listj.addAll(listi);
		Collections.sort(listj);
		
		System.out.printf("items entered in the order: %s\n",order);
		System.out.printf("items entered in reverse order: %s\n",listi);
		System.out.printf("items entered in ascending order: %s\n",listj);
		System.out.printf("items entered without repeating: %s\n",tree);
	}
	if (choice == 2) {
		System.out.println("Enter number of items: ");
		i = input.nextInt();
		
		System.out.println("Enter the items: ");
		while (j < i) 
		{	
			Double item = input.nextDouble();
			listi.addFirst(item);
			order.push(item);
			tree.add(item);
			
			j++;
		}
		listj.addAll(listi);
		Collections.sort(listj);
		
		System.out.printf("items entered in the order: %s\n",order);
		System.out.printf("items entered in reverse order: %s\n",listi);
		System.out.printf("items entered in ascending order: %s\n",listj);
		System.out.printf("items entered without repeating: %s\n",tree);
	}
	if (choice == 3) {
		System.out.println("Enter number of items: ");
		i = input.nextInt();
		
		System.out.println("Enter the items: ");
		while (j < i) 
		{	
			Integer item = input.nextInt();
			listi.addFirst(item);
			order.push(item);
			tree.add(item);
			
			j++;
		}
		listj.addAll(listi);
		Collections.sort(listj);
		
		System.out.printf("items entered in the order: %s\n",order);
		System.out.printf("items entered in reverse order: %s\n",listi);
		System.out.printf("items entered in ascending order: %s\n",listj);
		System.out.printf("items entered without repeating: %s\n",tree);
	}
}
}

shot1

shot2

shot3

Leave a Reply

Your email address will not be published. Required fields are marked *