最近在学习数据结构的java表示方法(本来C描述的就学不好,但觉得到时做毕业论文可能有用,所以就再学)
这是一个link-list的例子,半图形界面
Main.java是运行主类,ListTest.java是图形界面,List.java是链表主代码, Utility.java用来存储数据
I want to make the ActionListener as a separating class, but it doesn't work. So i make it as a method.
Main.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String [] args)
{
ListTest listTest = new ListTest();
listTest.init ();
listTest.setTitle("Link list");
listTest.setSize (400,400);
listTest.setVisible (true);
}
}
ListTest.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class ListTest extends JFrame implements ActionListener
{
JButton addAtF,addAtB,delFrF,delFrB,print;
JTextField input;
JLabel display;
JPanel p1,p2,p3,p4;
Container container;
Listener listener;
public void init()
{
Listener listener = new Listener();
//List myList = new List();
input = new JTextField(35);
display = new JLabel("Result: ");
JButton addAtF = new JButton("addAtF");
JButton addAtB = new JButton("addAtB");
JButton delFrF = new JButton("delFrF");
JButton delFrB = new JButton("delFrB");
JButton print = new JButton("print");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
container = getContentPane();
p1.add (input);
p2.add (display);
p3.add (addAtF);
p3.add (addAtB);
p3.add (delFrF);
p3.add (delFrB);
p3.add (print);
p4.add (p1);
p4.add (p2);
p4.add (p3);
container.add (p4);
addAtF.addActionListener(this);
addAtB.addActionListener(this);
delFrF.addActionListener(this);
delFrB.addActionListener(this);
print.addActionListener(this);
/*addAtF.addActionListener(listener);
addAtB.addActionListener(listener);
delFrF.addActionListener(listener);
delFrB.addActionListener(listener);
print.addActionListener(listener);
*/
}
public List myList;
public Utility myUtility;
public void actionPerformed( ActionEvent e)
{
List myList = new List();
//JLabel display = new JLabel();
double ran = 100 * Math.random();
int ranInt = (int)ran;
Integer objInt = new Integer( ranInt );
String cmd = e.getActionCommand();
if (cmd.equals("addAtF"))
{
if ( myUtility.count != 0)
for (int i=0;i
Integer intOut = new Integer(myUtility.ranNum[i]);
myList.insertAtFront(intOut);
}
display.setText("aaaa");
display.repaint();
myList.insertAtFront ( objInt );
myList.print ();
this.dataInput(ranInt);
//myUtility.ranNum[i] = 123;
//System.out.println("the random number "+ ranInt +" add at front");
}
else if (cmd.equals("addAtB"))
{
System.out.println("add at back");
myList.insertAtBack (objInt);
}
else if (cmd.equals("delFrF"))
{
System.out.println("delete from front");
}
else if (cmd.equals("delFrB"))
{
System.out.println("delete from back");
}
else if (cmd.equals("print"))
{
myList.print ();
}
}
public void dataInput(int a)
{
if (myUtility.count ==0)
{
Utility.ranNum[0]=a;
Utility.count = Utility.count+1;
//System.out.println (Utility.ranNum[0]);
}
else
{
int i = Utility.count;
myUtility.ranNum[i]=a;
Utility.count = Utility.count+1;
//System.out.println ("s3");
}
}
}
List.java
import java.*;
public class List
{
private ListNode firstNode;
private ListNode lastNode;
private String name;
public List( String s)
{
name = s;
firstNode = lastNode = null;
}
public List()
{ this("list");}
public void insertAtFront( Object insertItem)
{
if(isEmpty())
firstNode = lastNode = new ListNode( insertItem);
else
firstNode = new ListNode( insertItem, firstNode);
}
public void insertAtBack( Object insertItem)
{
if( isEmpty())
firstNode = lastNode = new ListNode( insertItem);
else
lastNode = lastNode.next = new ListNode( insertItem);
}
public Object delFromFront() throws EmptyListException
{
Object removeItem = null;
if (isEmpty())
throw new EmptyListException(name);
removeItem = firstNode.data;
if( firstNode.equals (lastNode))
firstNode = lastNode = null;
else
firstNode = firstNode.next;
return removeItem;
}
public Object delFromBack() throws EmptyListException
{
Object removeItem = null;
if ( isEmpty())
throw new EmptyListException(name);
removeItem = lastNode.data;
if(firstNode.equals (lastNode))
firstNode = lastNode = null;
else
{
ListNode movePoObject = firstNode;
while( movePoObject.next != lastNode)
movePoObject = movePoObject.next;
lastNode = movePoObject;
movePoObject.next = null;
}
return removeItem;
}
public boolean isEmpty()
{
return (firstNode == null);
}
public void print()
{
if (isEmpty())
{
System.out.println ("Empty" + name);
return;
}
System.out.print ("The" + name + "is: ");
ListNode current = firstNode;
while( current != null)
{
System.out.print(current.data);
current = current.next;
}
System.out.println ();
System.out.println ();
}
class ListNode
{
Object data;
ListNode next;
ListNode( Object o)
{
data = o;
next = null;
}
ListNode( Object o, ListNode nextNode)
{
data = o;
next = nextNode;
}
Object getNode()
{
return data;
}
ListNode getnext()
{
return next;
}
}
class EmptyListException extends RuntimeException
{
public EmptyListException (String name)
{
super("The" + name +"is empty");
}
}
}
Utility.java
public class Utility
{
public static int ranNum[]
={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
public static int count = 0;
public Utility()
{
}
}