因此,我正在使用搜索算法编写程序。 这是我的代码,
public class BismillahKasir3 extends javax.swing.JFrame {
LinkedList <String> namaa = new LinkedList<String>();
LinkedList <String> noohp = new LinkedList<String>();
LinkedList <String> kodebuku = new LinkedList<String>();
LinkedList <String> judulbuku = new LinkedList<String>();
static linked buku = new linked();
static node chatta = new node();
/**
* Launch the application.
*/
Scanner input = new Scanner(System.in);
buku.tambahakhir("BUKU", "A001", 13000);
buku.tambahakhir("5 cm", "A002", 2000);
buku.tambahakhir("1000 Muslim Mengejar Bintang", "A003", 3000);
buku.tambahakhir("bismillah", "A004", 5000);
tfKODE = new JTextField();
tfKODE.setFont(new Font("Tahoma", Font.PLAIN, 14));
tfKODE.setBounds(99, 62, 197, 29);
framekasir3.getContentPane().add(tfKODE);
tfKODE.setColumns(10);
linesearch uwu = new linesearch();
JButton btnCari = new JButton("Cari");
btnCari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
chatta = uwu.search(buku, tfKODE.getText());
if(uwu.indeks ==-1) {
JOptionPane.showMessageDialog(null, "Judul buku dengan kode " +chatta.kode+ " tidak ditemukan");
tfNAMA.setText("");
tfSATUAN.setText("");
}
else {
tfNAMA.setText("" + chatta.judul);
tfSATUAN.setText("" +chatta.harga);
}
}
});
btnCari.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnCari.setBounds(99, 96, 89, 23);
framekasir3.getContentPane().add(btnCari);
tfNAMA = new JTextField();
tfNAMA.setFont(new Font("Tahoma", Font.PLAIN, 10));
tfNAMA.setEditable(false);
tfNAMA.setBounds(99, 129, 197, 29);
framekasir3.getContentPane().add(tfNAMA);
tfNAMA.setColumns(10);
tfSATUAN = new JTextField();
tfSATUAN.setFont(new Font("Tahoma", Font.PLAIN, 14));
tfSATUAN.setEditable(false);
tfSATUAN.setBounds(99, 169, 197, 29);
framekasir3.getContentPane().add(tfSATUAN);
tfSATUAN.setColumns(10);
}
}
class node {
String kode;
String judul;
int harga;
node next;
node prev;
}
class linesearch{
int indeks;
public node search(linked b, String kodee){
indeks = -1;
node cari = new node();
cari.kode = kodee;
node ganti = b.head;
for (int i = 0; i< b.ukuran(); i++) {
String smbrng = ganti.kode;
if(kodee.equals(smbrng)) {
indeks = i;
cari.judul = ganti.judul;
cari.harga = ganti.harga;}
ganti = ganti.next;
}
return cari;
}
}
class linked {
node head;
public int ukuran() {
node em = head;
int size = 0;
while (em!= null) {
size = size+1;
em = em.next;}
return size;
}
public node indeks(int a) {
node ma = head;
if (a<ukuran()&& a>= 0) {
for (int i = 0; i<a; i++) {
ma = ma.next;
}
return ma;}
else return null;}
public void tambahawal (String a, String b, int c) {
node em = new node();
em.judul = a;
em.kode = b;
em.harga = c;
em.next = head;
em.prev = null;
if(head!=null)
head.prev = em;
head = em;
}
public void tambahakhir (String a, String b, int c) {
node em = new node();
em.judul = a;
em.kode = b;
em.harga = c;
node ganti = head;
if(kosong()) {
tambahawal(a,b,c);
}
else {
while(ganti.next!=null) {
ganti = ganti.next;
ganti.next = em;
em.prev = ganti;
em.next = null;
}
}
}
public void tampil() {
node em = head;
System.out.println("\n" +em.kode + "\t" + em.judul + "\t" + em.harga);
em = em.next;
while(em!= null) {
System.out.println("\n" +em.kode+ "\t" + em.judul + "\t" + em.harga);
em = em.next;
}
}
}
事实是,当我搜索“ A001”时,结果很好。但是当我搜索“ A002”或“ A003”或“ A004”时,结果不存在(但没有给我一个错误)。
你能帮我吗?先感谢您...