資料結構之鏈表、棧和隊列 java代碼實現

來源:互聯網
上載者:User

標籤:資料結構   鏈表   棧   隊列   

定義抽象節點類Node:

package cn.wzbrilliant.datastructure;/** * 節點 * @author ice * */public abstract class Node {private Node next;public Node(){next=null;}public void setNext(Node nextNode){next=nextNode;}public Node getNext(){return next;}}



鏈表類,實現了插入首尾節點、指定位置節點,刪除節點、指定位置節點,鏈表的逆序以及判空操作:

package cn.wzbrilliant.datastructure;/** * 鏈表 * @author ice * */public class Link {protected Node head;protected Node tail;protected int size;public Link() {this.head = null;this.tail = null;this.size = 0;}public void addAtFirst(Node node){node.setNext(head);head=node;if(size==0)tail=node;size++;}public void addAtLast(Node node){if(size==0){head=tail=node;}else{tail.setNext(node);tail=node;}node.setNext(null);size++;}public void removeFirst(){if(size==0)throw new RuntimeException("link size is 0...");head=head.getNext();if(size==1){tail.setNext(null);}size--;}public void removeLast(){if(size==0)throw new RuntimeException("link size is 0...");if(size==1){head=tail=null;size--;return ;}Node node=head;while(node.getNext()!=tail){node=node.getNext();}node.setNext(null);tail=node;size--;}/** * 隊列逆序 */public void reverse() {Node preNode, node, tempNode;if (size == 0)return;preNode = head;node = preNode.getNext();preNode.setNext(null);tail = preNode;while (node != null) {tempNode = node.getNext();node.setNext(preNode);preNode = node;node = tempNode;}head = preNode;}/** * 在第index個節點後插入newNode *  * @param newNode * @param index */public void insert(Node newNode, int index) {if (index < 0 || index > size)throw new RuntimeException("索引錯誤");if (index == 0) {newNode.setNext(head);head = newNode;size++;return;}if (index == size) {newNode.setNext(null);tail.setNext(newNode);tail = newNode;size++;return;}Node node = head;for (int i = 1; node != null; i++, node = node.getNext()) {if (i == index) {newNode.setNext(node.getNext());node.setNext(newNode);size++;return;}}}/** * 移除Node節點    Node節點需重寫equals()方法 *  * @param node */public void remove(Node node) {if (node == null || size == 0)throw new RuntimeException("remove error...");for (Node temp = head; temp != null; temp = temp.getNext()) {if (temp == head) {if (temp.equals(node)) {head = head.getNext();size--;continue;}}if (temp.getNext().equals(node)) {temp.setNext(temp.getNext().getNext());size--;}}}public Node getFirst() {if (size == 0)return null;return head;}public Node getLast() {if (size == 0)return null;return tail;}public int size() {return size;}public boolean isEmpty() {if (size == 0)return true;return false;}}



棧類,實現了入棧、出戰、擷取棧頂元素以及判空的操作:

package cn.wzbrilliant.datastructure;/** * 棧 * @author ice * */public class Stack {private int size;private Node top;public Stack() {size = 0;top = null;}public void push(Node node) {node.setNext(top);top = node;size++;}public Node pop() {if (top == null)return null;Node node = top;top = top.getNext();size--;return node;}public Node top() {return top;}public int size() {return size;}public boolean isEmpty() {if (size == 0)return true;return false;}}



隊列類,實現了入隊、出隊、判空的操作:

package cn.wzbrilliant.datastructure;/** * 隊列 * @author ice * */public class Queue {private Node head;private Node tail;private int size;public Queue() {this.head = null;this.tail = null;this.size = 0;}public void enQueue(Node node) {tail.setNext(node);tail = node;size++;}public Node deQueue() {if (size == 0)return null;Node node = head;head = head.getNext();size--;return node;}public int size() {return size;}public boolean isEmpty() {if (size == 0)return true;return false;}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

資料結構之鏈表、棧和隊列 java代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.