saban123

PROGRAM STACK AND QUEUE

Dec 10th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. public class node
  2. {   node next;
  3.     int data;
  4.     node prev;
  5.     public node(int data)
  6.     {
  7.         this.data=data;
  8.         next=null;
  9.     }
  10. }
  11.  
  12.  
  13.  
  14. import java.util.Scanner;
  15. public class jurnal1{
  16.     int b,max;
  17.     node head,tail;
  18.    
  19.     public jurnal1(){
  20.     head=tail=null;
  21.     b=0;}
  22.    
  23.     public void max(int x){
  24.     max=x;}
  25.    
  26.     public void enqueue(int x){
  27.         node baru =new node (x);
  28.         if (head==null){
  29.             head=baru;
  30.             tail=baru;
  31.             b=b+1;
  32.         }
  33.         else if(b<max){
  34.             tail.next=baru;
  35.             baru.prev=tail;
  36.             tail=baru;
  37.             b=b+1;}
  38.         else {
  39.             System.out.print("full");}
  40.     }
  41.    
  42.     public void pop(){
  43.         node temp=tail;
  44.         tail=tail.prev;
  45.         tail.next=null;
  46.         temp=null;
  47.         b=b-1;
  48.     }  
  49.        
  50.     public void dequeue(){
  51.         node temp=head;
  52.         head=head.next;
  53.         temp=null;
  54.         b=b-1;
  55.     }
  56.     public void tampil(){
  57.         node temp=head;     if (head==null){
  58.         System.out.print("kosong");}
  59.         else{
  60.             while (temp!=null){
  61.             System.out.println("["+temp.data+"]");
  62.             temp=temp.next;
  63.             }
  64.         }
  65.     }
  66.     public void tampil1(){
  67.         node temp=tail;
  68.         if (head==null){
  69.         System.out.print("kosong");}
  70.         else{
  71.             while (temp!=null){
  72.             System.out.println("["+temp.data+"]");
  73.             temp=temp.prev;
  74.             }
  75.         }
  76.     }
  77.    
  78.    
  79.    
  80.     public static void main(String[] args)
  81.     {
  82.         Scanner scan = new Scanner(System.in);  
  83.         jurnal1 ll = new jurnal1();
  84.         int w=0;
  85.         System.out.println("Linked jurnal1 Test\n");  
  86.         char ch;    
  87.         do
  88.         {
  89.        
  90.         System.out.println("\nLinked jurnal1 Operations");
  91.         System.out.println("1. maks");
  92.         System.out.println("2. enqueue");
  93.         System.out.println("3. dequeue");
  94.         System.out.println("4. push");
  95.         System.out.println("5. pop");
  96.         System.out.println("6. tampil queue");
  97.         System.out.println("7. tampil stack");
  98.         int choice = scan.nextInt();
  99.         switch (choice){
  100.         case 1 :
  101.         System.out.println("masukan maks=");
  102.         w=scan.nextInt();
  103.         ll.max(w);break;
  104.         case 2 :
  105.         System.out.println("masukan nilai=");
  106.         ll.enqueue(scan.nextInt());
  107.         break;  
  108.         case 3 :
  109.         ll.dequeue();
  110.         break;
  111.         case 4 :
  112.         ll.enqueue(scan.nextInt());;break;
  113.         case 5 :
  114.         ll.pop();break;
  115.         case 6 :
  116.         ll.tampil();break;
  117.         case 7 :
  118.         ll.tampil1();break;}
  119.         System.out.println("\nDo you want to continue (Type y or n) \n");
  120.         ch = scan.next().charAt(0);      
  121.         w++;
  122.         } while (ch == 'Y'|| ch == 'y');                
  123.        
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment