線形リスト:データ構造
public class Cell
{
int key;
String data;
Cell next;
Cell(int pkey,String pdata){
key=pkey;
data=pdata;
}
}//Cell
Cell cell=new Cell(Integer.parseInt(skey),sdata); cell.next=top; top=cell;
Cell find=top;
while(find.key!=ikey){
if (find.next==null) {
textField2.setText("not found");//見つからない
return ;
}
else
find=find.next;//次のセルを調べる
}
textField2.setText(find.data);//見つかった
void insert_actionPerformed(ActionEvent e) {
//挿入
String skey=textField1.getText();
String sdata=textField2.getText();
Cell cell=new Cell(Integer.parseInt(skey),sdata);
cell.next=top.next;
top.next=cell;
dispList();
}
void serch_actionPerformed(ActionEvent e) {
//検索
Cell find=top.next;
String skey=textField1.getText();
int ikey=Integer.parseInt(skey);
if (find ==null) return;
while(find.key!=ikey){
if (find.next==null) {
textField2.setText("not found");
return ;
}
else
find=find.next;
}
textField2.setText(find.data);
}
void delete_actionPerformed(ActionEvent e) {
String skey=textField1.getText();
delete(Integer.parseInt(skey));
}
void delete(int ikey){
Cell current,prev;
prev=top;
current=top.next;
if(current==null) return;
while(current.key !=ikey){
if(current.next==null) return;
else {
prev=current;
current=current.next;
}
}
prev.next=current.next;
dispList();
}
void dispList(){
Cell find=top.next;
String str="";
while(find != null){
str += Integer.toString(find.key)+":"+find.data+"\n";
find=find.next;
}
textArea1.setText(str);
}