单向链表的构造

public class LinkList {

    private Node headNode;

    public void createLinked( int[] a ){

        Node tailNode = new Node();//上一节点

        headNode = tailNode;//第一个节点是当前结点

        Node newNode;//当前节点

        for( int i = 0; i < a.length; i++ )
        {

            newNode = new Node();
            newNode.setValue( a[i] );
            tailNode.setNextNode(newNode);
            newNode.setNextNode(null);
            tailNode = newNode;
        }

    }

    public void searchLink( int value ){

        if( headNode == null )
            return;

        Node nextNode = headNode.getNextNode();

        while( nextNode != null )
        {
            if( nextNode.getValue() == value )
            {
                System.out.println("find the element");
                break;
            }
            nextNode = nextNode.getNextNode();
        }
        if( nextNode == null )
        {
            System.out.println("not found the element");
        }

    }

    public void deleteLink( int value ){

        if( headNode == null )
            return;

        Node preNode = headNode;
        Node nextNode = headNode.getNextNode();
        while(nextNode != null)
        {

            if( nextNode.getValue() == value )
            {
                preNode.setNextNode(nextNode.getNextNode());
                System.out.println("delete the element success");
                break;
            }
            preNode = nextNode;
            nextNode = nextNode.getNextNode();
        }
        if( nextNode == null )
        {
            System.out.println("not found the delete element");
        }

    }

    public void insertLink(int pos, int value){

        if( headNode == null )
            return;
        int count = 0;
        boolean isSuccess = false;
        Node currentNode = headNode;
        Node nextNode;

        while(currentNode != null)
        {
            nextNode = currentNode.getNextNode();
            if( pos == count )
            {
                Node insertNode = new Node();
                insertNode.setValue(value);
                currentNode.setNextNode(insertNode);
                insertNode.setNextNode(nextNode);
                isSuccess = true;
                break;
            }
            currentNode = nextNode;
            count++;
        }
        if( isSuccess )
        {
            System.out.println("insert the element success");
        }else{
            System.out.println("insert the element failed");
        }

    }

    public void reverseLink(){

        if( headNode == null )
            return;
        Node preNode = headNode;
        Node currentNode = headNode.getNextNode();
        Node tempNode = currentNode;

        while(currentNode != null)
        {
            Node next = currentNode.getNextNode();
            currentNode.setNextNode(preNode);
            preNode = currentNode;
            currentNode = next;
        }
        tempNode.setNextNode(null);
        headNode.setNextNode(preNode);
    }

    public void displayLink(){

        if( headNode == null )
            return;
        Node nextNode = headNode.getNextNode();

        System.out.print("head ->");

        while( nextNode != null )
        {
            System.out.print( nextNode.getValue() + " ->" );
            nextNode = nextNode.getNextNode();
        }
        System.out.println("null");

    }

    public static void main(String[] args) {  
        int a[] = new int[10];  
        for (int i = 0; i < a.length; i++) {  
            a[i] = i * 2;  
        }  
        LinkList list = new LinkList();  
        list.createLinked(a);  
        list.displayLink();

        list.searchLink(20);

        list.deleteLink(0);

        list.displayLink();

        list.insertLink(0, 1);

        list.displayLink();

        list.reverseLink();

        list.displayLink(); 

    }

}

class Node{

    private Node nextNode;

    private int value;

    public Node getNextNode() {
        return nextNode;
    }

    public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值