標籤:資料結構 鏈表 棧 隊列
定義抽象節點類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代碼實現