【java基础】简单的链表

  链表最基本的数据结构:1、包含一个存储数据的单元;2、还必须存储指向下一元素的信息。由以上两部分组成的存储映像成为节点,N个节点连在一起就是链表。

首先定义一个节点:

/**
 * 自定义一个节点类
 */
class Node {
    // 本节点指向的下一个节点(单链表只包含下一节点)
    Node next = null;
    // 本节点存储的数据
    int data;

    public Node(int data) {
        this.data = data;
    }
}

当节点只包含其后继节点的信息,链表就称之为单链表。定义一个链表,实现链表简单的数据添加,删除排序操作。

/**
 * 自定义一个链表类
 */
class MyLinkedList {
    // 链表的头
    Node head = null;

    /**
     * 向列表中插入数据
     * 
     * @param d
     *            数据内容
     */
    public void addNode(int d) {
        Node newNode = new Node(d);
        if (head == null) {
            head = newNode;
            return;
        }
        Node tmp = head;
        while (tmp.next != null) {
            tmp = tmp.next;
        }
        tmp.next = newNode;
    }

    /**
     * 删除第index个节点
     * 
     * @param index
     * @return 成功返回true,失败返回false
     */
    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 = preNode.next;
        while (curNode != null) {
            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 != null) {
            length++;
            tmp = tmp.next;
        }
        return length;
    }

    /**
     * 对链表进行排序,返回排序后的头节点
     */
    public Node orderList() {
        Node nextNode = null;
        int temp = 0;
        Node curNode = head;
        while (curNode.next != null) {
            nextNode = curNode.next;
            while (nextNode != null) {
                if (curNode.data > nextNode.data) {
                    temp = curNode.data;
                    curNode.data = nextNode.data;
                    nextNode.data = temp;
                }
                nextNode = nextNode.next;
            }
            curNode = curNode.next;
        }
        return head;
    }

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

测试链表:

public class TestMyLinkedList {
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addNode(5);
        list.addNode(3);
        list.addNode(4);
        list.addNode(1);
        list.addNode(2);
        System.out.println("list.lenght = " + list.length());
        System.out.println("----------before order --------------------------------------");
        list.printList();
        list.orderList();
        System.out.println("\n----------after order ---------------------------------------");
        list.printList();
        
    }
}

测试输出结果如下:

list.lenght = 5
----------before order --------------------------------------
5 3 4 1 2 
----------after order ---------------------------------------
1 2 3 4 5

  

猜你喜欢

转载自www.cnblogs.com/illegalwang/p/9510519.html
今日推荐