我的第一篇博客——自写MyLinkedList类

来源:互联网 发布:μcos ii源码v2.86 编辑:程序博客网 时间:2024/05/21 18:38

MyLinkedList:
首先, 我们先建立一个节点(Node)类,内容包括节点的前一节点pre,该节点的包含的元素obj以及指向的下一个节点next,下面是Node类的私有成员与构造函数。

class Node {    private Node pre;    private Object obj;    private Node next;    public Node(){}    public Node(Node pre, Object obj, Node next){        super();        this.pre = pre;        this.obj = obj;        this.next = next;    }}

然后是Node类的setter和getter方法。

pubilc void setPre(Node pre){    this.pre = pre;}public Node getPre(){    return this.pre;}pubilc void setObj(Object obj){    this.obj = obj;}public Object getObj(){    return this.obj;}pubilc void setNext(Node next){    this.next = next;}public Node getNext(){    return this.next;}

MyLinkedList的实例本质上就是双向链表,故含有链头(head)和链尾(tail)。

public class MyLinkedList {    private Node head;    private Node tail;    private int size = 0;    public MyLinkedList(){}    int size(){        return size;    }    //主要成员函数    //增加元素    public boolean addFirst(Object obj){}    public boolean add(Object obj){}    public boolean add(int index, Object obj){}    //找出元素    public Object get(int index){}    //删除元素    public boolean remove(Object obj){}    public boolean remove(int index){}    //修改元素    public boolean replace(Object obj, Object newObj){}    public boolean replace(int index, Object newObj){}    //一些帮助函数    public void rangeCheck(int index){}    public Node findNode(int index){}}

帮助函数。

public void rangeCheck(int index){    if(index<0 || index>=size)        throw new IndexOutOfBoundsException("IndexOutOfBounds!");}public Node findNode(int index){    Node temp = null;    if(first != null){        temp = first;        for(int i=0;i<index;i++){            temp = temp.getNext();          }    }    return temp;}

在列表头加入元素。

public boolean addFirst(Object obj){    Node newNode = new Node();    if(first == null){        newNode.setPre(null);        newNode.setObj(obj);        newNode.setNext(null);        head = newNode;        tail = newNode;    } else {        newNode.setPre(null);        newNode.setObj(obj);        newNode.setNext(head);        head.setPre(newNode);        head = newNode;    }    size++;    return true;}

在列表尾部加入元素。

public boolean add(Object obj){    Node newNode = new Node();    if(first == null){        newNode.setPre(null);        newNode.setObj(obj);        newNode.setNext(null);        head = newNode;        tail = newNode;    } else {        newNode.setPre(tail);        newNode.setObj(obj);        newNode.setNext(null);        tail.setNext(newNode);        tail = newNode;    }    size++;    return true;}

在列表中指定位置加入元素。

public boolean add(int index, Object obj){    rangeCheck(index);    if(index == size){        add(obj);        return true;    }    Node temp = findNode(index);    Node newNode = new Node();    if(temp == first){        temp.getPre.setNext(newNode);        newNode.setPre(temp.getPre);        newNode.setObj(obj);        newNode.setNext(temp);        temp.setPre(newNode);    }    return true;}

返回索引index位置的元素。

public Object get(int index){    rangeCheck(index);    Node temp = findNode(index);    if(temp != null)        return temp.obj;    return null;}

删除与obj相同的元素。

public boolean remove(Object obj){    for(Node temp=head;temp != null;temp = temp.getNext()){            if(temp.getObj().equals(obj)){                if(temp == head){                               head = temp.getNext();                }else if(temp == tail){                         last = temp.getPre();                }else{                    temp.getPre().setNext(temp.getNext());                    temp.getNext().setPre(temp.getPre());                }                size--;            }        }        return true;}

删除指定索引位置的元素。

public boolean remove(int index){    rangeCheck(index);    Node temp = findNode(index);    if(temp == head){        head.getNext().setPre(null);        head = head.getNext();    }else if(temp == tail){        tail.getPre().setNext(null);        tail = last.getPre();    }else{        temp.getNext().setPre(temp.getPre());        temp.getPre().setNext(temp.getNext());    }    size--;    return true;}

将指定元素代替为给定元素。

public boolean replace(Object obj, Object newObj){    for(int i=0;i<size;i++){        Node temp = findNode(i);        if(temp.getObj().equals(obj)){            temp.setObj(newObj);        }    }    return true;}

将指定索引位置的元素替换为给定元素。

public boolean replace(int index, Object obj){    rangeCheck(index);    Node temp = findNode(index);    temp.setObj(obj);    return true;}

本文仅仅写出了LinkedList的最基本的几个函数,其余需要的函数具体可查看API文档。文中代码部分直接复制粘贴的MyEclipse中的代码,部分变量粘贴做出了一些改变,可能部分没有注意到的地方会有一些问题,如有发现,还请指正。

0 0
原创粉丝点击