Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class node
- { node next;
- int data;
- node prev;
- public node(int data)
- {
- this.data=data;
- next=null;
- }
- }
- import java.util.Scanner;
- public class jurnal1{
- int b,max;
- node head,tail;
- public jurnal1(){
- head=tail=null;
- b=0;}
- public void max(int x){
- max=x;}
- public void enqueue(int x){
- node baru =new node (x);
- if (head==null){
- head=baru;
- tail=baru;
- b=b+1;
- }
- else if(b<max){
- tail.next=baru;
- baru.prev=tail;
- tail=baru;
- b=b+1;}
- else {
- System.out.print("full");}
- }
- public void pop(){
- node temp=tail;
- tail=tail.prev;
- tail.next=null;
- temp=null;
- b=b-1;
- }
- public void dequeue(){
- node temp=head;
- head=head.next;
- temp=null;
- b=b-1;
- }
- public void tampil(){
- node temp=head; if (head==null){
- System.out.print("kosong");}
- else{
- while (temp!=null){
- System.out.println("["+temp.data+"]");
- temp=temp.next;
- }
- }
- }
- public void tampil1(){
- node temp=tail;
- if (head==null){
- System.out.print("kosong");}
- else{
- while (temp!=null){
- System.out.println("["+temp.data+"]");
- temp=temp.prev;
- }
- }
- }
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- jurnal1 ll = new jurnal1();
- int w=0;
- System.out.println("Linked jurnal1 Test\n");
- char ch;
- do
- {
- System.out.println("\nLinked jurnal1 Operations");
- System.out.println("1. maks");
- System.out.println("2. enqueue");
- System.out.println("3. dequeue");
- System.out.println("4. push");
- System.out.println("5. pop");
- System.out.println("6. tampil queue");
- System.out.println("7. tampil stack");
- int choice = scan.nextInt();
- switch (choice){
- case 1 :
- System.out.println("masukan maks=");
- w=scan.nextInt();
- ll.max(w);break;
- case 2 :
- System.out.println("masukan nilai=");
- ll.enqueue(scan.nextInt());
- break;
- case 3 :
- ll.dequeue();
- break;
- case 4 :
- ll.enqueue(scan.nextInt());;break;
- case 5 :
- ll.pop();break;
- case 6 :
- ll.tampil();break;
- case 7 :
- ll.tampil1();break;}
- System.out.println("\nDo you want to continue (Type y or n) \n");
- ch = scan.next().charAt(0);
- w++;
- } while (ch == 'Y'|| ch == 'y');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment