Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Node{
- int data;
- Node next;
- Node prev;
- Node(int nilai){
- data=nilai;
- }
- }
- class priorityQueue{
- Node head;
- Node tail;
- int size=0;
- void push(int value){
- if(head==null){
- head=tail=new Node(value);
- size++;
- }
- else{
- Node baru=new Node(value);
- if(baru.data<head.data){
- baru.next=head;
- head.prev=baru;
- head=head.prev;
- }
- else if(baru.data>tail.data){
- tail.next=baru;
- baru.prev=tail;
- tail=tail.next;
- size++;
- }
- else{
- Node temp=head;
- while(temp!=null){
- if(baru.data<temp.data){
- baru.next=temp;
- baru.prev=temp.prev;
- temp.prev.next=baru;
- temp.prev=baru;
- size++;
- break;
- }
- temp=temp.next;
- }
- }
- size++;
- }
- }
- void pop(){
- head=head.next;
- head.prev=null;
- size--;
- }
- void tampil(){
- Node temp=head;
- while(temp!=null){
- System.out.print(" "+temp.data);
- temp=temp.next;
- }
- System.out.println();
- }
- public static void main(String[] args){
- priorityQueue a =new priorityQueue();
- Scanner x=new Scanner (System.in);
- System.out.print("masukkan jumlah maksimum antriaan : ");
- int max = x.nextInt();
- int banyak=0;
- while (banyak<max){
- System.out.print("masukkan data : ");
- int pus = x.nextInt();
- a.push(pus);
- banyak=banyak+1;
- }
- System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-1 :");
- a.tampil();
- a.pop();
- System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-2 :");
- a.tampil();
- a.pop();
- System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-3 :");
- a.tampil();
- a.pop();
- System.out.print(" BERIKUT ADALAH SEBUAH ANTTRIAN KE-4 :");
- a.tampil();
- a.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment