码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构之链表操作

时间:2018-05-12 13:48:29      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:更改   简单实现   style   code   增删改   实现   ring   color   oid   

 

对链表的增删改查简单实现

public class LinkedList<E> {

    private class Node{
        public Node next;
        public E e;

        public Node(E e){
            this.next = null;
            this.e = e;
        }

        public Node(E e, Node node){
            this.next = node;
            this.e = e;
        }
    }

    private Node head; // 头节点
    private int size;

    public void addFirst(E e){  // 先变node.next的值,然后把node的值赋给head.
        /*Node node = new Node(e);
        node.next = head;
        head = node;*/
        //OR
        Node node = new Node(e,head);
        head = node;
        size++;
    }

    public void addLast(E e){
        Node prev = head;
        Node node = new Node(e);
        while (prev.next != null){
            prev = prev.next;
        }
        prev.next = node;
        size++;
    }

    public void add(int index ,E e){
        if(index < 0 || index > size){
            return;
        }
        Node node = new Node(e);
        if(index == 0){
            node.next = head;
            head = node;
            size++;
        }else{
            Node prev = head;
            for (int i = 0; i < index-1; i++) {
                prev = prev.next;
            }
            node.next = prev.next;
            prev.next = node;
            size++;
        }
    }

    public E remove(int index){
        Node del = new Node(null,null);
        if(index < 0 || index > size){
            return del.e;
        }
        Node prev = head;
        if(index == 0){
            del = head;
            head = del.next;
            size--;
            return del.e;
        }else{
            for (int i = 0; i < index - 1; i++) {
                prev = prev.next;
            }
            del = prev.next;
            prev.next = del.next;
            del.next = null;
            size--;
            return del.e;
        }
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        Node curr = head;
        for (int i = 0; i < size; i++) {
            sb.append(curr.e).append(" --> ");
            curr = curr.next;
        }
        sb.append("NULL");
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.addFirst(1);
        System.out.println(list);
        list.addFirst(2);
        System.out.println(list);
        list.addFirst(3);
        System.out.println(list);
        list.addLast(4);
        System.out.println(list);
        list.addLast(5);
        System.out.println(list);
        list.add(1,6);
        System.out.println(list);
        list.add(2,7);
        System.out.println(list);
        list.add(7,8);
        System.out.println(list);
        list.remove(2);
        System.out.println(list);
        list.remove(0);
        System.out.println(list);
    }

}

对上面实现的改进,增加一个虚拟头结点,方便了增删改查的实现

public class LinkedList2<E> {

    private class Node{
        public Node next;
        public E e;

        public Node(E e){
            this.next = null;
            this.e = e;
        }

        public Node(){
            this.next = null;
            this.e = e;
        }

    }

    private Node dummyHead;
    private int size;

    public LinkedList2(){
        this.dummyHead = new Node();
        this.size = 0;
    }

    public void addFist(E e){
       /* Node node = new Node(e);
        node.next = dummyHead.next;
        dummyHead.next = node;
        size++;*/
        add(0,e);
    }

    public void addLast(E e){
        /*Node node = new Node(e);
        Node prev = dummyHead.next;
        while (prev.next!= null){
            prev = prev.next;
        }
        prev.next = node;
        size++;*/
        add(size,e);
    }

    /**
     * 添加元素到第index位置
     * @param index
     * @param e
     */
    public void add(int index,E e){
        if(index < 0 || index > size){
            return;
        }
        Node node = new Node(e);
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        node.next = prev.next;
        prev.next = node;
        size++;
    }

    /**
     * 得到第index的值
     * @param index
     * @return
     */
    public E get(int index){
        if(index < 0 || index > size){
            return null;
        }
        Node curr = dummyHead.next;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        return curr.e;
    }

    public E getFirst(){
        return get(0);
    }

    /**
     * 移出链表中第index
     * @param index
     * @return
     */
    public E remove(int index){
        if(index < 0 || index > size){
            return null;
        }
        Node del = null;
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        del = prev.next;
        prev.next = del.next;
        del.next = null;
        size--;
        return del.e;
    }

    /**
     * 更改链表中第index个的值
     * @param index
     * @param e
     */
    public void set(int index,E e){
        if(index < 0 || index > size){
            return;
        }
        Node curr = dummyHead.next;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        curr.e = e;
    }

    public String toString(){
        StringBuffer sb = new StringBuffer();
        Node curr = dummyHead.next;
        while (curr!= null){
            sb.append(curr.e).append(" --> ");
            curr = curr.next;
        }
        sb.append("NULL");
        return sb.toString();
    }

    public static void main(String[] args) {
        LinkedList2 list = new LinkedList2();
        list.addFist(1);
        System.out.println(list);
        list.addFist(2);
        System.out.println(list);
        list.addFist(3);
        System.out.println(list);
        list.addLast(4);
        System.out.println(list);
        list.addLast(5);
        System.out.println(list);
        list.add(0,6);
        System.out.println(list);
        list.add(6,7);
        System.out.println(list);
        System.out.println(list.get(1));
        System.out.println(list.get(5));
        list.remove(1);
        System.out.println(list);
        list.remove(5);
        System.out.println(list);
        list.addLast(8);
        System.out.println(list);
        list.set(0,9);
        System.out.println(list);

    }
}

  

 

数据结构之链表操作

标签:更改   简单实现   style   code   增删改   实现   ring   color   oid   

原文地址:https://www.cnblogs.com/zhangcece/p/9022022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!