Java实现单链表代码

Java实现简练单链表代码

package 链表;

import javax.print.attribute.standard.NumberOfDocuments;

import static com.sun.tools.classfile.CharacterRangeTable_attribute.Entry.length;
class Node{
    Node next = null;
    int data;

    public Node(int data) {
        this.data = data;
    }
}
public class MyLinkedList {
    Node head = null;

    /**
     * 链表的插入
     * @param d 插入链表的数据
     */
    public void addNode(int d){
        //新建一个节点
        Node newNode = new Node(d);
        //链表为空
        if(null == head){
            head = newNode;
            return;
        }
        //循环找到链表的末尾
        Node tmp = head;
        while (tmp.next!=null){
            tmp=tmp.next;
        }
        tmp.next = newNode;
    }

    /**
     *
     * @param index 链表的索引
     * @return 如果索引小于1或者索引大于链表的长度返回false
     *          删除成功返回true
     */
    public boolean deleteNode(int index){
        if(index<1||index>length()){
            return false;
        }
        //特判删除头指针,因为单向链表没有前驱节点,删除比较简单
        if(index == 1){
            head = head.next;
            return true;
        }
        int i=1;
        Node preNode = head;
        Node curNode = head.next;
        while (curNode!=null){
            //找到索引的位置,将i对应的节点删除,
            //i的前驱节点指向index的后继节点(删除第index个节点)
            if(i==index){
                preNode.next = curNode.next;
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
            i++;
        }
        return true;
    }

    /**
     *
     * @return 返回链表的长度
     */
    public int length(){
        int length = 0;
        Node tmp = head;
        while (tmp.next!=null){
            length++;
            tmp = tmp.next;
        }
        return length;
    }

    /**
     *
     * @return链表为空返回true
     */
    public boolean isEmpty(){
        return null == head?true:false;
    }

    /**
     *
     * @return返回排好序的链表
     */
    public Node orderList(){
        int tmp=0;
        Node curNode = head;
        Node nextNode = null;
        while (curNode.next!=null){
            nextNode = curNode.next;
            //从小到大排序
            while(nextNode!=null){
                if(curNode.data > nextNode.data){
                    tmp = curNode.data;
                    curNode.data = nextNode.data;
                    nextNode.data = tmp;
                }
                nextNode = nextNode.next;
            }
            curNode = curNode.next;
        }
        return head;
    }

    /**
     * 输出链表
     */
    public void printList(){
        Node tmp = head;
        while(tmp!=null){
            System.out.println(tmp.data);
            tmp = tmp.next;
        }
    }

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addNode(10);
        list.addNode(3);
        list.addNode(5);
        list.addNode(1);
        list.addNode(9);
        System.out.println("length is "+list.length());
        System.out.println("before order");
        list.printList();
        list.orderList();
        System.out.println("after order");
        list.printList();
    }
}
阅读更多

没有更多推荐了,返回首页