Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Implementing an ordered list using LinkedList and ListIterator in Java

Problem:

Implement an ordered list using LinkedList and ListIterator (the one in the book). You need to provide the following methods: AddSorted, Retrieve, Delete and Search.

Output:

Not available.

Solution:

/**------------------MyComparable.java---------------------*/
public interface MyComparable 
{
 public int MyCompareTo(Object O);
}

/**------------------OrderedList.java---------------------*/
import java.util.*;
public class OrderedList<E extends MyComparable>
{
 private LinkedList<E> linkedList = new LinkedList<E>();
 private ListIterator<E> listIterator;

 public void AddSorted(E entry)
 {
  listIterator = linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if(entry.MyCompareTo(listIterator.next())>0)
   {
    listIterator.previous();
    listIterator.add(entry);
    return;
   }
  }
  
  listIterator.add(entry);
 }
 
 public void Delete(E entry)
 {
  listIterator=linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if(entry.MyCompareTo(listIterator.next())==0)
   {
    listIterator.remove();
    
    System.out.println("Deleted");
    
    return;
   }    
  }
 }
 
 public E Retrieve(E entry)
 {
  listIterator = linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if( entry.MyCompareTo(listIterator.next()) == 0)
   {
    return listIterator.previous();
   }    
  }
  
  throw new NoSuchElementException();
 }
 
 public void Search(E entry)
 {
  listIterator = linkedList.listIterator();
  
  while(listIterator.hasNext())
  {
   if(entry.MyCompareTo(listIterator.next())==0)
   {
    System.out.println("Found");
    return;
   }    
  }
  System.out.println("Not Found");
 }
 
}
/**----------------------Students.java-------------------------*/
public class Student implements MyComparable
{
 private String name;
 private int id;
 
 
 public Student(String name, int id) {
  this.name = name;
  this.id = id;
 }
 
 public String getName() {
  return name;
 }


 public void setName(String name) {
  this.name = name;
 }


 public int getId() {
  return id;
 }


 public void setId(int id) {
  this.id = id;
 }
 
 public int MyCompareTo(Object o)
    {
        Student student =(Student) o;
        
        if(id < student.getId())
            return -1;
        
        else if(id > student.getId())
            return 1;
        
        else
            return 0;
    }
 
 public String toString() {
  return "Student, " + name + "\n" + "Id, " + id;
 }
}
/**-------------------------Tester.java------------------------------*/
public class Tester
{
 public static void main(String[] args)
 {
  OrderedList<Student> student = new OrderedList<Student>();
  
   student.AddSorted(new Student("name1",1));
   student.AddSorted(new Student("name2",2));
   student.AddSorted(new Student("name3",3));
   student.AddSorted(new Student("name4",4));
   student.AddSorted(new Student("name5",5));

   System.out.println("Searching for \"name1\": ");
   //Searching based on id number not name
   student.Search(new Student("name1",1)); 
   System.out.println();
  
   System.out.println("Retrieving \"name1\": ");
   System.out.println(student.Retrieve(new Student("name1",1)));
   System.out.println();
  
   System.out.println("Deleting \"name1\": ");
   student.Delete(new Student("name1",1));
   System.out.println();
   
   System.out.println("Re-searching for  \"name1\": ");
   student.Search(new Student("name1",1));
   System.out.println();
  
 }
}
Read More

Reversing and sorting an array in Java

Problem:

Write a program that reads 10 values of integers from the user store them in an array of integers.
a. Then print the reverse of this array.
Example:

1 6 3 4 2 9 8 10 5 7

Reverse: 7 5 10 8 9 2 4 3 6 1
b.The program writes the array in increasing order. (Using for loops).

Increasing order: 1 2 3 4 5 6 7 8 9 10

Output:

Not applicable.

Solution:

import java.util.Scanner;

public class Prob2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter " + 10 + " numbers: ");
int[] list = new int[10];

for(int i=0; i<10; i++)
{
list[i] = scan.nextInt();
}

System.out.println("The array in reverse is: ");
for(int i=9; i>=0; i--)
{
System.out.print(list[i] + " ");
}

System.out.println();
System.out.println("The array in increassing order is: ");
for(int i=0;i<list.length;i++)
{
int temp = 0;
for(int j=0;j<list.length-1;j++)
{
if(list[j]>list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
for(int i=0; i<list.length; i++)
{
System.out.print(list[i] + " ");
}


}



}
Read More

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact