Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊(duì)列、樹的實(shí)現(xiàn)方法示例
本文實(shí)例講述了Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊(duì)列、樹的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
最近無(wú)意中翻到一本書,閑來(lái)無(wú)事寫幾行代碼,實(shí)現(xiàn)幾種常用的數(shù)據(jù)結(jié)構(gòu),以備后查。
一、線性表(鏈表)
1、節(jié)點(diǎn)定義
/**鏈表節(jié)點(diǎn)定義 * @author colonel * */ class Node { public int data; Node next=null; public Node(int data){ this.data=data; } }
2、鏈表操作類
/**鏈表操作類 * @author colonel * */ public class operateClass { public Node headNode=null; /*給鏈表添加界節(jié)點(diǎn) * @param data 鏈表節(jié)點(diǎn)數(shù)據(jù) */ public Node addNode(int data){ Node newNode=new Node(data); if (headNode==null) { headNode=newNode; newNode.next=null; return headNode; } Node tempNode=headNode; while (tempNode.next!=null) { //tempNode=headNode; tempNode=tempNode.next; } tempNode.next=newNode; return headNode; } /**刪除節(jié)點(diǎn) * @param 刪除節(jié)點(diǎn)的位置 * */ public boolean delNode(int index){ if (index<1||index>length()) { return false; } if (index==1) { headNode=headNode.next; return true; } Node preNode=headNode; Node curNode=preNode.next; int count=2; while (curNode!=null) { if (count==index) { preNode.next=curNode.next; return true; } preNode=curNode; curNode=curNode.next; count++; } return true; } /**取鏈表的長(zhǎng)度 * @return返回鏈表的長(zhǎng)度 */ public int length(){ int length=0; Node temp=headNode; while (temp!=null) { length++; temp=temp.next; } return length; } /**按照值域?qū)︽湵頂?shù)據(jù)排序 * @return 返回排序后的鏈表頭節(jié)點(diǎn) */ public Node orderList(){ Node nextNode=null; int temp=0; Node curNode=headNode; while (curNode.next!=null) { nextNode=curNode.next; while (nextNode!=null) { if (curNode.data>nextNode.data) { temp=curNode.data; curNode.data=nextNode.data; nextNode.data=temp; } nextNode=nextNode.next; } curNode=curNode.next; } return headNode; } /** * 去除鏈表中值域重復(fù)的元素 */ public void redRepeat(){ if (length()<=1) { return; } Node curNode=headNode; while (curNode!=null) { Node insidNode=curNode.next; Node insidPreNode=insidNode; while (insidNode!=null) { if (insidNode.data==curNode.data) { insidPreNode.next=insidNode.next; //return; } insidPreNode=insidNode; insidNode=insidNode.next; } curNode=curNode.next; } } /**倒序輸出鏈表中所有的數(shù)據(jù) * @param hNode 鏈表頭節(jié)點(diǎn) */ public void reversePrint(Node hNode){ if (hNode!=null) { reversePrint(hNode.next); System.out.println(hNode.data); } } /** * 從頭節(jié)點(diǎn)開始到為節(jié)點(diǎn)結(jié)尾打印出值 */ public void printList(){ Node tmpNode=headNode; while (tmpNode!=null) { System.out.println(tmpNode.data); tmpNode=tmpNode.next; } } }
二、棧
1、該棧使用數(shù)組實(shí)現(xiàn),具體的棧操作類
class MyStack<E>{ private Object[] stack; int top=-1; public MyStack(){ stack=new Object[10]; } public boolean isEmpty(){ return top==0; } /**彈出棧頂元素(不刪除) * @return */ public E peek(){ if (isEmpty()) { return null; } return (E) stack[top]; } /**出棧站頂元素 * @return 棧頂元素 */ public E pop(){ E e=peek(); stack[top]=null; top--; return e; } /**壓棧 * @param item 待壓元素 * @return 返回待壓元素 */ public E push(E item){ //ensureCapacity(top+1); stack[++top]=item; return item; } /**棧滿擴(kuò)容 * @param size */ public void ensureCapacity(int size){ int len=stack.length; if (size>len) { int newLen=10; stack=Arrays.copyOf(stack, newLen); } } /**返回棧頂元素 * @return */ public E getTop(){ if (top==-1) { return null; } return (E) stack[top]; } }
三、隊(duì)列
該隊(duì)列使用鏈?zhǔn)綄?shí)現(xiàn)
1、隊(duì)節(jié)點(diǎn)定義
/** * @author colonel *隊(duì)節(jié)點(diǎn)定義 * @param <Elem> */ class queueNode<Elem>{ queueNode<Elem> nextNode=null; Elem data; public queueNode(Elem data){ this.data=data; } }
2、隊(duì)列操作類
/** * @author colonel *隊(duì)列操作類 * @param <Elem> */ class MyQueue<Elem>{ private queueNode<Elem> headNode=null; private queueNode<Elem> tailNode=null; private queueNode<Elem> lastNode=null; /**判斷該隊(duì)列是否為空 * @return 返回true or false */ public boolean isEmpty(){ return headNode==tailNode; } /**入隊(duì)操作 * @param data 節(jié)點(diǎn)元素值 */ public void put(Elem data){ queueNode<Elem> newNode=new queueNode<Elem>(data); if (headNode==null&&tailNode==null) { headNode=tailNode=newNode; //tailNode=headNode.nextNode; lastNode=tailNode.nextNode; return; } tailNode.nextNode=newNode; tailNode=newNode; lastNode=tailNode.nextNode; //tailNode=tailNode.nextNode; } /**出隊(duì)操作 * @return 返回出隊(duì)元素 */ public Elem pop(){ if (headNode==lastNode) { return null; } queueNode<Elem> tempNode=headNode; Elem statElem=tempNode.data; headNode=tempNode.nextNode; return statElem; } /**返回隊(duì)列長(zhǎng)度 * @return 長(zhǎng)度 */ public int size(){ if (isEmpty()) { return 0; } int length=0; queueNode<Elem> temp=headNode; while (temp!=null) { length++; temp=temp.nextNode; } return length; } }
四、二叉樹
1、節(jié)點(diǎn)定義
/**樹節(jié)點(diǎn)定義 * @author colonel * */ class TreeNode{ public int data; public TreeNode leftNode; public TreeNode rightNode; public TreeNode(int data){ this.data=data; this.leftNode=null; this.rightNode=null; } }
2、二叉樹操作類
/**二叉排序樹操作類 * @author colonel * */ class OperateTree{ public TreeNode rootNode; public OperateTree(){ rootNode=null; } /**元素插入二叉排序樹 * @param data 待插節(jié)點(diǎn)數(shù)據(jù) */ public void insert(int data){ TreeNode newNode=new TreeNode(data); if (rootNode==null) { rootNode=newNode; }else { TreeNode current=rootNode; TreeNode parent; while (true) { parent=current; if (data<current.data) { current=current.leftNode; if (current==null) { parent.leftNode=newNode; return; } } else { current=current.rightNode; if (current==null) { parent.rightNode=newNode; return; } } } } } /**構(gòu)建二叉排序樹 * @param item 元素?cái)?shù)組 */ public void buildTree(int[] item){ for (int i = 0; i < item.length; i++) { insert(item[i]); } } /** * 先序遍歷二叉樹 */ public void preOrder(TreeNode root){ if (root!=null) { System.out.println(root.data); preOrder(root.leftNode); preOrder(root.rightNode); } } /**中序遍歷 * @param root */ public void inOrder(TreeNode root){ if (root!=null) { inOrder(root.leftNode); System.out.println(root.data); inOrder(root.rightNode); } } /**后序遍歷 * @param root */ public void afterOrder(TreeNode root){ if (root!=null) { afterOrder(root.leftNode); afterOrder(root.rightNode); System.out.println(root.data); } } /** * 層序遍歷二叉排序樹 */ public void layerTrave(){ if (this.rootNode==null) { return; } Queue<TreeNode> myQueue=new LinkedList<>(); myQueue.add(rootNode); while (!myQueue.isEmpty()) { TreeNode tempNode=myQueue.poll(); System.out.println(tempNode.data); if (tempNode.leftNode!=null) { myQueue.add(tempNode.leftNode); } if (tempNode.rightNode!=null) { myQueue.add(tempNode.rightNode); } } }
五、總結(jié)
更好的理解數(shù)據(jù)結(jié)構(gòu)為何物,還需繼續(xù)探索,謹(jǐn)記。by:colonel
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- java實(shí)現(xiàn)隊(duì)列queue數(shù)據(jù)結(jié)構(gòu)詳解
- Java深入了解數(shù)據(jù)結(jié)構(gòu)之棧與隊(duì)列的詳解
- Java隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)
- Java數(shù)據(jù)結(jié)構(gòu)與算法之循環(huán)隊(duì)列的實(shí)現(xiàn)
- Java數(shù)據(jù)結(jié)構(gòu)與算法之稀疏數(shù)組與隊(duì)列深入理解
- java數(shù)據(jù)結(jié)構(gòu)-堆實(shí)現(xiàn)優(yōu)先隊(duì)列
- java實(shí)現(xiàn)隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼詳解
- java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例
- Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之隊(duì)列
相關(guān)文章
SpringBoot配置GlobalExceptionHandler全局異常處理器案例
這篇文章主要介紹了SpringBoot配置GlobalExceptionHandler全局異常處理器案例,通過簡(jiǎn)要的文章說(shuō)明如何去進(jìn)行配置以及使用,需要的朋友可以參考下2021-06-06java中動(dòng)態(tài)代理如何實(shí)現(xiàn)詳解
動(dòng)態(tài)代理是基于接口實(shí)現(xiàn)的代理,mybatis就是用這個(gè)技術(shù)實(shí)現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于java中動(dòng)態(tài)代理如何實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01SpringBoot+Redis大量重復(fù)提交問題的解決方案
Spring Boot Redis重復(fù)提交是指在使用Spring Boot框架和Redis緩存時(shí),為了防止用戶重復(fù)提交表單或者請(qǐng)求,采取的一種解決方案,本文通過代碼示例給大家介紹了SpringBoot+Redis大量重復(fù)提交問題的解決方案,需要的朋友可以參考下2024-03-03Java多線程高并發(fā)中的Fork/Join框架機(jī)制詳解
本文主要介紹了 Java 多線程高并發(fā)中的 Fork/Join 框架的基本原理和其使用的工作竊取算法(work-stealing)、設(shè)計(jì)方式和部分實(shí)現(xiàn)源碼,感興趣的朋友跟隨小編一起看看吧2021-11-11Spring DATA JPA 中findAll 進(jìn)行OrderBy方式
這篇文章主要介紹了Spring DATA JPA 中findAll 進(jìn)行OrderBy方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java創(chuàng)建、識(shí)別條形碼和二維碼方法示例
這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建、識(shí)別條形碼和二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Java為什么匿名內(nèi)部類參數(shù)引用需要用final進(jìn)行修飾?
今天小編就為大家分享一篇關(guān)于Java為什么匿名內(nèi)部類參數(shù)引用需要用final進(jìn)行修飾?,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04用Java設(shè)計(jì)模式中的觀察者模式開發(fā)微信公眾號(hào)的例子
這篇文章主要介紹了用Java設(shè)計(jì)模式中的觀察者模式開發(fā)微信公眾號(hào)的例子,這里Java的微信SDK等部分便不再詳述,只注重關(guān)鍵部分和開發(fā)過程中觀察者模式優(yōu)點(diǎn)的體現(xiàn),需要的朋友可以參考下2016-02-02