复习数据结构—单链表

 抽空复习并实战了数据结构—单链表,在此记录一下。

package com.dev.dontworry;

/**
 * Created by 邓耀宁 on 2018/2/5.
 */

public class MyLinkedList {
    private Node head;
    private int size = 0;

    public int size(){
       return size;
    }

    static class Node{
        private int data;
        private Node next;
        public Node(int data){
            this.data = data;
        }

        public Node next(){
            return next;
        }

        public void setNext(Node next){
            this.next = next;
        }
    }

    public boolean add(Node newNode){
        if(newNode == null){
            return false;
        }
        if(head == null){
            head = newNode;
        }else {
            Node tmp = head;
            while (tmp.next() != null) {
                tmp = tmp.next();
            }
            tmp.setNext(newNode);
        }
        size++;
        return true;
    }

    public boolean add(Node newNode, int position){
        if(newNode == null || position < 1 || position > size){
            return false;
        }
        if(head == null){
            head = newNode;
        }else {
            Node tmp = head;
            int count = 1;
            if (position == 1) {
                newNode.setNext(tmp);
                head = newNode;
                size++;
                return true;
            }
            while (count < position - 1) {
                count++;
                tmp = tmp.next();
            }
            newNode.setNext(tmp.next());
            tmp.setNext(newNode);
        }
        size++;
        return true;
    }

    public boolean remove(int position){
        if(position < 1 || position > size){
            return false;
        }
        int count = 1;
        Node tmp = head;
        if(position == 1){
            head = tmp.next();
            tmp.setNext(null);
            size--;
            return true;
        }
        while(count < position - 1){
            count++;
            tmp = tmp.next();
        }
        tmp.setNext(tmp.next().next());
        size--;
        return true;
    }

    @Override
    public String toString() {
        String info = "size = " + size + ",[";
        Node tmp = head;
        while(tmp != null){
            if(tmp.next() != null) {
                info += tmp.data + " -> ";
                tmp = tmp.next();
            }else{
                info += tmp.data;
                break;
            }
        }
        info += "]";
        return info;
    }

    public static void main(String[] args){
        MyLinkedList linkedList = new MyLinkedList();
        linkedList.add(new Node(3));
        linkedList.add(new Node(4));
        linkedList.add(new Node(7));
        linkedList.add(new Node(2));
        linkedList.add(new Node(1), 1);
        linkedList.add(new Node(5));
        linkedList.remove(1);
        System.out.println(linkedList.toString());
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值