Lab #5

Description:
In this program we are going to use data structures that display a group of elements in different orders. A LinkedList is a data structure consists of a group of nodes which together represent a sequence.A Stack is a data structure that allows data to be inserted using the push command or removed using pop command. A Collection is simply an object that groups multiple elements into a single unit. A Set is a Collection that cannot contain duplicate elements. In this program we use TreeSet which sorts according to the natural ordering of its elements. We want the user enters the data and after finishing the program will analyze it by displaying the following:

1- Display all the elements in the order they were entered.
2- Display all the elements in the reverse order they were entered.
3- Display all the elements in ascending order.
4- Display the elements only once (i.e. no repetitions).

Code:

package lab_5;

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

public class dataStructures {
	
	public static void main(String[] args) {
		
	int choice = 0;
	int x = 0;
	int y = 0;
	
	Scanner input = new Scanner(System.in);
	System.out.println("Choose one of the following numbers for the data type of elements: ");
	System.out.println("1 - String ");
	System.out.println("2 - Double ");
	System.out.println("3 - Integer ");
	choice = input.nextInt();
	
	LinkedList list1 = new LinkedList();
	LinkedList list2 = new LinkedList();
	Stack stack = new Stack();
	TreeSet treeset = new TreeSet();
	
	
	if (choice == 1) {
		System.out.println("Enter how many elements to process: ");
		x = input.nextInt();
		
		System.out.println("Enter the elements: ");
		while (y < x) 
		{	
			String e = input.next();
			list1.addFirst(e);
			stack.push(e);
			treeset.add(e);
			
			y++;
		}
		list2.addAll(list1);
		Collections.sort(list2);
		
		System.out.printf("Elements in the order they were entered: %s\n",stack);
		System.out.printf("Elements in the reverse order they were entered: %s\n",list1);
		System.out.printf("Elements in ascending order: %s\n",list2);
		System.out.printf("Elements in ascending order without repeating: %s\n",treeset);
	}
	if (choice == 2) {
		System.out.println("Enter how many elements to process: ");
		x = input.nextInt();
		
		System.out.println("Enter the elements: ");
		while (y < x) 
		{	
			Double e = input.nextDouble();
			list1.addFirst(e);
			stack.push(e);
			treeset.add(e);
			
			y++;
		}
		list2.addAll(list1);
		Collections.sort(list2);
		
		System.out.printf("Elements in the order they were entered: %s\n",stack);
		System.out.printf("Elements in the reverse order they were entered: %s\n",list1);
		System.out.printf("Elements in ascending order: %s\n",list2);
		System.out.printf("Elements in ascending order without repeating: %s\n",treeset);
	}
	if (choice == 3) {
		System.out.println("Enter how many elements to process: ");
		x = input.nextInt();
		
		System.out.println("Enter the elements: ");
		while (y < x) 
		{	
			Integer e = input.nextInt();
			list1.addFirst(e);
			stack.push(e);
			treeset.add(e);
			
			y++;
		}
		list2.addAll(list1);
		Collections.sort(list2);
		
		System.out.printf("Elements in the order they were entered: %s\n",stack);
		System.out.printf("Elements in the reverse order they were entered: %s\n",list1);
		System.out.printf("Elements in ascending order: %s\n",list2);
		System.out.printf("Elements in ascending order without repeating: %s\n",treeset);
	}
}
}

Screenshots:
String
Lab#5a
Double
Lab#5b
Integer
Lab#5c

Leave a Reply

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