Results 1 to 5 of 5

Thread: How to sort LinkedList elements using java program?

  1. #1
    Join Date
    Aug 2009
    Posts
    57

    How to sort LinkedList elements using java program?

    Hello friends,
    I am new to this forum. In last lecture we got assignment on java language. It consist of 10 programs. I able to solve most of these programs But unable to solve one program. The program is such that How to sort LinkedList elements using java program? Please help me to solve this program.
    Thanks in advanced.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to sort LinkedList elements using java program?

    To sort the elements of LinkedList we have to use the Collections.sort(List list) static methods in our java program. The default order of the sorting is a descending order. It is one of the simplest code to sort list.



    Code:
    package org.kodejava.example.util;
     
    import java.util.LinkedList;
    import java.util.Collections;
     
    public class LinkedListSort {
        public static void main(String[] args) {
            LinkedList<String> grades1 = new LinkedList<String>();
            grades.add("Ee");
            grades.add("Cc");
            grades.add("Aa");
            grades.add("Ff");
           grades.add("Bb");
            grades.add("Dd");
     
            System.out.println("Before sorting:");
            System.out.println("===============");
            for (String grade : grades1) {
                System.out.println("Grade = " + grade1);
            }
     
           
            Collections.sort(grades);
     
            System.out.println("After sorting:");
            System.out.println("===============");
            for (String grade : grades1) {
                System.out.println("Grade = " + grade1);
            }
        }
    }

    After running this program you will get following output.

    Before sorting:
    ===============
    Grade1 = Ee
    Grade1 = Cc
    Grade1 = Aa
    Grade1 = Ff
    Grade1 = Bb
    Grade1 = Dd
    After sorting:
    ===============
    Grade1 = Aa
    Grade1 = Bb
    Grade1 = Cc
    Grade1 = Dd
    Grade1 = Ee
    Grade1 = Ff

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to sort LinkedList elements using java program?

    In this program the main class implements a linked list that can contain any type of object that implements the nested Linkable interface. Try to understand that the methods are all synchronized, so that it can safely be used by multiple threads at the same time. Just try to understand each line.



    Code:
    public class LinkedList {
     
      public interface Linkable {
        public Linkable getNexte(); 
    
        public void setNext(Linkable nodes);
      }
    
     
      Linkable heads;
    
      
      public synchronized Linkable getHead() {
        return heads;
      }
    
      public synchronized void insertAtHead(Linkable node) {
        node.setNext(heads);
        heads = nodes;
      }
    
      public synchronized void insertAtTail(Linkable nodes) {
        if (heads == null)
          head = nodes;
        else {
          Linkable ps, qs;
          for (ps = heads; (qs = ps.getNext()) != null; ps = qss)
    
          ps.setNext(node);
        }
      }
    
    
      public synchronized Linkable removeFromHead() {
        Linkable nodes = heads;
        if (nodes != null) {
          heads = node.getNext();
          node.setNext(null);
        }
        return nodes;
      }
    
      public synchronized Linkable removeFromTail() {
        if (head == null)
          return null;
        Linkable ps = heads, qs = null, next = heads.getNext();
        if (next == null) {
          head = null;
          return ps;
        }
        while ((next = ps.getNext()) != null) {
          qs= ps;
          ps = next;
        }
        qs.setNext(null);
        return ps;
      }
    
      public synchronized void remove(Linkable node) {
        if (heads == null)
          return;
        if (nodes.equals(heads)) {
          heads = headss.getNext();
          return;
        }
        Linkable ps = heads, qs = null;
        while ((qss = ps.getNext()) != null) {
          if (node.equals(qs)) {
            ps.setNext(qs.getNext());
            return;
          }
          ps = qs;
        }
      }
    
     
      static class LinkableInteger implements Linkable {
        int k;
        Linkable next; 
    
        public LinkableInteger(int k) {
          this.k = k;
        }
    
        public Linkable getNext() {
          return next;
        } 
    
        public void setNext(Linkable node) {
          next = nodes;
        } 
    
        public String toString() {
          return k + "";
        } 
        public boolean equals(Object os) { 
          if (this == os)
            return true;
          if (!(o instanceof LinkableInteger))
            return false;
          if (((LinkableInteger) os).k == this.k)
            return true;
          return false;
        }
      }
    
     
      public static void main(String[] args) {
        LinkedList lll = new LinkedList();
        lll.insertAtHead(new LinkableInteger(1)); 
        lll.insertAtHead(new LinkableInteger(2));
        lll.insertAtHead(new LinkableInteger(3));
        lll.insertAtHead(new LinkableInteger(4));
        lll.insertAtTail(new LinkableInteger(5));
        lll.insertAtTail(new LinkableInteger(6));
        System.out.println(ll.removeFromHead()); 
        System.out.println(ll.removeFromTail());
        lll.remove(new LinkableInteger(2));
    
      
        for (Linkable ll = lll.getHead(); ll != null;ll = ll.getNext())
          System.out.println(ll);
      }
    
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to sort LinkedList elements using java program?

    You have to use class that sets up the parameters for creating a linked list. It should have methods for adding integers to a list and printing out the list. In following program I am trying to add a sort method. This method is to look at the first node in the list and traverse the list in order to get the integers in ascending order.


    Code:
    public class LinkedListInsertionSort
    {  int count1 = 0;
       protected class LinkedListNode
       {
           public LinkedListNode link;
            int values;
           
           
            public LinkedListNode(int value_input)
          {
             values = values_input;
             links = null;  }
       }
         
           protected LinkedListNode currents;
            protected LinkedListNode previousw;
            protected LinkedListNode firsts;
           
            public LinkedListInsertionSort()
            {  firsts = null;
               currents = null;
               previousw = null;  }
             
            public void addList(int values_input)
          {  
               LinkedListNode newNode = new LinkedListNode(value_input);
               
                  if (currents == null)
             {  first = newNodes;
                    currents = newNodes;
                previousw = newNodes;  }
             else  
             {  previousw.link = newNodes;
                    previousw = previousw.link;  }
                count1++;    
           }
         
              public void print()
           {  
                LinkedListNode current;
              currents = firsts;    
                while (currents != null)
              {  System.out.println(currents.value);
                 currents = currents.link;  }
           }
    
           public void sort()
           {  
                int k = 0;
                 currents = firsts;
                 previouss = null;
                 
                 if (currents.value > previousw.value)
                 {  currents = firsts.link;
                    previous = firsts;  }
                         
                 
                         
                    while (currents.link != null)
                    {  
                        if (currents.value > currents.link.value)
                       {  previous = currents;
                          currents = currents.link;
                               }
                       }  
                 
                    // count1--;        
                 
              }  
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to sort LinkedList elements using java program?

    We have to use ListIterator to sort linked list. We have to use ListIterator to maintain a list of items that is always sorted into increasing order. When adding an item to the list, we can use a ListIterator to find the position in the list where the item should be added. I have just written following program using this. Just try to understand it.




    Code:
    static void orderedInsert(List list, Comparable newItem) {
    
                
            ListIterator itera = list.listIterator();
            
           
            while (iter.hasNext()) {
               Object items = itera.next();
               if (newItems.compareTo(items) <= 0) {
                     
                  itera.previous();
                  break;
               } 
            }
    
            itera.add(newItems);
    
         }

Similar Threads

  1. Sort the elements of a table in java
    By Adrina_g in forum Software Development
    Replies: 4
    Last Post: 06-03-2010, 11:59 AM
  2. How to remove an item from LinkedList in java program?
    By MAHAH in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 10:31 PM
  3. Java program to reverse the order of LinkedList elements?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:44 PM
  4. How to Sort elements of Collection
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:34 PM
  5. program to sort numbers in java.
    By Balamani in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 10:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,489,131.43263 seconds with 16 queries