Sunday 1 June 2014

contoh program java SINGEL LINKED LIST

package linkd;

/**
 *
 * @author IZAN
 */
public class SingelLinkedList {
    private int data;
    private SingelLinkedList next;
    public SingelLinkedList(){
        data = 0;
        next = null;
    }
    public SingelLinkedList(int value){
        data = value;
        next = null;
    }
    public SingelLinkedList InsertNext(int value){
        SingelLinkedList node = new SingelLinkedList(value);
        if(this.next == null){
            node.next = null;
            this.next = node;
        }
        else{
            SingelLinkedList temp = this.next;
            node.next = temp;
            this.next = node;
        }
        return node;
    }
    public int DeleteNext(){
        if(next == null)
            return 0;
        SingelLinkedList node = this.next;
        this.next = this.next.next;
        node = null;
        return 1;
    }
    public void Traverse(SingelLinkedList node){
        if(node == null)
            node = this;
        System.out.println("Traversing in Forward Direction");
        while(node != null)
        {
            System.out.println(node.data);
            node = node.next;
        }
    }
    public static void main(String[] args){
       SingelLinkedList node1 = new SingelLinkedList(1);
       SingelLinkedList node2 = node1.InsertNext(2);
       SingelLinkedList node3 = node2.InsertNext(3);
       SingelLinkedList node4 = node3.InsertNext(4);
       SingelLinkedList node5 = node4.InsertNext(5);
        node1.Traverse(null);
        node3.DeleteNext();
        node2.Traverse(null);
    }
}

contoh sorcod CIRCULAR QUEUE

package linkd;
import java.util.Scanner;

/**
 *
 * @author IZAN
 */
public class circuler {
    public static void main(String args[])
    {
        Scanner sc = new Scanner (System.in);
        System.out.print("Berapa data yang akan dimasukkan:");
        int max = sc.nextInt();
        QueueLinkList queue = new QueueLinkList(max);
        int pilihan=0;
     
        while(pilihan != 3)
        {
            System.out.println("Menu");
            System.out.print("1.insert\n2.remove\n3.exit\npilihan Anda:");
            pilihan = sc.nextInt();
            switch (pilihan)
            {
                case 1 : System.out.println("insert queue:");
                        System.out.print("Masukkan data:");
                        int temp;
                        temp = sc.nextInt();
                        queue.enqueue(temp);
                        queue.display();
                break;
                case 2 : System.out.println("remove queue:");
                        queue.dequeue();
                        queue.display();
                break;
                case 3 : System.out.println("exit");
            }
        }
    }
}

  class item        //atau node/simpul
             {
                public int data;
                public item next; // next node link in list
                public item prev; // previous node link in list
                public item(int id) // constructor
            {
                                data = id; // initialize data
               }
                public void displayLink() // display ourself
                {
                      System.out.print("{" + data + "} ");
                }
            }
 class QueueLinkList
            {
                private item front; // ref to first link on list
                private item rear; // ref to last link on list
             int jumlah;
             int max;
                public QueueLinkList(int m ) // constructor
                {
              max = m;
              jumlah = 0;
              front = rear = null; // no items on list yet
                }
                public boolean isEmpty() // true if list is empty
                {
              return (front==null);
                }
                public void enqueue(int id)  //node baru selalu di top // void hanya sesuai prosedur hanya langkah2                 dan untuk dirinya sendiri
                {
               item newitem = new item(id);
               if (jumlah < max)
              {
                             
              if (front == null)    // the first node created
          {
               front = rear = newitem; // first --> newLink
                       newitem.next = front;
                       newitem.prev = front;
                                }
            else        // the second node and the next node
                {
                        rear.next = newitem;     //next dr top (awal) diarahkan ke node baru
                        newitem.prev = rear;     //prev dr node baru diarahkan ke tail (awal)
                        newitem.next = front;   //buat circular
                        front.prev = newitem;
                        rear = newitem;        //top (baru) diarahkan ke node baru
                                }
                jumlah++;
                }
                else
               {
                System.out.println("Kapasitas penuh. Terjadi pergesaran item");
                front=front.next;
                front.prev=newitem;
                newitem.next=front;
                rear.next=newitem;
                newitem.prev=rear;
                rear=newitem;
                }
                }
                public item dequeue() // delete first item
                {          
                    item temp = null;
                    if (front == null)                // queue is empty
                   System.out.println("Queue is empty");
                 else if (front == rear)      // queue is only one data
          {
                         temp = front;
                     front = rear = null;
                        jumlah--;
                                }
                                else        // queue has more than one data
                                {
                   temp = front; // save reference to link
                front = front.next; // delete it: first-->old next
                        rear.next = front;
                        front.prev = rear;
                        jumlah--;
                    }
                   return temp;
                }
                public void display()
                {
               item current = front; // start from the first data
               if (current == null)    //queue kosong
                     System.out.println("The Queue is empty");
                else if (current.next == front) //node hanya 1
                {
                    current.displayLink();
                }
                else //if (current.next != front)
                {
                    for(;current.next != front;) // until end of list,
                                                {
                 current.displayLink(); // print data
                 current = current.next; // move to next link
                                                }
                    current.displayLink();
                }
               System.out.println("");
                }
                } // end class LinkList