//单链表的节点类publicTS Class Node{Object data;Node next;
//构造丽数
Node (Nodenextval) {next=nextval;
//数据元素
//表示下一个节点的对象引用
//用于头节点的构造函数1
Node (Object obj,Node nextval){data=obj;
next=nextval;
//用于其他节点的构造函数2
public object getElement() {return data;
/ /取data
public void setElement (Object obj) {//置data data=obj;
public String toString(){return data. toString();
//转换data为String类型
//线性表抽象数据类型的接口定义
public interface List {
public void insert (int i,Object obj) throws Exception; //在i位置插入元素public Object delete (int i) throws Exception;
//删除i位置的元素
public object getData(int i) throws Exception;
//取i位置的数据元素
public void print () throws Exception;
//输出单链表的数据元素
public int size() ;
//求元素个数
public boolean isEmpty();
//确定是否为空
//单链表类的设计
public class LinList implements ListNode head;
Node current;
int size;
LinList() {
head=current= new Node (null);
public void index (int 1) throws Exception{if(i<-111 i >size-1) {
throw new Exception(“参数错误!”);
if(i==-1) {
current= head;return;
current= head. next;
int j=0;
wl(lurrent 1nu11)68」<1)(current-current.next;
j++;
public void insert (int i,Object obj) throws Exceptionfif(i<011 i>size) i
throw new Exception(" ##i!");
index(i-1) ;
Node s=new Node (obj,null) ;s.next=current .next;
current.next=s;
size++;
//分配新节点
public object delete(int i) throws Exception [
if (size==0) [
throw new Exception(“fxeà iT JH!”);
throw new Exceptlo
index(i -1);
Objectobj- cu
current nextcurrent,next -next,
nt0;
size --;
return obj;
public int size()lreturn size;
public void print()thrtows ExceptionlSystem . out .println("ff
for(int i=0;i <size; i++)1첷휴서:");System. out ·print (gtDatali±
");
public boolean isEmpty()treturn size==0;
public object getData(int i) throws Exception(if(i<-1lli >size -1) l
throw new Exception(“参数错误!”);
index(i) ;
return current .getElement0li
//建立个线性表,依次输人数据元素1.2. 3…
publicic class LinListTestf、10,删除元紫 5,最后显示当前元素public static void main (String argsllll
LinList linList=new LinList (Y :
int n=10;
tryl
for(inti=0;i<n;i++)l
1 inList. insert (i,new Integer (i+1)) ;
1 inList.print0;
//删除第5个元素
linList.delete(4);Syvstem. out .println(;system .out ,print (副除后”:1inList.print)
catch (Exception e)e . getMessagel)liSystem.ouut .println