最近在学习数据结构的java表示方法(本来C描述的就学不好,但觉得到时做毕业论文可能有用,所以就再学)
这是一个link-list的例子,半图形界面,但还没有调试成功。
Main.java是运行主类,ListTest.java是图形界面,List.java是链表主代码
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.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
{
private JButton addAtF,addAtB,delFrF,delFrB,print;
private JTextField input;
private JLabel display;
private JPanel p1,p2,p3,p4;
private Container container;
private List myList;
Listener listener;
public void init()
{
Listener listener = new Listener();
List myList = new List();
JTextField input = new JTextField(35);
JLabel 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(listener);
addAtB.addActionListener(listener);
delFrF.addActionListener(listener);
delFrB.addActionListener(listener);
print.addActionListener(listener);
}
class Listener implements ActionListener
{
public void actionPerformed( ActionEvent e)
{
double ran = 100 * Math.random();
int ranInt = (int)ran;
Integer objInt = new Integer(ranInt);
String cmd = e.getActionCommand();
if (cmd.equals("addAtF"))
{
System.out.println("the random number "+ ranInt +" add at front");
myList.insertAtBack( objInt );
}
else if (cmd.equals("addAtB"))
{
System.out.println("add at back");
}
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();
}
}
}
}
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");
}
}
}
本文地址:http://com.8s8s.com/it/it12433.htm