java學習筆記—自訂實現linkedList集合

來源:互聯網
上載者:User
/* * 自訂實現linkedList集合結構 */public class MyLinkedList {private Node firstNode; // 永遠指向第一個元素private Node lastNode; // 永遠指向最後一個元素private int size; //集合的長度// 添加元素public boolean add(Node node) {if (null == node) {throw new IllegalArgumentException("不允許null的Node元素");}if (null == firstNode) {firstNode = node; // 集合中沒有元素,就把該元素賦值給第一個元素} else {Node lastNode = getLast();//雙向鏈表lastNode.setNext(node); //指向的下一個元素node.setPrev(lastNode); //指向的上一個元素}lastNode=node; //把新添加的元素賦值給最後一個元素size++; //每添加一次長度加1return true;}//在列表的首部添加元素public void addFirst(Node node){if(node == null){throw new IllegalArgumentException("不允許null的Node元素");}if(firstNode == null){lastNode=node; }else{Node n=getFirst();n.setPrev(node);node.setNext(n);}firstNode=node;size++;}//在列表的末尾添加元素public void addLast(Node node){if(node == null){throw new IllegalArgumentException("不允許null的Node元素");}if(lastNode == null){firstNode=node; }else{Node n=getLast();n.setNext(node);node.setPrev(n);}lastNode=node;size++;}//刪除列表中第一個元素public void removeFirst(){Node node=firstNode.getNext();node.setPrev(null);firstNode=node;size--;}//刪除列表中最後一個元素public void removeLast(){Node node=lastNode.getPrev();node.setNext(null);lastNode=node;size--;}//刪除列表中指定的元素public void remove(int index){if(index < 0 || index >= size){throw new ArrayIndexOutOfBoundsException();}if (null == firstNode) {throw new IllegalStateException("集合中沒有元素");}int i=0;Node node=firstNode;if(index == 0){node=node.getNext();node.setPrev(null);firstNode=node;size--;return;}if(index == (size-1)){node=lastNode.getPrev();node.setNext(null);lastNode=node;size--;return;}if(index<size/2){while(i!=index){node=node.getNext();i++;}}else{i=size-1;node=lastNode;while(i!=index){node=node.getPrev();i--;}}Node next=node.getNext();Node prev=node.getPrev();next.setPrev(prev);prev.setNext(next);size--;}/* * 返回此列表中指定位置處的元素(二分法) */public Node get(int index) {if(index < 0 || index >= size){throw new ArrayIndexOutOfBoundsException();}if (null == firstNode) {throw new IllegalStateException("集合中沒有元素");}int i=0;Node node;if(index<size/2){node=firstNode;while(i!=index){node=node.getNext();i++;}}else{i=size-1;node=lastNode;while(i!=index){node=node.getPrev();i--;}}return node;}/* * 返回此列表中指定位置處的元素(普通方法) */public Node myGet(int index) {if(index < 0 || index >= size){throw new ArrayIndexOutOfBoundsException();}if (null == firstNode) {throw new IllegalStateException("集合中沒有元素");}int i=0;Node node=firstNode;while(i!=index){node=node.getNext();i++;}return node;}/* * 擷取最後一個元素 */public Node getLast() {if (null == firstNode) {throw new IllegalStateException("集合中沒有元素");}return lastNode;}/* * 擷取最後一個元素 */public Node getFirst() {if (null == firstNode) {throw new IllegalStateException("集合中沒有元素");}return firstNode;}/* * 擷取集合長度 */public int size() {return size;}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.