INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

LinkedList

How to sort a LinkedList by FredrikN
Posted: 18 Apr 03

This is an example how you can sort your LinkedList.

I'm using CarNodes and sort it by the year of the car and then write the sorted list to the console

The unsorted output will look like

VW VR6 170 1997
Volvo 850 170 1992
BMW M3  286 1993
Porsche 911 284 1992
Porsche 911 312 1994


and after calling the sorted method

VW VR6 170 1997
Porsche 911 312 1994
BMW M3  286 1993
Volvo 850 170 1992
Porsche 911 284 1992


Any questions can be emailed to fredrick.n@thegate.nu
The source code


-----------------------------------------------------------
public class Driver
{

    public static void main(String[] arg)
    {
    
        LinkedList list = new LinkedList();
        
        list.addCar("Porsche 911",312,1994);
        list.addCar("Porsche 911",284,1992);
        list.addCar("BMW M3 ",286,1993);
        list.addCar("Volvo 850",170,1992);
        list.addCar("VW VR6",170,1997);
        
        //Method sort the list after year the car was made
        list.sortList();
        
        //Method to print all objects in List
        System.out.println(list.viewAll());
            
    }
}

-----------------------------------------------------------
import java.util.*;
public class LinkedList
{
    
    private CarNode head = null;
    
    public void addCar(String name , int hk , int year)
    {    
        //If head = null then create the first node
        if(head == null)
        {
            head = new CarNode(name,hk,year,null);
        }
        else
        {
            //If there are more than 1 node
            head = new CarNode(name,hk,year,head);            
        }
                
    }


    public void sortList()
    {
        
        boolean sorted = false;
        
        while(!sorted)
        {
            
            sorted = true;
            
            for(CarNode cursor = head ; cursor.getNext() != null ; cursor = cursor.getNext())
            {
                if(cursor.getYear() < cursor.getNext().getYear())
                {
                    String n = cursor.getName();
                    int y = cursor.getYear();
                    int hk = cursor.getHk();
                    
                    cursor.setName(cursor.getNext().getName());
                    cursor.setYear(cursor.getNext().getYear());
                    cursor.setHk(cursor.getNext().getHk());
                    
                    cursor.getNext().setName(n);
                    cursor.getNext().setYear(y);
                    cursor.getNext().setHk(hk);
                                                        
                    sorted = false;        
                }
                    
            
            }
                    
            
        }
        
        
        
        
    }
    

    public String viewAll()
    {
            
        StringBuffer str = new StringBuffer();    
                
        for(CarNode cursor = head ; cursor != null ; cursor = cursor.getNext())
        {
            //Appending car by car until there are no more cars
            str.append(cursor+"\n");
        }
        return new String(str);
        
    }

    
}
-----------------------------------------------------------
public class CarNode
{
    private String name;
    private int hk;
    private int year;
    private CarNode next;
    
    public CarNode(String name,int hk,int year,CarNode next)
    {
        this.name = name;
        this.hk = hk;
        this.year = year;
        this.next = next;
            
    }



    public CarNode getNext()
    {
        return next;
    }
    
    public String getName()
    {
        return name;
    }
    
    public int getHk()
    {
        return hk;
    }
    
    public int getYear()
    {
        return year;
    }
    
    
    public void setName(String in)
    {
        name = in;
    }
    
    public void setHk(int in)
    {
        hk = in;
    }
    
    public void setYear(int in)
    {
        year = in;
    }
    
    
    public String toString()
    {
        return name + " " + hk + " " + year;
    }
    
    
}
-----------------------------------------------------------


 

Back to Java FAQ Index
Back to Java Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close