目錄
棧
?隊列
鏈表
棧
? ? ? ? 棧數據結構特點:先入棧的數據后出,此數據結構常用的方法有:入棧push、出棧pop、查看棧頂元素peek等,下方示例以數組實現棧結構。
package com.ginko.datastructure;
import lombok.extern.slf4j.Slf4j;/*** 基于數組實現的棧*/
@Slf4j
public class ArrayStack {private int arr[];//棧存放的數據private int top;//棧頂索引/*** 構造棧* @param capacity 棧容量*/public ArrayStack(int capacity) {arr = new int[capacity];top = -1;//初始top為-1}/*** 出棧,從棧頂獲取元素* @return*/public int pop() {if(top == -1) {throw new RuntimeException("棧為空,無法出棧...");}return arr[top--];//出棧后,棧頂下移}/*** 入棧* @param value 入棧數據* @return*/public void push(int value) {if(top == arr.length -1) {throw new RuntimeException("棧已滿,無法入棧...");}arr[++top] = value;//入棧,先將棧頂++}/*** 獲取棧頂數據* @return*/public int peek() {if(top == -1) {throw new RuntimeException("棧為空,沒有棧頂數據...");}return arr[top];}public static void main(String[] args) {ArrayStack arrayStack = new ArrayStack(5);arrayStack.push(100);arrayStack.push(90);arrayStack.push(80);arrayStack.push(60);arrayStack.push(50);log.info("出棧數據:{}",arrayStack.pop()); log.info("出棧數據:{}",arrayStack.pop());log.info("棧頂數據:{}",arrayStack.peek());}
}
? ? ? ? 測試效果如下,復合預期:
隊列
????????隊列數據結構特點:先入隊列的數據先出,此數據結構常用的方法有:入對enQueue、出對deQueue等,下方示例以數組實現隊列結構。
package com.ginko.datastructure;
import lombok.extern.slf4j.Slf4j;/*** 基于數組實現的隊列*/
@Slf4j
public class ArrayQueue {private int arr[];//存放隊列元素private int head;//隊列頭指針private int tail;//隊列尾指針private int size;//隊列里面數據的個數/*** 構造隊列* @param capacity 隊列容量*/public ArrayQueue(int capacity) {arr = new int[capacity];head = 0;tail = -1;size = 0;//隊列沒有數據}/*** 入隊* @param value 加入隊列的值*/public void enQueue(int value) {//隊列滿了就不能入隊if(size == arr.length) {throw new RuntimeException("隊列已滿,無法加入隊列...");}//加入隊列加載數組的末尾arr[++tail] = value;size ++;//隊列數據自增}/*** 出隊,從隊列頭獲取數據*/public int deQueue() {//隊列滿了就不能入隊if(size == 0) {throw new RuntimeException("隊列為空,無法出隊列...");}//從隊列頭獲取數據size--;//隊列數據減少return arr[head++];}public static void main(String[] args) {ArrayQueue arrayQueue = new ArrayQueue(5);arrayQueue.enQueue(10);arrayQueue.enQueue(20);arrayQueue.enQueue(30);arrayQueue.enQueue(40);arrayQueue.enQueue(50);log.info("出隊數據:{}",arrayQueue.deQueue());log.info("出隊數據:{}",arrayQueue.deQueue());log.info("隊列元素個數:{}",arrayQueue.size);}
}
????????測試效果如下,復合預期:
鏈表
? ? ? ? 鏈表數據結構特點:通過前后指針串聯起來完整的數據,包含單向鏈表、雙向鏈表等,下方示例演示單向鏈表,核心方法有鏈表插入元素,刪除元素,遍歷鏈表等。
package com.ginko.datastructure;
import lombok.extern.slf4j.Slf4j;/*** 鏈表,單向鏈表,在鏈表的末尾加入數據*/
@Slf4j
public class LinkList {Node head;//頭節點int size;//鏈表中節點的個數public LinkList() {head = null;size = 0;}/*** 加入鏈表,加入鏈表的末尾* @param value*/public void addElement(int value) {if(head == null) {//加入鏈表頭head = new Node(value,null);size ++;//鏈表中節點的個數return;}//找到此鏈表的末尾節點,遍歷后temp節點就是末尾節點Node temp = head;while (temp.next != null) {temp = temp.next;}//新建末尾節點的下一個節點,然后設置末尾節點Node tailNodeNext = new Node(value,null);temp.next = tailNodeNext;size ++;//鏈表中節點的個數}/*** 刪除鏈表中的元素,核心是找到刪除節點的位置* @param value 要刪除的鏈表數據*/public void delElement(int value) {if(head == null) {throw new RuntimeException("鏈表為空,無法刪除元素...");}//刪除表頭節點if(head.value == value) {//要刪除表頭Node headNext = head.next;//直接將head next設置為表頭就刪除表頭了head = headNext;size--;//鏈表中節點的個數}else {//非表頭節點,需要遍歷整個鏈表,查詢出要刪除的節點的上一個節點tempNode temp = head;while (temp.next != null && temp.next.value != value) {temp = temp.next;}if(temp.next.value == value) {if(temp.next != null) {temp.next = temp.next.next;//跳過要刪除的節點,指向下一個節點}else {temp.next = null;}}size--;//鏈表中節點的個數}}/*** 輸出鏈表節點數據*/public void outputLinkInfo() {Node headNode = head;while (headNode != null) {log.info("節點數據:{}",headNode.value);headNode = headNode.next;}}public static void main(String[] args) {LinkList linkList = new LinkList();linkList.addElement(10);linkList.addElement(20);linkList.addElement(30);linkList.addElement(40);linkList.addElement(50);linkList.outputLinkInfo();log.info("鏈表節點數量:{}",linkList.size);linkList.delElement(20);linkList.delElement(50);linkList.outputLinkInfo();log.info("鏈表節點數量:{}",linkList.size);}
}/*** 鏈表中的節點*/
class Node{int value;//節點的值Node next;//鏈表指針,指向下一個節點public Node(int value,Node next) {this.value = value;this.next = next;}
}
????????測試效果如下,復合預期: