JAVA实现简单链表操作

最近拾起数据结构和算法,特此开博,记录一下,希望坚持下去

Java语言中的对象引用实际上是一个指针,所以我们可以编写这样的类来实现链表中的结点。

class Node 
  { 
  Object tData; 
  Node next;//指向下一个结点 
  } 

将数据域定义成Object类是因为Object类是广义超类,任何类对象都可以给其赋值,增加了代码的通用性。为了使链表可以被访问还需要定义一个表头,表头必须包含指向第一个结点的指针和指向当前结点的指针。

写了个简单例子,实现了单链表的一些简单功能

public class SingleLinkList {
	
	//节点
	class Node {
		public Object tData;
		public  Node next;
		
		Node(Object data){
			this.tData = data;
		}
		public void display() {  
	        System. out.print( tData + " ");  
	   } 
	}
	
	public Node first;
	public int nodeIndex;

	SingleLinkList() {
		this.first = null;
	}
	//显示所有节点
	public void display(){  
        if(first == null)  
            System.out.println("NULL");  
        Node cur = first;  
        while(cur != null){  
            System.out.print(cur.tData.toString() + " -> ");  
            cur = cur.next;  
        }  
        System.out.print("\n");  
    }  
	
	//链表长度
	public int getListLength(){  
        int len=0;  
        Node cur = first;
        while(cur!=null){  
            len++;  
            cur=cur.next;  
        }  
        return len;  
    }  
	//将单链表反转,循环  
    public Node reverseList(){  
    	Node cur = first;
        if(cur==null||cur.next==null) return cur;  
        Node pre=null;  
        Node nex=null;  
        while(cur!=null){  
            nex=cur.next;  
            cur.next=pre;  
            pre=cur;  
            cur=nex;  
        }  
        return pre;  
    }  
    //将单链表反转,递归  
    public  Node reverseListRec(){  
    	Node cur = first;
        if(cur==null||cur.next==null)return cur;  
        Node reHead=reverseListRec();  
        cur.next.next=cur;  
        cur.next=null;  
        return reHead;  
    }  
	//插入头节点
	public void insertFirstList(Object obj) {
		Node node = new Node(obj);
		node.next = first;
		first = node;
	}
	//删除头节点,返回一个头节点
	public Node deleteFirstNode() throws Exception {
		if(first == null)  
            throw new Exception("NULL"); 
		Node tempNode = first;
		first = tempNode.next;
		return tempNode;
	}
    //指定位置增加节点
	public void add(int index, Object data) {
		int pos = 0;
		Node node = new Node(data);
		Node current = first;
		Node previous = first;
		while (pos != index) {
			previous = current;
			current = current.next;
			pos++;
		}
		node.next = current;
		previous.next = node;
	}
	
	//根据data查找节点信息
	public Object find(Object obj) throws Exception{  
        if(first == null)  
            throw new Exception("LinkedList is empty!");  
        Node cur = first;  
        while(cur != null){  
            if(cur.tData == obj){  
                return cur.tData;  
            }  
            cur = cur.next;  
        }  
        return null;  
    }  
	//根据位置查找节点信息
	public Object find(int index) throws Exception{  
        int pos = 0;  
        Node cur = first;  
        while(pos != index){  
        	cur = cur.next; 
            pos++;
        }  
        return cur;  
    }  
	
	//删除指定节点信息
	public Node deleteNode(int index) throws Exception {

		int pos =0;
		Node cur = first;
		Node pre = first;
		while(pos != index){
			pre = cur;
			cur = cur.next;
			pos++;
		}
		
		if(cur == first) {  
           first = first. next;  
       } else {    
           pre.next = cur.next;  
       }  
		return cur;
	}
	
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		SingleLinkList  sl = new SingleLinkList();
		
		sl.insertFirstList(111);
		sl.insertFirstList(222);	
		sl.insertFirstList(333);	
		sl.display();	
		sl.deleteFirstNode();
		sl.display();
		sl.insertFirstList(444);
		sl.add(2, 888);
		sl.display();
		sl.deleteNode(2);
		sl.display();
		Node d = (Node) sl.find(2);
		System.out.println(d.tData.toString());
		;
		
	}
	
}

 

和数组相比,链表的优势在于长度没有限制,并且在进行插入和删除操作时,不需要移动数据项,效率上比数组要高很多

劣势在于随机访问,无法像数组那样直接通过下标找到特定的数据项

posted @ 2016-12-20 15:29  芝麻未来  阅读(181)  评论(0)    收藏  举报