java封装数组链表

数组MyArrayList

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyArrayList<E> implements Iterable<E> {
    private int size=0;
    private static final int INIT_CAP=1;
    private E[] data;
    private boolean isElementIndex(int index){
        return index>=0 && index<size;
    }
    private boolean isPositionIndex(int index){
        return index>=0 && index<=size;
    }
    private void checkElementIndex(int index){
        if(!isElementIndex(index))
            throw new IndexOutOfBoundsException("Index:"+index+" ,Size:"+size);

    }
    private void checkPositionIndex(int index){
        if(!isPositionIndex(index))
            throw new IndexOutOfBoundsException("Index:"+index+" ,Size:"+size);
    }
    private void resize(int newCap){
        if(size>newCap)
            return;
        else{
            E[] temp=(E[])new Object[newCap];
            for(int i=0;i<size;i++){
                temp[i]=data[i];
            }
//            System.arraycopy(data,0,temp,0,size);
            data=temp;
        }
    }
    public int size(){
        return size;
    }
    public boolean isEmepty(){
        return size==0;
    }
    public E get(int index){
        checkElementIndex(index);
        return data[index];
    }
    public E set(E newVal,int index){
        checkElementIndex(index);
        E oldVal=data[index];
        data[index]=newVal;
        return oldVal;
    }
    public void addLast(E e){
        int cap=data.length;
        if(cap==size){
            resize(2*cap);
        }
        data[size]=e;
        size++;
    }
    public void add(E e,int index){
        checkPositionIndex(index);
        int cap=data.length;
        if(cap==size){
            resize(cap*2);
        }
        System.arraycopy(data,index,data,index+1,size-index);
        data[index]=e;
        size++;
    }
    public void addFirst(E e){
        add(e,0);
    }
    public E removeLast(){
        if(size==0)
            throw new NoSuchElementException();
        int cap=data.length;
        if(size==cap/4){
            resize(cap/2);
        }
        E deletedVal=data[size];
        data[size-1]=null;
        size--;
        return deletedVal;
    }
    public E remove(int index){
        checkElementIndex(index);
        int cap=data.length;
        if(size==cap/4){
            resize(cap/2);
        }
        E deletedVal=data[index];
        System.arraycopy(data,index+1,data,index,size-index-1);
        data[size-1]=null;
        size--;
        return deletedVal;
    }
    public void removeFirst(){
        remove(0);
    }
    public MyArrayList(){
        this(INIT_CAP);
    }
    public MyArrayList(int capacity){
        data=(E[]) new Object[capacity];
    }
    private void display(){
        System.out.println("size:"+size+" ,capacity:"+data.length);
        System.out.println(Arrays.toString(data));
    }
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private int p=0;
            @Override
            public boolean hasNext() {
                return p!=size;
            }

            @Override
            public E next() {
                return data[p++];
            }
        };
    }
    public static void main(String[] args) {
        MyArrayList<Integer> arr=new MyArrayList<>(4);
        System.out.println(arr.isEmepty());
        for(int i=0;i< 5;i++){
            arr.addLast(i+1);
        }
        arr.display();
        arr.add(43,3);
        arr.display();
        arr.addFirst(100);
        arr.display();
        arr.removeLast();
        arr.display();
        arr.remove(2);
        arr.display();
        arr.removeFirst();
        arr.display();
        System.out.println(arr.isEmepty());
        System.out.println(arr.size());
        System.out.println(arr.get(2));
        System.out.println(arr.set(13,1));
        arr.display();
        Iterator <Integer> iterator=arr.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }


}

双链表MyLinkedList

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyLinkedList<E> implements Iterable<E> {
    public MyLinkedList() {
        head=new Node<>(null);
        tail=new Node<>(null);
        head.next=tail;
        tail.prev=head;
        size=0;
    }
    public int size(){
        return size;
    }
    public boolean isEmpty(){
        return size==0;
    }
    private boolean isElementIndex(int index){
        return index>=0 && index<size;
    }
    private boolean isPositionIndex(int index){
        return index>=0 && index<=size;
    }
    private void checkElementIndex(int index){
        if(!isElementIndex(index))
            throw new IndexOutOfBoundsException("Index:"+index+" ,Size:"+size);
    }
    private void checkPositionIndex(int index){
        if(!isPositionIndex(index)){
            throw new IndexOutOfBoundsException("Index:"+index+" ,Size:"+size);
        }
    }
    public void display(){
        System.out.println("Size:"+size);
        for(Node<E>p=head.next;p!=tail;p=p.next){
            System.out.print(p.val+"->");
        }
        System.out.println("null");
        System.out.println();
    }
    public void addLast(E e){
        Node<E> x=new Node<>(e);
        Node<E> temp=tail.prev;
        x.next=tail;
        x.prev=temp;
        temp.next=x;
        tail.prev=x;
        size++;
    }
    public void addFirst(E e){
        Node<E> x=new Node<>(e);
        Node<E> temp=head.next;
        x.next=temp;
        x.prev=head;
        head.next=x;
        temp.prev=x;
        size++;
    }
    public void add(E e,int index){
        checkPositionIndex(index);
        if(size==index){
            addLast(e);
            return;
        }
        Node<E> x=new Node<>(e);
        Node<E> temp=getNode(index);
        Node<E> prevNode=temp.prev;
        x.prev=prevNode;
        x.next=temp;
        prevNode.next=x;
        temp.prev=x;
        size++;
    }
    public E removeLast(){
        if(size==0)
            throw new NoSuchElementException();
        Node<E> x=tail.prev;
        Node<E> temp=x.prev;
        temp.next=tail;
        tail.prev=temp;
        x.next=x.prev=null;
        size--;
        return x.val;
    }
    public E removeFirst(){
        if(size==0)
            throw new NoSuchElementException();
        Node<E> x=head.next;
        Node<E> temp=x.next;
        head.next=temp;
        temp.prev=head;
        x.prev=x.next=null;
        size--;
        return x.val;
    }
    public E remove(int index){
        checkElementIndex(index);
        Node<E> x=getNode(index);
        Node<E> prevNode=x.prev;
        Node<E> nextNode=x.next;
        prevNode.next=nextNode;
        nextNode.prev=prevNode;
        x.prev=x.next=null;
        size--;
        return x.val;
    }
    public E getFirst(){
        if(isEmpty())
            throw new NoSuchElementException();
        return head.next.val;
    }
    public E getLast(){
        if(isEmpty())
            throw new NoSuchElementException();
        return tail.prev.val;
    }
    public E get(int index){
        checkElementIndex(index);
        Node<E>p=getNode(index);
        return p.val;
    }
    public E setFirst(E e){
        if(size==0)
            throw new NoSuchElementException();
        E oldVal=head.next.val;
        head.next.val=e;
        return oldVal;
    }

    public E setLast(E e){
        if(size==0)
            throw new NoSuchElementException();
        E oldVal=tail.prev.val;
        tail.prev.val=e;
        return oldVal;
    }
    public E set(E e,int index){
        checkElementIndex(index);
        Node<E> p=getNode(index);
        E oldVal=p.val;
        p.val=e;
        return oldVal;
    }
    private Node<E> getNode(int index){
        checkElementIndex(index);
        Node<E> p;
        if(index<(size>>2)){
            p=head.next;
            for(int i=0;i<index;i++){
                p=p.next;
            }
        }else{
            p=tail.prev;
            for(int i=size-1;i>index;i--){
                p=p.prev;
            }
        }
        return p;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private Node<E>p=head.next;
            @Override
            public boolean hasNext() {
                return p!=tail;
            }

            @Override
            public E next() {
                E val=p.val;
                p=p.next;
                return val;
            }
        };
    }

    private static class Node<E>{
        Node<E> prev,next;
        E val;
        Node(E val){
            this.val=val;

        }
    }
    private final Node<E> head,tail;
    private int size;

    public static void main(String[] args) {
        MyLinkedList<Integer>arr=new MyLinkedList<>();
        arr.addLast(12);
        arr.addLast(32);
        arr.addLast(-10);
        arr.addLast(19);
        arr.addLast(-40);
        arr.addLast(29);
        arr.addLast(-70);
        arr.addLast(98);
        arr.addLast(-170);
        arr.addLast(928);
        arr.display();
        arr.remove(5);
        arr.display();

//        System.out.println(arr.getFirst()+", "+ arr.getLast()+" ,"+ arr.get(2));
//
//        arr.addFirst(21);
//        arr.addFirst(100);
//        arr.display();
//        arr.add(11,6);
//        arr.display();
//
//        System.out.println(arr.setFirst(102)+", "+arr.setLast(31)+", "+arr.set(44,5));
//        arr.display();
//
//        System.out.println(arr.removeLast());
//        arr.display();
//        System.out.println(arr.removeFirst());
//        arr.display();
//        System.out.println(arr.remove(1));
//        arr.display();
//        System.out.println(arr.remove(0));
//        arr.display();
//        System.out.println(arr.remove(2));
//        arr.display();
//        Iterator<Integer> iterator=arr.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }

    }

}


单链表MySingleLinkedList

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MySingleLinkedList<E> implements Iterable<E>{
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private Node<E>p=head.next;
            @Override
            public boolean hasNext() {
                return p!=null;
            }

            @Override
            public E next() {
                E val=p.val;
                p=p.next;
                return val;
            }
        };
    }

    private static class Node<E>{
        Node<E> next;
        E val;
        Node(E val){
            this.val=val;
            this.next=null;
        }
    }
    private final Node<E> head;

    private int size;
    public MySingleLinkedList(){
         head=new Node<>(null);
         size=0;
     }
     public int size(){
        return size;
     }

     public boolean isEmepty(){
        return size==0;
     }
     private boolean isElementIndex(int index){
        return index>=0 && index<size;
     }
    private boolean isPositionIndex(int index){
        return index>=0 && index<=size;
    }
    private void checkElementIndex(int index){
        if(!isElementIndex(index))
            throw new IndexOutOfBoundsException("Size:"+size+" ,Index:"+index);
    }
    private void checkPositionIndex(int index){
        if(!isPositionIndex(index)){
            throw new IndexOutOfBoundsException("Size:"+size+" ,Index:"+index);
        }
    }
    public void addLast(E e){
        Node<E> x=new Node<>(e);
        if(size!=0){
            Node<E>p=getNode(size-1);
            p.next=x;
        }else{
            head.next=x;
        }
        size++;
    }
    public void addFirst(E e){
        Node<E>x=new Node<>(e);
        x.next=head.next;
        head.next=x;
        size++;
    }
    public void add(E e,int index){
        checkPositionIndex(index);
        if(size==index){
            addLast(e);
            return;
        }
        Node<E>x=new Node<>(e);
        Node<E>p=getNode(index);
        E pval=p.val;
        p.val=x.val;
        x.val=pval;
        x.next=p.next;
        p.next=x;
        size++;
    }
    public E removeFirst(){
        if(size==0)
            throw new NoSuchElementException();
        Node<E>p=head.next;
        head.next=p.next;
        p.next=null;
        E deletedVal=p.val;
        size--;
        return deletedVal;
    }
    public E removeLast(){
        if(size==0)
            throw new NoSuchElementException();
        if(size==1){
            return removeFirst();
        }
        Node<E>p=getNode(size-2);
        E deletedVal=p.next.val;
        p.next=null;
        size--;
        return deletedVal;
    }
    public E remove(int index){
        checkElementIndex(index);
        if(index==0)
            return removeFirst();
        else if (index==size-1) {
            return removeLast();
        }else {
            Node<E>p=getNode(index-1);
            E deletedVal=p.next.val;
            p.next=p.next.next;
            size--;
            return deletedVal;
        }
    }
    public E getFisrt(){
        if(isEmepty())
            throw new NoSuchElementException();
        return head.next.val;
    }
    public E getLast(){
        if(isEmepty())
            throw new NoSuchElementException();
        Node<E>p=getNode(size-1);
        return p.val;
    }
    public E get(int index){
        checkElementIndex(index);
        Node<E>p=getNode(index);
        return p.val;
    }

    public E setFisrt(E e){
        if(isEmepty())
            throw new NoSuchElementException();
        E oldVal=head.next.val;
        head.next.val=e;
        return oldVal;
    }

    public E setLast(E e){
        if(isEmepty())
            throw new NoSuchElementException();
        E oldVal=getLast();
        Node<E>p=getNode(size-1);
        p.val=e;
        return oldVal;
    }

    public E set(E e,int index){
        checkElementIndex(index);
        Node<E>p=getNode(index);
        E oldVal=p.val;
        p.val=e;
        return oldVal;
    }

    public Node<E> getNode(int index){
        checkElementIndex(index);
        Node<E> p=head.next;
        for(int i=0;i<index;i++){
            p=p.next;
        }
        return p;
    }
    public void display(){
        if(size!=0){
            Node<E>p=head.next;
            System.out.println("Size:"+size);
            while (p!=null){
                System.out.print(p.val+"->");
                p=p.next;
            }
        }
        System.out.println("null");
        System.out.println();

    }

    public static void main(String[] args) {
        MySingleLinkedList<Integer>arr=new MySingleLinkedList<>();
        arr.addLast(12);
        arr.display();
        System.out.println(arr.removeLast());
        arr.display();
        arr.addLast(13);
        arr.display();
        arr.addFirst(-9);
        arr.addFirst(32);
        arr.display();
        arr.add(42,2);
        arr.display();
        arr.add(37,0);
        arr.display();
        arr.add(89,5);
        arr.display();
        System.out.println(arr.removeFirst());
        arr.display();
        System.out.println(arr.removeLast());
        arr.display();
        System.out.println(arr.remove(2));
        arr.display();
        arr.addFirst(15);
        arr.addFirst(24);
        arr.display();
        System.out.println(arr.getFisrt()+" ,"+arr.getLast()+" ,"+arr.get(2));
        System.out.println(arr.setFisrt(22)+" ,"+arr.setLast(-199)+" ,"+arr.set(29,3));
        arr.display();
        Iterator<Integer> iterator=arr.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值