欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊列、樹的實現(xiàn)方法示例

 更新時間:2019年03月12日 11:16:26   作者:0colonel0  
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊列、樹的實現(xiàn)方法,結(jié)合實例形式分析了Java數(shù)據(jù)結(jié)構(gòu)中鏈表、棧、隊列、樹的功能、定義及使用方法,需要的朋友可以參考下

本文實例講述了Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊列、樹的實現(xiàn)方法。分享給大家供大家參考,具體如下:

最近無意中翻到一本書,閑來無事寫幾行代碼,實現(xiàn)幾種常用的數(shù)據(jù)結(jié)構(gòu),以備后查。

一、線性表(鏈表)

1、節(jié)點定義

/**鏈表節(jié)點定義
 * @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é)點
 * @param data 鏈表節(jié)點數(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é)點
 * @param 刪除節(jié)點的位置
 *
 */
 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;
 }
 /**取鏈表的長度
 * @return返回鏈表的長度
 */
 public int length(){
 int length=0;
 Node temp=headNode;
 while (temp!=null) {
  length++;
  temp=temp.next;
 }
 return length;
 }
 /**按照值域?qū)︽湵頂?shù)據(jù)排序
 * @return 返回排序后的鏈表頭節(jié)點
 */
 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é)點
 */
 public void reversePrint(Node hNode){
 if (hNode!=null) {
  reversePrint(hNode.next);
  System.out.println(hNode.data);
 }
 }
 /**
 * 從頭節(jié)點開始到為節(jié)點結(jié)尾打印出值
 */
 public void printList(){
 Node tmpNode=headNode;
 while (tmpNode!=null) {
  System.out.println(tmpNode.data);
  tmpNode=tmpNode.next;
 }
 }
}

二、棧

1、該棧使用數(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;
 }
 /**棧滿擴容
 * @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];
 }
}

三、隊列

該隊列使用鏈式實現(xiàn)

1、隊節(jié)點定義

/**
 * @author colonel
 *隊節(jié)點定義
 * @param <Elem>
 */
class queueNode<Elem>{
 queueNode<Elem> nextNode=null;
 Elem data;
 public queueNode(Elem data){
 this.data=data;
 }
}

2、隊列操作類

/**
 * @author colonel
 *隊列操作類
 * @param <Elem>
 */
class MyQueue<Elem>{
 private queueNode<Elem> headNode=null;
 private queueNode<Elem> tailNode=null;
 private queueNode<Elem> lastNode=null;
 /**判斷該隊列是否為空
 * @return 返回true or false
 */
 public boolean isEmpty(){
 return headNode==tailNode;
 }
 /**入隊操作
 * @param data 節(jié)點元素值
 */
 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;
 }
 /**出隊操作
 * @return 返回出隊元素
 */
 public Elem pop(){
 if (headNode==lastNode) {
  return null;
 }
 queueNode<Elem> tempNode=headNode;
 Elem statElem=tempNode.data;
 headNode=tempNode.nextNode;
 return statElem;
 }
 /**返回隊列長度
 * @return 長度
 */
 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é)點定義

/**樹節(jié)點定義
 * @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é)點數(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 元素數(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ù)探索,謹記。by:colonel

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • SpringBoot配置GlobalExceptionHandler全局異常處理器案例

    SpringBoot配置GlobalExceptionHandler全局異常處理器案例

    這篇文章主要介紹了SpringBoot配置GlobalExceptionHandler全局異常處理器案例,通過簡要的文章說明如何去進行配置以及使用,需要的朋友可以參考下
    2021-06-06
  • java中動態(tài)代理如何實現(xiàn)詳解

    java中動態(tài)代理如何實現(xiàn)詳解

    動態(tài)代理是基于接口實現(xiàn)的代理,mybatis就是用這個技術(shù)實現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于java中動態(tài)代理如何實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java中捕獲線程異常的幾種方式總結(jié)

    Java中捕獲線程異常的幾種方式總結(jié)

    這篇文章主要介紹了Java中捕獲線程異常的幾種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot+Redis大量重復(fù)提交問題的解決方案

    SpringBoot+Redis大量重復(fù)提交問題的解決方案

    Spring Boot Redis重復(fù)提交是指在使用Spring Boot框架和Redis緩存時,為了防止用戶重復(fù)提交表單或者請求,采取的一種解決方案,本文通過代碼示例給大家介紹了SpringBoot+Redis大量重復(fù)提交問題的解決方案,需要的朋友可以參考下
    2024-03-03
  • Java enum 對枚舉元素的賦值和取值方式

    Java enum 對枚舉元素的賦值和取值方式

    這篇文章主要介紹了Java enum 對枚舉元素的賦值和取值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java多線程高并發(fā)中的Fork/Join框架機制詳解

    Java多線程高并發(fā)中的Fork/Join框架機制詳解

    本文主要介紹了 Java 多線程高并發(fā)中的 Fork/Join 框架的基本原理和其使用的工作竊取算法(work-stealing)、設(shè)計方式和部分實現(xiàn)源碼,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • Spring DATA JPA 中findAll 進行OrderBy方式

    Spring DATA JPA 中findAll 進行OrderBy方式

    這篇文章主要介紹了Spring DATA JPA 中findAll 進行OrderBy方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java創(chuàng)建、識別條形碼和二維碼方法示例

    Java創(chuàng)建、識別條形碼和二維碼方法示例

    這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建、識別條形碼和二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java為什么匿名內(nèi)部類參數(shù)引用需要用final進行修飾?

    Java為什么匿名內(nèi)部類參數(shù)引用需要用final進行修飾?

    今天小編就為大家分享一篇關(guān)于Java為什么匿名內(nèi)部類參數(shù)引用需要用final進行修飾?,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • 用Java設(shè)計模式中的觀察者模式開發(fā)微信公眾號的例子

    用Java設(shè)計模式中的觀察者模式開發(fā)微信公眾號的例子

    這篇文章主要介紹了用Java設(shè)計模式中的觀察者模式開發(fā)微信公眾號的例子,這里Java的微信SDK等部分便不再詳述,只注重關(guān)鍵部分和開發(fā)過程中觀察者模式優(yōu)點的體現(xiàn),需要的朋友可以參考下
    2016-02-02

最新評論