saban123

priority queue

Dec 12th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Node{
  3.     int data;
  4.     Node next;
  5.     Node prev;
  6.     Node(int nilai){
  7.         data=nilai;
  8.     }
  9. }
  10.  
  11. class priorityQueue{
  12.     Node head;
  13.     Node tail;
  14.     int size=0;
  15.    
  16.     void push(int value){
  17.         if(head==null){
  18.             head=tail=new Node(value);
  19.             size++;
  20.         }
  21.         else{
  22.             Node baru=new Node(value);
  23.             if(baru.data<head.data){
  24.                 baru.next=head;
  25.                 head.prev=baru;
  26.                 head=head.prev;
  27.             }
  28.             else if(baru.data>tail.data){
  29.                 tail.next=baru;
  30.                 baru.prev=tail;
  31.                 tail=tail.next;
  32.                 size++;
  33.             }
  34.             else{
  35.                 Node temp=head;
  36.                 while(temp!=null){
  37.                     if(baru.data<temp.data){
  38.                         baru.next=temp;
  39.                         baru.prev=temp.prev;
  40.                         temp.prev.next=baru;
  41.                         temp.prev=baru;
  42.                         size++;
  43.                         break;
  44.                     }
  45.                     temp=temp.next;
  46.                 }
  47.             }
  48.             size++;
  49.         }
  50.     }
  51.    
  52.     void pop(){
  53.         head=head.next;
  54.         head.prev=null;
  55.         size--;
  56.     }
  57.    
  58.     void tampil(){
  59.         Node temp=head;
  60.         while(temp!=null){
  61.             System.out.print(" "+temp.data);
  62.             temp=temp.next;
  63.         }
  64.         System.out.println();
  65.     }
  66.    
  67.     public static void main(String[] args){
  68.         priorityQueue a =new priorityQueue();
  69.         Scanner x=new Scanner (System.in);
  70.         System.out.print("masukkan jumlah maksimum antriaan : ");
  71.         int max = x.nextInt();
  72.         int banyak=0;
  73.         while (banyak<max){
  74.         System.out.print("masukkan data : ");
  75.         int pus = x.nextInt();
  76.         a.push(pus);
  77.         banyak=banyak+1;
  78.         }
  79.        
  80.         System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-1 :");
  81.         a.tampil();
  82.         a.pop();
  83.         System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-2 :");
  84.         a.tampil();
  85.         a.pop();
  86.         System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-3 :");
  87.         a.tampil();
  88.         a.pop();
  89.         System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-4 :");
  90.         a.tampil();
  91.         a.pop();
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment