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);}});
}
}
- java 数据结构 遍历链表程序
- java--数据结构--二叉树遍历
- java数据结构:链表与迭代器入门程序
- Java数据结构 遍历 排序 查找 算法实现
- java数据结构之二叉树的遍历
- Java数据结构-二叉树及其遍历
- Java数据结构Map遍历和排序
- java数据结构链表
- 《数据结构》2.6通过一趟遍历找出链表中的最大值
- 数据结构系列之链表——单链表遍历
- 数据结构—链表的定义、创建、遍历、插入、删除
- java模拟---遍历链表
- 数据结构学习二 数据结构之链表代码版【创建,遍历,删除,插入】
- 数据结构学习二 数据结构之链表图解版【创建,遍历,删除,插入】
- java数据结构--二叉树,递归遍历,非递归遍历,层次遍历
- 【Java数据结构】Java数据结构之链表反转
- java数据结构之链表
- JAVA数据结构之链表
- 用vc实现对qq发送按钮消息的截获
- ajax文件上传核心代码
- DES加密解密
- 归并排序
- 分析mirand事件
- java 数据结构 遍历链表程序
- DotNetNuke
- C#调用 C++
- java 五子棋中的AI技术
- Meditation(part 1)
- 用CSS打造中英文双语导航菜单
- java的象棋 核心代码部分
- 19岁的村主任与25岁的副处级
- dll文件的c++制作dll文件的c++制作