saban123

Untitled

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