Вы находитесь на странице: 1из 5

JURNAL MODUL 2

1. Jurnal no1
a. Source code
package PRAKTIKUM;

public class Jurnal_1 {


class Node {
int data;
Node next;
Node prev;
public Node (int data){
this.data=data;
next=null;
prev=null;
}
}
Node head,tail;
int size = 0;

public static void main(String[] args) {


Jurnal_1 list=new Jurnal_1();
list.addfirst(2);
list.addfirst(4);
list.addfirst(6);
list.addfirst(8);
list.addfirst(10);
list.show();
System.out.println();
list.removeafter(8);
list.show();
System.out.println();
list.addafter(4, 6);
list.show();
System.out.println();
}
void addfirst(int data){
Node tambah = new Node(data);
if (head == null){
head = tail = tambah;
}else{
tambah.next = head;
tambah.prev = tail;
head.prev = tambah;
tail.next = tambah;
head = tambah;
}
size++;
}
void removeafter(int indeks) {
Node current = head;
Node temp;
for(int i = 1; i<=indeks; i++){
current=current.next;
}
temp = current.prev.prev;
current.prev = temp;
temp.next= temp.next.next;
size--;
}
void addafter(int data, int indeks){
Node tambah = new Node(data);
Node current = head;
Node temp;
for(int i = 1; i<indeks; i++){
current=current.next;
}
temp = current.prev;
temp.next = tambah;
tambah.next = current;
tambah.prev = temp;
current.prev = tambah;
size++;
}

void show() {
Node current = head;
while (current != null){
System.out.print(current.data + " ");
current=current.next;
if (current == head){
System.out.print(current.data + " ");
break;
}
}
}

}
b. Hasil run

2. Jurnal no2
a. Source code
package PRAKTIKUM;
import java.util.Random;
public class Jurnal_2 {
class node{
int data;
node next;
node prev;
public node(int data){
this.data=data;
next=null;
prev=null;
}
}
node head;
node tail;
public static void main(String[] args) {
Jurnal_2 code = new Jurnal_2();
Jurnal_2 pasword = new Jurnal_2();
Random angka = new Random();
boolean cek=false;
code.addLast(1);
code.addLast(2);
code.addLast(3);
code.addLast(4);
code.addLast(5);
System.out.println("Kode Gembok dirahasiakan dari bambang");
code.print();
int x=0;
System.out.println("");
System.out.println("Mari kita Brute Force");
while(cek!=true){
pasword.addLast(angka.nextInt(5));
pasword.addLast(angka.nextInt(5));
pasword.addLast(angka.nextInt(5));
pasword.addLast(angka.nextInt(5));
pasword.addLast(angka.nextInt(5));
pasword.print();
node kode=pasword.head;
node kunci=code.head;
while(kode!=null){

if(kode.data==kunci.data){
x++;
System.out.println(x);
kode=kode.next;
kunci=kunci.next;
}else{
System.out.println("Kode Salah");
break;
}
}
if(x==4){
System.out.println("Gembok Berhasil dibuka");
cek=true;
}
x=0;
pasword.del();
}
}
public boolean IsEmpty (){
return head==null;
}
public void addLast(int data){
node baru = new node(data);
if(IsEmpty()){
head=tail=baru;
}
else{
tail.next=baru;
baru.prev=tail;
tail=baru;
tail.next=null;
}
}
public void del(){
head=tail=null;
}

public void print(){


if(head==null){
System.out.print("Empty");
}
else{
node tmp=head;
do{
System.out.print("{"+tmp.data+"}");
tmp=tmp.next;
}while(tmp!=null);
}
}

b. Hasil run

Вам также может понравиться