Kamis, 12 November 2015

Ordered Double Link List dalam Java

Seperti yang sudah saya bahas pada postingan sebelumnya http://shemwidodo.blogspot.co.id/2015/09/linked-list-dengan-menggunakan-bahasa.html.
Double Link List adalah link list yang mempunyai dua link umumnya disebut next dan prev.
Kali ini saya mencoba membuat program sederhana untuk mengurutkan inputan user pada saat ditampilkan menggunakan double link list dengan bahasa pemrograman Java.

Berikut ini adalah source code ordered double link list
class node{
    int data;
    node next,prev;
}
public class Algoritma {
static node head,tail;
static void insert(int x){
        node new_node=new node();
        new_node.data=x;
        if(head == null && tail == null){ // linked list kosong
            head = tail=new_node;
        }else {
            tail.next=new_node;
            new_node.prev=tail;
            tail=new_node;
        }
    }
static void view()
{
    node x =new node();
        while(x!=null)
        {
            x=head;
            while(x!=null)
            {
                node next=x.next;
                node doublenext=x.next.next;
                if (x.data>next.data) {
                    next.prev=x.next=null;
                    next.next=x;
                    x.prev=next;
                    x.next=doublenext;
                    doublenext.prev=x;
                }
                x=x.next;
            }
        }
        x=head;
        while(x != null){
            System.out.print(x.data + " - ");
            x = x.next;
        }
        System.out.println();
}
    public static void main(String[] args) {
        insert(2);
        insert(1);
        insert(0);
        view();
    }
}

1 komentar:

Anonim mengatakan...

Slots - Casino - JTG Hub
Slots Casino - Get free $50 bonus 포항 출장마사지 + $20 deposit bonus. Play 창원 출장마사지 slots 춘천 출장마사지 online for 포항 출장안마 free 목포 출장안마 and win real money! Join JTG today.