Queue LinkedList pada Java

Rabu, 07 Desember 2011
/**
 * @author Ilham Yuliantoro 105090600111029
 */
import java.util.Random;
import java.util.Scanner;
class Link{
public Object Data;
public Link next;
public Link(Object d){
Data=d;
}  
public void display(){
        System.out.print("("+Data+")");
    }
}


class LinkList{
public static Link first;
public static Link last;
public LinkList(){
first=null;
last=null;
}
public boolean IsEmpty(){
return (first==null);
}
public void Insert(Object d){
Link baru=new Link(d);
if (IsEmpty())
first=baru;
else
last.next=baru;
last=baru;
}
public Object Remove(){
Object temp=first.Data;
if (first.next==null)
last=null;
first=first.next;
return temp;
}
public Object peek(){
if(first!=null)
System.out.println ("First Data (Peek):"+first.Data);
else{
System.out.println ("Data Kosong");
} return null;
}
  public int size() {
  int size=0;
  for (Link j=first;j!=last;j=j.next)
  size++;
        return size+1;
    }
public void displayList(){
Link current=first;
while(current!=null){
current.display();
current=current.next;
}
System.out.println ("");
}
}

class LinkQueue{
private LinkList TheList;
public LinkQueue(){
TheList=new LinkList();
}
public boolean isEmpty(){
return TheList.IsEmpty();
}
public void  push(int i){
TheList.Insert(i);
}
public Object pop(){
return TheList.Remove();
}
public Object peek(){
return TheList.peek();
}
public int size(){
return TheList.size();
}
public void displayQueue(){
System.out.print("First-->Last:");
TheList.displayList();
}
}

public class Queue_Ilham {
public static void main(String[] args){
Scanner input=new Scanner(System.in); 
LinkQueue theQueue = new LinkQueue();
System.out.println ("Inputkan Bilangan :");
theQueue.push(input.nextInt());
theQueue.push(input.nextInt());
theQueue.push(input.nextInt());
theQueue.push(input.nextInt());
theQueue.push(input.nextInt());
theQueue.displayQueue();
theQueue.peek();
System.out.println ("Size:"+theQueue.size());
theQueue.pop();
theQueue.pop();
System.out.println ("");
System.out.println ("After Pop");
theQueue.displayQueue();
}    
}

3 komentar:

  1. Anonim mengatakan...:

    masih di UB kah Kak?

  1. Unknown mengatakan...:

    maaf kak , saya dr TI angkatan 2013. . kak mau tanya donk. . program buat queue double lingked list yg supermarket dan teller itu gmn ya ?

Posting Komentar