Saturday, October 4, 2014

Doubly Linked List

 hai hai para blogger lama nih gag ngeposting sesuatu mumpung lagi ada mood buat nulih yah udah sempatin deh. kali ini aq bakalan posting hasil kerjaan ku yang mungkin buat orang yang udah mahir gag butuh tapi buat aq pribadi ini sangat bagus dan sangat berguna buat teman teman ketahui. ok langsung aja yah. jadi kali ini aq mau posting tentang source code dari struktur data dasar yaitu link list. tapi kali ini yang akan di posting bukan link list biasa melainkan dubly linked list.... yaps karna udah terlalu banyak basa basi langsung aja lah...


pertama kita buat kelas linknya dulu


public class Link 
{
    public long dData;                 // data item
    public Link next;                  // next link in list
    public Link previous;              // previous link in list
// -------------------------------------------------------------
    public Link(long d)                // constructor
    { dData = d; }
// -------------------------------------------------------------
    public void displayLink()          // display this link
    { System.out.print(dData + " "); }
// -------------------------------------------------------------    
}


setelah membuat kelas linknya kita buat  kelas untuk menampung proses link list

public class DoublyLinkedList 
{
    private Link first;               // ref to first item
    private Link last;                // ref to last item
// -------------------------------------------------------------
    public DoublyLinkedList()         // constructor
    {
        first = null;                  // no items on list yet
        last = null;
    }
// -------------------------------------------------------------
    public boolean isEmpty()          // true if no links
    { return first==null; }
// -------------------------------------------------------------
    public void insertFirst(long dd)  // insert at front of list
    {
        Link newLink = new Link(dd);   // make new link
        
        if( isEmpty() )                // if empty list,
            last = newLink;             // newLink <-- --="" else="" first.previous="newLink;" first="" last="" newlink.next="first;" newlink="" old=""> old first
        first = newLink;               // first --> newLink
    }
// -------------------------------------------------------------
    public void insertLast(long dd)   // insert at end of list
    {
        Link newLink = new Link(dd);   // make new link
        if( isEmpty() )                // if empty list,
            first = newLink;            // first --> newLink
        else
        {
            last.next = newLink;        // old last --> newLink
            newLink.previous = last;    // old last <-- -------------------------------------------------------------="" --="" assumes="" delete="" deletefirst="" else="" first.next.previous="null;" first.next="=" first="" if="" item="" last="" link="" list="" newlink="" next="" non-empty="" null="" old="" one="" only="" public="" temp="first;"> old next
        return temp;
    }
// -------------------------------------------------------------
    public Link deleteLast()          // delete last link
    {                              // (assumes non-empty list)
        Link temp = last;
        if(first.next == null)         // if only one item
            first = null;               // first --> null
        else
            last.previous.next = null;  // old previous --> null
        last = last.previous;          // old previous <-- -------------------------------------------------------------="" --="" after="" assumes="" at="" beginning="" boolean="" current.ddata="" current="=last)" dd="" didn="" false="" find="" found="" if="" insert="" insertafter="" is="" it="" just="" key="" last="" link="" list="" long="" make="" match="" move="" new="" newlink.next="null;" newlink="" next="" non-empty="" null="" public="" return="" start="" t="" temp="" to="" until="" while=""> null
            last = newLink;             // newLink <-- --="" else="" last="" link="" newlink.next="current.next;" newlink="" not=""> old next
            // newLink <-- --="" current.next.previous="newLink;" current.next="newLink;" current="" newlink.previous="current;" newlink="" next="" old=""> newLink
        return true;                   // found it, did insertion
    }
// -------------------------------------------------------------
    public boolean insertBefore(long key, long dd)
    {                              // (assumes non-empty list)
        Link current = first;          // start at beginning
        while(current.dData != key)    // until match is found,
        {
            current = current.next;     // move to next link
            if(current == null)
                return false;            // didn't find it
        }
        Link newLink = new Link(dd);   // make new link
        
        if(current==first)              // if last link,
        {
            newLink.previous = null;        // newLink --> null
            first = newLink;             // newLink <-- --="" else="" last="" link="" newlink.previous="current.previous;" newlink="" not=""> old next
            // newLink <-- --="" current.previous.next="newLink;" current.previous="newLink;" current="" newlink.next="current;" newlink="" next="" old=""> newLink
        return true;                   // found it, did insertion
    }
// -------------------------------------------------------------
    public Link deleteKey(long key)   // delete item w/ given key
    {                              // (assumes non-empty list)
        Link current = first;          // start at beginning
        while(current.dData != key)    // until match is found,
        {
            current = current.next;     // move to next link
            if(current == null)
                return null;             // didn't find it
        }
        if(current==first)             // found it; first item?
            first = current.next;       // first --> old next
        else                           // not first
            // old previous --> old next
            current.previous.next = current.next;
        
        if(current==last)              // last item?
            last = current.previous;    // old previous <-- -------------------------------------------------------------="" current.next.previous="current.previous;" current="" displayforward="" else="" first--="" ist="" last="" next="" not="" old="" previous="" public="" return="" system.out.print="" value="" void="">last): ");
        Link current = first;          // start at beginning
        while(current != null)         // until end of list,
        {
            current.displayLink();      // display data
            current = current.next;     // move to next link
        }
        System.out.println("");
    }
// -------------------------------------------------------------
    public void displayBackward()
    {
        System.out.print("List (last-->first): ");
        Link current = last;           // start at end
        while(current != null)         // until start of list,
        {
            current.displayLink();      // display data
            current = current.previous; // move to previous link
        }
        System.out.println("");
    }
// -------------------------------------------------------------
}  // end class DoublyLinkedList    



nah yang terakhir kita tinggal membuat kelas aplikasinya

public class DoublyLinkedApp 
{
    private Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {                             // make a new list
        DoublyLinkedList theList = new DoublyLinkedList();
        Scanner input = new Scanner(System.in);
        
        int key, dd;

        System.out.println("1. Insert First");
        System.out.println("2. Insert Last");
        System.out.println("3. Insert After");
        System.out.println("4. Insert Before");
        System.out.println("5. Display");
        
        System.out.print("Pilih Inputan: ");
        int a = input.nextInt();
        while (a <= 5) {

            switch (a) {
                case 1:
                    theList.displayForward();
                    input.nextLine();
                    System.out.print("Masukkan Nilai: ");
                    dd = input.nextInt();
                    theList.insertFirst(dd);
                    break;
                case 2:
                    theList.displayForward();
                    input.nextLine();
                    System.out.print("Masukkan Nilai: ");
                    dd = input.nextInt();
                    theList.insertLast(dd);
                    break;
                case 3:
                    theList.displayForward();
                    input.nextLine();
                    System.out.print("Masukkan Nilai: ");
                    key = input.nextInt();
                    System.out.print("Masukkan Nilai: ");
                    dd = input.nextInt();
                    theList.insertAfter(key,dd);
                    break;
                case 4:
                    theList.displayForward();
                    input.nextLine();
                    System.out.print("Masukkan Nilai: ");
                    key = input.nextInt();
                    System.out.print("Masukkan Nilai: ");
                    dd = input.nextInt();
                    theList.insertBefore(key,dd);
                    break;
                default:
                    System.out.println("Salah Inputan");
                    
            }
            System.out.println("1. Insert First");
            System.out.println("2. Insert Last");
            System.out.println("3. Insert After");
            System.out.println("4. Insert Before");
            System.out.println("5. Display");
            
            System.out.print("Pilih Inputan: ");
            a = input.nextInt();
        
        }
        
        
        theList.displayForward();     // display list forward
    }  // end main()    
}



nah itu dia salah satu program dasar yang mungkin berhasil aku buat walau liat panduan juga sih tapi lumayan lah buat tambah tambah pengalaman ngoding

oh iya program yang aq buat ini terpisah kelasnya tapi kalau mau disatuka bisa juga kok. asal aturan programnya di ikutin dengan benar yah ok sekian dari aq buat hari ini yah sampai ketemu di postingan postingan lainnya yah.....

1 comment:

  1. I just found tons of free Steam games at http://getsteamgifts.com/ 10877 people already got their gift cards!

    ReplyDelete