Results 1 to 1 of 1
-
How to create a Sorted List in Java
Java Code:public class SortedList { private Link first; public SortedList() { first = null; } public boolean isEmpty(){ return (first == null); } public void insert(long key){ Link newLink = new Link(key); Link previous = null; Link current = first; while (current != null && key > current.data) { previous = current; current = current.next; } if (previous == null) first = newLink; else previous.next = newLink; newLink.next = current; } public Link remove() { Link temp = first; first = first.next; return temp; } public void displayList() { System.out.print("List: "); Link current = first; while (current != null){ current.displayLink(); current = current.next; } System.out.println(""); } public static void main(String[] args) { SortedList theSortedList = new SortedList(); theSortedList.insert(20); theSortedList.insert(40); theSortedList.displayList(); theSortedList.insert(10); theSortedList.insert(30); theSortedList.insert(50); theSortedList.displayList(); theSortedList.remove(); theSortedList.displayList(); } class Link { public long data; public Link next; public Link(long dd) { data = dd; } public void displayLink() { System.out.print(data + " "); } } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM -
How to Search a List in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:38 PM -
Sorting, Searching, and Inserting into a sorted array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:39 PM -
My doublyLinked list does not get sorted
By hasani6leap in forum New To JavaReplies: 0Last Post: 01-06-2008, 03:09 PM -
can java.io.File create a list of all files and folders.
By MattStone in forum New To JavaReplies: 20Last Post: 12-17-2007, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks