tugas ini saya bikin dalam rangka untuk memenuhi nilai ujian akhir semester 3, ini adalah mata kuliah struktur data. disini kami, khususnya di suruh untuk membuat program pada struktur data yang terdiri dari STACK, BUBBLE SHORT, QUEUE, SEARCHING, & LINKED LIST. OKE semoga bermafaat untuk kamu :)
Ø PROGRAM STACK / TUMPUKAN
public class Stack_FITRI_APRIANI {
privateintUkur;
private char [] Tumpukan;
privateint top;
publicStack_FITRI_APRIANI (int a) {
Ukur = a ;
Tumpukan = new char [Ukur];
top = -1;
}
public void Push (char b) {
Tumpukan [++top] = b;
}
public char Pop () {
returnTumpukan[top--];
}
publicbooleanisEmpty () {
System.out.println (" Tolong di Input ya ");
return (top == -1);
}
publicbooleanisFull () {
System.out.println ("Tolong di Input ya...??? ");
return (top == Ukur-1);
}
public void Baca () {
int c = top;
while (c>=0) {
System.out.print (Tumpukan[c]);
System.out.print (" ");
c--;
}
}
public static void main (String [] args) {
Stack Tumpuk = new Stack (50);
String Nama1, Nama2 ;
String Masuk;
java.util.Scanner scan = new java.util.Scanner (System.in);
System.out.println (" MasukkanNamaAnda...??? ");
Nama1 = scan.next();
for (int c = 0; c<Nama1.length(); c++) {
Tumpuk.Push(Nama1.charAt(c));
}
Tumpuk.Baca();
System.out.println (" ");
System.out.println (" Data Yang DihapusAdalah = "+Tumpuk.Pop());
System.out.println (" ");
Tumpuk.Baca();
}
}
Ø Contoh Program Bubble Sort
public class Bubble_Sort_Fitri_Apriani {
public void urutBubble(String nama[]) {
String Awal;
for (int b=0;b<=nama.length-2; b++) {
for (int a=0; a<=nama.length-2-b;a++) {
if (nama[a].compareTo(nama[a+1]) >0) {
Awal = nama[a];
nama[a] = nama[a+1];
nama[a+1] = Awal;
}
}
}
}
public voidLihatData(String nama[]) {
int a;
for (a=0; a<12;a++){
System.out.println(nama[a]);
}
}
public static void main(String args[]) {
Bubble_Sort_Fitri_Apriani Test = new Bubble_Sort_Fitri_Apriani();
String nama[]= new String [12];
java.util.Scanner scan =new java.util.Scanner(System.in);
System.out.println("masukkan Data AwalAnda :");
for (int a=0; a<12; a++){
nama[a]=scan.next();
}
System.out.println("");
Test.urutBubble(nama);
System.out.println("Data Akhirsetelahdiurutkan :");
Test.LihatData(nama);
}
}
Ø contoh program antrian
public class Antrian_Fitri_Apriani {
privateintUkur;
private char [] Data;
privateint Back, Front;
privateintJum;
publicAntrian_Fitri_Apriani (int a){
Ukur = a;
Data = new char [Ukur];
Front = 0;
Back = -1;
Jum =0;
}
public void Masukkan_Data (char b){
if (!isFull()) {
Data[++Back] = b;
Jum++;
}
}
public char Hasil_Data () {
charAmbil = Data[0];
if (!isEmpty()){
for (int c=0; c<Jum; c++){
Data[c]=Data[c+1];
}
Jum--;
Back--;
}
returnAmbil;
}
publicbooleanisFull () {
return Back == Ukur-1;
}
publicbooleanisEmpty () {
returnJum == 0;
}
publicintUkuran () {
returnJum;
}
public void Cetak () {
for (int c=0; c<=Back; c++)
System.out.println (Data[c]);
}
public static void main (String [] args) {
Antrian_Fitri_AprianiAntri = new Antrian_Fitri_Apriani(30);
java.util.Scanner scan = new java.util.Scanner (System.in);
String Tanya;
System.out.println (" Masukkan Data Anda ");
Tanya = scan.next();
for (int c=0; c<Tanya.length(); c++) {
Antri.Masukkan_Data(Tanya.charAt(c));
}
System.out.println (" Data Antrian :");
Antri.Cetak();
System.out.println (" ");
System.out.println (" Data Yang KeluarPertamaAdalah : "+Antri.Hasil_Data());
System.out.println (" Jumlah Data Yang Tersisa : "+Antri.Ukuran());
System.out.println (" Output Dari Data Yang AndaMasukkan :");
Antri.Cetak();
}
}
Ø CONTOH PROGRAM SEARCHING
importjava.util.Scanner;
public class Larik_FITRI_APRIANI {
//-------- UntukMenampilkan Isi Dari Larik ------------
static void Data_Hasil(String Nama [], int a)
{ System.out.println("**********************************************");
System.out.println(" SELAMAT DATANG DI PROGRAM FITRI APRIANI ");
System.out.println("***********************************************");
for (int b=0; b<=a-1; b++)
{ System.out.print(" " + Nama[b]+"");
}
}
//-------- UntukMencari Data DalamLarikSecara Binary ------------
static void Mencari_Binary (String Nama[], int a)
{
String Mencari_Nama;
booleanHasil = false;
Scanner Input_Data = new Scanner(System.in);
System.out.println ("");
System.out.println(" Masukkan Data Yang InginAndaCari : ");
Mencari_Nama = Input_Data.next();
for (int b=0; b<a; b++){
if (Nama[b].equals(Mencari_Nama)) {
System.out.println (" NamaAnda Ada Di...!!! "+b);
Hasil = true;
break;
}
}
if(!Hasil){
System.out.println (" Data Anda Error, MohonDicobaLagiYa...");
}
}
//--------program utama------------
public static void main(String [] args)
{ Scanner InputanA = new Scanner(System.in);
int a;
String Nama[] = {"F","I","T","R","I"," ","A","P","R","I","A","N","I"};
a = 30;
Data_Hasil(Nama,a);
Mencari_Binary(Nama,a);
}
}
Link list
importjavax.swing.*;
public class fitrilinklist {
public static void main (String [] args) {
fitriclassOperasi=new fitriclass();
System.out.println ("fitrilinklist");
System.out.println (".................");
intfitrijawab=0, fitrijawablagi=0;
do {
String operasi=JOptionPane.showInputDialog("operasi yang dapatandapilih:\n1.add\n2.hapus\n3.cetak");
int op=Integer.parseInt (operasi);
switch (op) {
case 1: {
do {
String add=JOptionPane.showInputDialog("manambahkan node:");System.out.print ("\nmenambahkan node ("+add+")");
Operasi.add(add);
String jawab=JOptionPane.showInputDialog("please enter 1 if you want to maju ");
fitrijawab=Integer.parseInt(jawab);}
while (fitrijawab==1);}
break;
case 2: {
String hapus = JOptionPane.showInputDialog ("Menghapus node :");
System.out.println ("\nMenghapus Node ("+hapus+")");
Operasi.Hapus(hapus);
}
break ;
case 3 : {
System.out.println () ;
Operasi.Cetak () ;
}
break ;
default :
JOptionPane.showInputDialog (null,"Maaf, operasi yang andapilihtidakada","LAPORAN",JOptionPane.ERROR_MESSAGE) ;
}
String jawab2 = JOptionPane.showInputDialog("Tekan '1' jikainginmemilihoperasi yang lain");
fitrijawablagi = Integer.parseInt(jawab2);
}
while (fitrijawablagi==1);
JOptionPane.showInputDialog(null,"TERIMA KASIH ",":-)",JOptionPane.INFORMATION_MESSAGE);
}
}
//=============================================================================================================================================//
classfitriapriani {
public String input ;
public fitriaprianimaju ;
publicfitriaprianimundur ;
}
//============================================================================================================================================//
classfitriclass {
privatefitriapriani head, tail;
public void fitriclass() {
head=null;
tail=null;}
public void add(String add) {
fitriapriani node=new fitriapriani();
node.input=add;
if(head==null) {
head=tail=node;}
else {
node.mundur=tail;
tail.maju=node;
tail=tail.maju;}}
public void Hapus(String hapus) {
fitriaprianidihapus=head;
while (dihapus !=null) {
if (hapus.equals(dihapus.input)) {
if(dihapus==head) {
head=dihapus.maju;
head.mundur=null;
dihapus=null;
return;}
else{
if(dihapus.maju==null){
tail=tail.mundur;
tail.maju=null;
}
else {
fitriapriani bantu=dihapus.mundur;
bantu.maju=dihapus.maju;
fitriapriani bantu2=dihapus.maju;
bantu2.mundur=bantu;
dihapus=null;}
//dihapus.maju.mundur=dihapus.mundur;
//dihapus.mundur.maju=dihapus.naxt;
//dihapus.maju=dihapus;
//dihapus.mundur=dihapus;
return;}}
else{
dihapus=dihapus.maju;}}
if(dihapus==null)
JOptionPane.showMessageDialog(null,"sorry, data yang akanAndahapustidakada","note",JOptionPane.ERROR_MESSAGE);}
public void Cetak() {
String caracetak=JOptionPane.showInputDialog("Cara cetak yang dapatAndapilih :\n1.Head -> Tail\n2.Tail-> Head");
intcarafitri=Integer.parseInt(caracetak);
switch(carafitri) {
case 1:{
fitriaprianicetak=head;
System.out.println("Cetak Head -> Tail :");
while(cetak!=null) {
System.out.print (cetak.input+"->");
cetak=cetak.maju; }
System.out.print("null");
System.out.print(""); }
break;
case 2: {
fitriaprianicetak=tail;
System.out.println("Cetak Tail -> Head :");
System.out.print("null");
while (cetak!=null) {
System.out.print("->"+cetak.input);
cetak=cetak.mundur;}
System.out.println ();}
break;
default:
JOptionPane.showMessageDialog(null,"Maaf, caracetak yang Andapilihtidakada","note",JOptionPane.ERROR_MESSAGE);} }}
Ø
0 komentar:
Posting Komentar