java 数据结构 遍历链表程序

来源:互联网 发布:hexo github 绑定域名 编辑:程序博客网 时间:2024/05/16 09:56

java 数据结构 遍历链表程序
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class shop extends Panel{
 String number,name;
 int shuliang;
 float danjia;
 shop(String number,String name,int shuliang,float danjia){
  this.number=number;
  this.name=name;
  this.shuliang=shuliang;
  this.danjia=danjia;
 }
}

class shopping extends JFrame implements ActionListener{
 LinkedList goods_list=null;
 JTextField numbertxt=new JTextField();
 JTextField nametxt=new JTextField();
 JTextField danjiatxt=new JTextField();
 JTextField shuliangtxt=new JTextField();
 JTextField deletetxt=new JTextField();
 JButton b_add=new JButton("添加商品");
 JButton b_del=new JButton("删除商品");
 JButton b_show=new JButton("显示商品");
 JTextArea show=new JTextArea();
 shopping(){
  goods_list=new LinkedList();
  Container con=getContentPane();
  JScrollPane pane=new JScrollPane(show);
  show.setEditable(false);
  JPanel save=new JPanel();
  save.setLayout(new GridLayout(5,2));
  save.add(new Label("输入代码"));
  save.add(numbertxt);
  save.add(new Label("输入名称"));
  save.add(nametxt);
  save.add(new Label("输入库存"));
  save.add(shuliangtxt);
  save.add(new Label("输入单价"));
  save.add(danjiatxt);
  save.add(new Label("单击添加"));
  save.add(b_add);
  JPanel del=new JPanel();
  del.setLayout(new GridLayout(2,2));
  del.add(new Label("输入删除的代码"));
  del.add(deletetxt);
  del.add(new Label("单击删除"));
  del.add(b_del);
  JPanel show1=new JPanel();
  show1.setLayout(new BorderLayout());
  show1.add(pane,BorderLayout.CENTER);
  show1.add(b_show,BorderLayout.SOUTH);
  JSplitPane split_one,split_two;
  split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,save,del);
  split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,show1);
  con.add(split_two,BorderLayout.CENTER);
  b_add.addActionListener(this);
  b_del.addActionListener(this);
  b_show.addActionListener(this);
 }
 public void actionPerformed(ActionEvent e){
  if(e.getSource()==b_add){
   String daihao,mingcheng;
   int kucun;
   float danjia;
   daihao=numbertxt.getText();
   mingcheng=nametxt.getText();
   kucun=Integer.parseInt(shuliangtxt.getText());
   danjia=Float.valueOf(danjiatxt.getText()).floatValue();
   shop goods=new shop(daihao,mingcheng,kucun,danjia);
   goods_list.add(goods);
   try{
    FileOutputStream file=new FileOutputStream("goods.txt");
    ObjectOutputStream out=new ObjectOutputStream(file);
    out.writeObject(goods_list);
    out.close();
   }catch(IOException ex1){}
  }
  else if(e.getSource()==b_del){
   String daihao=deletetxt.getText();
   try{
    FileInputStream come_in= new FileInputStream("goods.txt");
    ObjectInputStream in=new ObjectInputStream(come_in);
    goods_list=(LinkedList)in.readObject();
    in.close();
   }catch(ClassNotFoundException e1){}
   catch(IOException e2){}
   int num=goods_list.size();
   for(int i=0;i<num;i++){
    shop temp=(shop)goods_list.get(i);
    if(temp.number.equals(daihao)){
     goods_list.remove(i);}
     try{
      FileOutputStream file_out=new FileOutputStream("goods.txt");
      ObjectOutputStream out=new ObjectOutputStream(file_out);
      out.writeObject(goods_list);
      out.close();
     }catch(IOException ex){}
    }
   }
   else if(e.getSource()==b_show){
    show.setText(null);
    try{
     FileInputStream come_in= new FileInputStream("goods.txt");
        ObjectInputStream in=new ObjectInputStream(come_in);
        goods_list=(LinkedList)in.readObject();
     }catch(ClassNotFoundException e1){}
         catch(IOException e2){}
         Iterator iter=goods_list.iterator();
         while(iter.hasNext()){
       shop te=(shop)iter.next();
       show.append("商品代码:"+te.number);
       show.append("商品名称:"+te.name);
       show.append("商品库存:"+te.shuliang);
       show.append("商品单价:"+te.danjia);
       show.append("/n");
      }
     }
    }
   }
   public class LinkListFour{
    public static void main(String args[]){
     shopping sp=new shopping();
     sp.setSize(100,100);
     sp.setVisible(true);
     sp.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
       System.exit(0);}});
      }
     }

 

 

原创粉丝点击