数据结构之单链表

来源:互联网 发布:思迅v7数据库链接 编辑:程序博客网 时间:2024/05/17 09:16
/* *@date:08-07-06 *@descript:单链表的实现与应用 **/public class LinList{public  Node head;//头指针public  Node current;//当前节点位置private int size;//数据元素个数public LinList()//构造函数{head=current=new Node(null);size=0;}public Node getHead(){return head;}//定位函数public void index(int i) throws Exception{if(i<-1||i>size-1){throw new Exception("参数错误!");}if(i==-1){return;}current=head.next;int j=0;while((current!=null)&&jsize){throw new Exception("参数错误!");}index(i-1);//新节点的next域为current.next,而current的next域为新节点current.setNext(new Node(obj,current.next));size++;}//删除函数public Object delete(int i)throws Exception{if(size==0){throw new Exception("链表元素已空无法删除");}if(i<0||i>size-1){throw new Exception("参数错误!");}index(i-1);Object obj=current.next.getElement();//i-1个节点的next域指向i个节点的next域,让i节点脱链current.setNext(current.next.next);size--;return obj;}//获取数据元素public Object getData(int i)throws Exception{if(i<0||i>size){throw new Exception("参数错误!");}index(i);return current.getElement();}//获取元素个数public int size(){return size;}//判断是否为空public boolean isEmpty(){return size==0;}//主函数public static void main(String[] args)        {    LinList aLinList=new LinList();    LinList bLinList=new LinList();    LinList cLinList=new LinList();    int n=10;    try {    for (int i=0; i
原创粉丝点击