Showing posts with label DataStructure. Show all posts
Showing posts with label DataStructure. Show all posts

Berikut adalah salah satu contoh baris program sederhana 



import java.util.Scanner;
public class ReverseSum {
    public static void main(String[] args) {
        int x, z, a, b, m = 0, n = 0, all;
        
        Scanner s = new Scanner(System.in);
        System.out.print("Masukan Bilangan Pertama : ");
        x = s.nextInt();
        System.out.print("Masukang Bilangan Kedua : ");
        z = s.nextInt();
        
        do{
            a = x % 10;
            m = m * 10 + a;
            x = x / 10;
        }
        while(x>0);
        do{
            b = z % 10;
            n = n * 10 + b;
            z = z / 10;
        }
        while(z>0);
        
        System.out.println("Hasil reverse bilangan pertama : "+m);
        System.out.println("Hasil reverse bilangan kedua : "+n);
        
        System.out.println("Penjumlahan Kedua Bilangan Reverse");
        all = m + n;
        System.out.println(all);
    }
    
}

Berikut adalah video proses membuat program sederhana


Program Sederhana Membuat Aplikasi Reverse Sum menggunakan Bahasa Java


Berikut adalah salah satu contoh baris program sederhana 

import java.util.Arrays;

public class Array {
   public static void main(String[] args) {
       int[] x = {1, 2, 3};
       int[] z = {8, 9, 30};
       int sumX = 0;
       int sumZ = 0;
       int jumlah;
       
       for(int num : x){
           sumX = sumX+num;
       }
       for(int num : z){
           sumZ = sumZ+num;
       }
       jumlah = sumX + sumZ;
       
       System.out.println("Jumlah Nilai Kedua Array : "+jumlah);
       
       int[] xz = new int[x.length+z.length];
       System.arraycopy(x, 0, xz, 0, z.length);
       System.arraycopy(z, 0, xz, x.length, z.length);
       
       System.out.println("Menggabungkan Kedua Array");
       System.out.println(Arrays.toString(xz));

       
  }
    
}

Berikut adalah video proses membuat program sederhana


Program Sederhana Membuat Aplikasi Penambahan Bilangan Array menggunakan Bahasa Java


Tipe data abstrak (TDA) atau lebih dikenal dalam bahasa Inggris sebagai Abstract data type (ADT) merupakan model matematika yang merujuk pada sejumlah bentuk struktur data yang memiliki kegunaan atau perilaku yang serupa. Tipe data abstrak umumnya didefinisikan tidak secara langsung, melainkan hanya melalui operasi matematis tertentu sehingga membutuhkan penggunaan tipe data tersebut meski dengan risiko kompleksitas yang lebih tinggi atas operasi tersebut. Salah satu penggunaan ADT pada pemograman adalah Queue, Stack dan List. 

Berikut adalah salah satu contoh baris program sederhana 


import java.util.Scanner;
public class Kalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int hari = 0, tanggal, bulan, tahun;
        boolean Tanggal, Bulan, Valid;
        String hasil, namaBulan = null;
        
        System.out.print("Masukan Tanggal : ");
        tanggal = input.nextInt();
        System.out.print("Masukan Bulan : ");
        bulan = input.nextInt();
        System.out.print("Masukan Tahun : ");
        tahun = input.nextInt();
        
        if(bulan == 1){
            hari = 31;
            namaBulan = "Januari";
        }
        else if(bulan == 2){
            if((tahun % 4 == 0 && tahun % 100!=0) || tahun % 400 == 0){
                hari = 29;
                namaBulan = "Februari";
            }
            else{
                hari = 28;
                namaBulan = "Februari";
            }
        }
        else if(bulan == 3){
            hari = 31;
            namaBulan = "Maret";
        }
        else if(bulan == 4){
            hari = 30;
            namaBulan = "April";
        }
        else if(bulan == 5){
            hari = 31;
            namaBulan = "Mei";
        }
        else if(bulan == 6){
            hari = 30;
            namaBulan = "Juni";
        }
        else if(bulan == 7){
            hari = 31;
            namaBulan = "Juli";
        }
        else if(bulan == 8){
            hari = 30;
            namaBulan = "Agustus";
        }
        else if(bulan == 9){
            hari = 31;
            namaBulan = "September";
        }
        else if(bulan == 10){
            hari = 30;
            namaBulan = "Oktober";
        }
        else if(bulan == 11){
            hari = 31;
            namaBulan = "November";
        }
        else if(bulan == 12){
            hari = 30;
            namaBulan = "Desember";
        }
        else{
            hari = -1;
            namaBulan = Integer.toString(bulan);
        }
        
        Tanggal = tanggal >=1 && tanggal <= hari;
        Bulan = bulan >=1 && bulan <=12;
        Valid = Tanggal && Bulan;
        
        if(Valid){
            hasil = " merupakan tanggal yang valid.";
        }
        else{
            hasil = " merupakan tanggal yang tidak valid.";
        }
        
        System.out.println("\n"+tanggal+" "+namaBulan+" "+tahun+hasil);
    }
    
}

 Berikut adalah video proses membuat program sederhana 



Program Sederhana Membuat ADT Tanggal menggunakan Bahasa Java


Berikut adalah salah satu contoh baris program sederhana




public class CircularLL{
    static class Node{
        int data;
        Node next;
    }
    
    static Node addToEmpty(Node last, int data){
        if (last != null)
            return last;
        
        Node temp = new Node();
        temp.data = data;
        last = temp;
        last.next = last;
        
        return last;
    }
    
    static Node addBegin(Node last, int data){
        if(last == null)
            return addToEmpty(last, data);
        
        Node temp = new Node();
        
        temp.data = data;
        temp.next = last.next;
        last.next = temp;
        
        return last;
    }
    
    static Node addEnd(Node last, int data){
        if (last == null)
            return addToEmpty(last, data);
        
        Node temp = new Node();
        
        temp.data = data;
        temp.next = last.next;
        last.next = temp;
        last = temp;
        
        return last;
    }
    
    static Node addAfter(Node last, int data, int item){
        if (last == null)
            return null;
        
        Node temp, p;
        p = last.next;
        
        do{
            if(p.data == item){
                temp = new Node();
                temp.data = data;
                temp.next = p.next;
                p.next = temp;
                
                if(p == last)
                    last = temp;
                return last;
            }
            p = p.next;
        }
        while(p != last.next);
        
        System.out.println(item + " not present in the list.");
        return last;
    }
    
    static void traverse(Node last){
        Node p;
        
        if (last == null){
            System.out.println("List is Empty.");
            return;
        }
        
        p = last.next;
        
        do{
            System.out.print(p.data + " ");
            p = p.next;
        }
        while (p != last.next);
    }
    
    public static void main(String[] args){
        Node last = null;
        
        last = addToEmpty(last,6);
        last = addBegin(last, 4);
        last = addBegin(last, 3);
        last = addEnd(last, 10);
        last = addEnd(last, 9);
        last = addEnd(last, 50);
        last = addAfter(last, 70, 9);
        
        traverse(last);
    }
}

Berikut adalah video proses membuat program sederhana 


Program Sederhana Membuat Aplikasi Circular Linked List menggunakan Bahasa Java


Berikut adalah salah satu contoh baris program sederhana yang kita bagi menjadi 3 bagian class

Class Node



public class Node{
    int data;
    Node next;
    Node prev;
    
    public Node(int data){
        this.data = data;
    }
    
    public void tampil(){
        System.out.print("("+data+")");
    }
}

Class DoublyLinkedList


public class DoubleLinkedList{
    Node first;
    Node last;
    
    public DoubleLinkedList(){
        first = null;
        last = null;
    }
    
    public boolean isEmpty(){
        return (first == null);
    }
    
    public void insertFirst(int data){
        Node node = new Node(data);
        if(isEmpty()){
            last = node;
        }
        else{
            first.prev = node;
        }
        node.next = first;
        first = node;
    }
    
    public void insertLast(int data){
        Node node = new Node(data);
        if(isEmpty())
            first = node;
        else{
            last.next = node;
            node.prev = last;
        }
        last = node;
    }
    
    public Node deleteFirst(){
        Node temp = first;
        if(first.next == null)
            last = null;
        else
            first.next.prev = null;
        first = first.next;
        return temp;
    }
    
    public Node deleteLast(){
        Node temp = last;
        if (first.next == null)
            first = null;
        else
            last.prev.next = null;
        last = last.prev;
        return temp;
    }
    
    public boolean insertAfter(int key, int data){
        Node current = first;
        while(current.data != key){
            current = current.next;
            if(current == null)
            return false;
        }
        Node node = new Node(data);
        
        if (current==last){
            node.next = null;
            last = node;
        }
        else{
            node.next = current.next;
            
            current.next.prev = node;
        }
        node.prev = current;
        current.next = node;
        return true;
    }
    
    public Node deleteKey(int key){
        Node current = first;
        while(current.data != key){
            current = current.next;
        if(current == null)
            return null;
        }
        if(current == first)
            first = current.next;
        else
            current.prev.next = current.prev;
            return current;
    }
    
    public void displayForward(){
        System.out.print("List | First -> Last : ");
        Node current = first;
        
        while(current != null){
            current.tampil();
            current = current.next;
        }
        System.out.println("");
    }
    
    public void displayBackward(){
        System.out.print("List | Last -> First : ");
        Node current = last;
        while(current != null){
            current.tampil();
            current = current.prev;
        }
        System.out.println("");
    }
}

Class APP


public class APP{
    public static void main(String[] args){
        DoubleLinkedList list = new DoubleLinkedList();
        list.insertFirst(10);
        list.insertFirst(15);
        list.insertFirst(20);
        list.insertLast(30);
        list.insertLast(35);
        list.insertLast(40);
        list.displayForward();
        list.displayBackward();
        list.deleteFirst();
        list.deleteLast();
        list.deleteKey(15);
        list.displayForward();
        list.insertAfter(10, 90);
        list.insertAfter(30, 95);
        list.displayForward();
    }
}


Berikut adalah video proses membuat program sederhana 



Program Sederhana Membuat Aplikasi Double Linked List menggunakan Bahasa Java


Queue merupakan koleksi item yang cara penambahan itemnya terjadi pada sebuah ujung yang biasa disebut sebagai “ekor” atau (rear) dan untuk penghapusannya, terjadi pada ujung yang satunya. Atau biasa kita beri nama “kepala” atau (head). Jadi konsep dari queue ini menggunakan konsep layaknya FIFO yang merupakan kepanjangan dari First in First out. 

Berikut adalah salah satu contoh program sederhana menggunakan metode Queue

package queue;

public class Queue {
    int queue[]=new int[5];
    
    public void push(int value){
        if(queue[0]==0){
            queue[0]=value;
            System.out.println("Antrian Pertama :" + queue[0]);
        }
        else if(queue[1]==0){
            queue[1]=value;
            System.out.println("Antrian Kedua :" + queue[1]);
        }
        else if(queue[2]==0){
            queue[2]=value;
            System.out.println("Antrian Ketiga :" + queue[2]);
        }
        else if(queue[3]==0){
            queue[3]=value;
            System.out.println("Antrian Keempat :" + queue[3]);
        }
        else if(queue[4]==0){
            queue[4]=value;
            System.out.println("Antrian Kedua :" + queue[4]);
        }
        else{
            isFull();
        }
    }
    
    public void isFull(){
        System.out.println("Antrian Penuh");
    }
    
    public void pop(){
        if(queue[0]!=0){
            System.out.println("POP :" + queue[0]);
            queue[0]=0;
        }
        else if(queue[1]!=0){
            System.out.println("POP :" + queue[1]);
            queue[1]=0;
        }
        else if(queue[2]!=0){
            System.out.println("POP :" + queue[2]);
            queue[2]=0;
        }
        else if(queue[3]!=0){
            System.out.println("POP :" + queue[3]);
            queue[3]=0;
        }
        else if(queue[4]!=0){
            System.out.println("POP :" + queue[4]);
            queue[4]=0;
        }
        else{
            isEmpty();
        }
    }
    
    public void isEmpty(){
        System.out.println("Antrian Kosong");
    }
    
    public void clear(){
        queue[0]=0;
        queue[1]=0;
        queue[2]=0;
        System.out.println("Antrian Sudah Bersih");
    }
    
    public void cetak(){
        System.out.println("--------------------------------");
        for(int i=0;i<queue.length;i++){
            if(queue[i]!=0){
            System.out.println(queue[i]+" ");
            }
        }
        System.out.println("--------------------------------");
    }
    
    public static void main(String[] args) {
        Queue s=new Queue();
        s.push(5);
        s.push(3);
        s.push(2);
        s.push(7);
        s.push(8);
        s.cetak();
        s.pop();
        s.pop();
        s.cetak();
        s.clear();
    }
    
}

Berikut adalah video proses membuat program sederhana

Program Sederhana Membuat Antrian menggunakan Bahasa Java

Berikut adalah salah satu contoh baris program sederhana



package strukturdata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StrukturData {
    public static void main(String[] args) {
        List daftarAngka = new ArrayList();
        daftarAngka.add("1");
        daftarAngka.add("4");
        daftarAngka.add("3");
        daftarAngka.add("5");
        daftarAngka.add("2");
        
        System.out.println("Sebelum Diurutkan");
        System.out.println(daftarAngka);
        
        Collections.sort(daftarAngka);
        System.out.println("Setelah Diurutkan");
        System.out.println(daftarAngka);
    }
    
}

Berikut adalah video proses membuat program sederhana


Program Sederhana Membuat Aplikasi Insert List Urut menggunakan Bahasa Java

Berikut adalah salah satu contoh baris program sederhana 

x = [5, 20, 30]
z = [20, 30, 90]

# Menghitung jumlah nilai semua array
xTotal = sum(x)
zTotal = sum(z)
jumlah = xTotal + zTotal
print("Jumlah Nilai Kedua Array :", jumlah)

# Menggabungkan data dari kedua array
print("Gabungan Array")
print(x + z)

Berikut adalah video proses membuat program sederhana


Program Sederhana Membuat Aplikasi Penambahan Bilangan Array pada Bahasa Python