cfcccvc
发贴: 2
积分: 0
|
于 2005-11-27 22:29
各位大虾好,小弟初学java数据结构,编了一个链表的程序,却苦于无法运行,望各位给检查检查,谢谢啦!!
import java.io.*; import java.applet.Applet; public class LinkedListApplet extends Applet{ public void init() { LinkedList=new LinkedList(); L.insertNewLastNode("DUS"); L.insertNewLastNode("ORD"); L.insertNewLastNode("SAN"); L.print(); L.insertNewSecondNode("BRU"); L.print(); L.deleteLastNode(); L.print(); } } //ListNode类定义 public class ListNode{ String airport; ListNode link; } //LinkedList类定义 public class LinkedList{ int length; ListNode firstNode; public int size(){ return length; } insertNewSecondNode(airportCode) listSearch(airportCode) deleteLastNode() insertNewLastNode(airportCode) print() } //插入新的次节点的方法 public void instertNewSecondNode(String airportCode){ if(length==0) return; ListNode newNode=new ListNode(); newNode.airport=airportCode; newNode.link=firstNode.link; firstNode.link=newNode; return airportCode; length++; } //搜索 public ListNode listSearch(String airportCode){ ListNode N; N=firstNode; while(N!=null){ if(airportCode.equals(N.airport){ return N; } else { N=N.link; }
return N; } //删除链表末端节点 public void deleteLastNode(){ ListNode PreviousNode.currentNode; if(firstNode!=null){ if(firstNode.link++null){ firstNode=null; length--; }else{ previousNode=firstNode; currentNode=firstNode.link; while(currentNode.link !=null){ previousNode=currentNode; currentNode=currentNode.link; } previousNode.link=null; length--;; } } } //插入新的末端节点 public void insertNewLastnode(String airportCode){ ListNode N= new ListNode(); N.airport=airportCode; N.link=null; if(firstNode==null){ firstNode=N; }else{ ListNode P=firstNode; while(P.link!=null){ P=P.link; } P.link=N; } length++; } //打印链表 public void print(){ ListNode N; System.out.print("("); N=firstNode; while(N!=null){ System.out.print(N.airport); N=N.link; if(N!=null){ System.out.print(","); } } System.out.println(")");
新手上路,请多指教!
|