link-list java版

最近在學習數據結構的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<myUtility.count;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()
  {
        
  }   
}

發佈了22 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章