LinkedLists


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: LinkedLists

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    LinkedLists

    Hi all

    Got a quick problem that shouldnt be to hard but im having some major problems getting my head around it!

    I need to create a linked list from scratch...ive got everything working apart from the sort method...im using a straight selection sort (or bubble sort) but i just cant get it working...

    The current setup is 2 classes, Node & Linkedlist

    The node class has a compareTo method but I just cant get the loops right

    Anyone got an example?This is the code I have at the moment and it doesnt work...
    while(temp1 != null){
    Node temp2 = temp1.getNext();
    while(temp2 != null){
    if(temp1.compareTo(temp2) == 1){
    temp3.setNext(temp1);
    temp1.setNext(temp2.getNext());
    temp2.setNext(temp1);
    }
    temp2 = temp1.getNext();
    }
    temp1 = temp1.getNext();
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I can't figure out where temp3 comes from.... As for the compareTo() method I'm
    puzzled, - I've always thought that this method should return 0 for equality,
    > 0 if o1 > o2 and < 0 if o1 < o2.

    Anyway, here are the bare bones of a bubblesort:

    Code:
    public class BubbleSort {
      public final static int ASCENDING=0;
      public final static int DESCENDING=1;
      private int [] values;
      private int sortDirection=ASCENDING;
      public BubbleSort(int [] values, int sortDirection) {
        this.values=values;
        this.sortDirection=sortDirection;
      }
      public void sort() {
        int last=values.length;
        boolean wasSwapped;
        for (int i=values.length-1;i>=0; i--) {
          wasSwapped=false;
          for (int j=0;j<i;j++) {
            if (swap(j)) wasSwapped=true;
          }
          /**
           * if a scan completes without any swapping the array is sorted
           */
          if (!wasSwapped) break;
        }
      }
    
      private boolean swap(int pos) {
        if ( (this.sortDirection==ASCENDING && values[pos] > values[pos+1]) ||
             (this.sortDirection==DESCENDING && values[pos] < values[pos+1])) {
        //if (values[pos] > values[pos+1]) {
          int copy=values[pos];
          values[pos]=values[pos+1];
          values[pos+1]=copy;
          return true;
        } else {
          return false;
        }
      }
      public static void main(String[] args) {
        int [] v=new int[] {
            2,4,3,5,4,6,7,5,3,1,33,44,22
        };
        BubbleSort bs=null;
        System.out.println("Unsorted:");
        listValues(v);
        bs = new BubbleSort(v, BubbleSort.DESCENDING);
        bs.sort();
        System.out.println("Sorted Decending:");
        listValues(v);
        bs = new BubbleSort(v, BubbleSort.ASCENDING);
        bs.sort();
        System.out.println("Sorted Ascending:");
        listValues(v);
      }
      static void listValues(int [] v) {
        for (int i=0;i<v.length;i++) {
          System.out.println(v[i]);
        }
    
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center