Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊(duì)列、樹的實(shí)現(xiàn)方法示例
本文實(shí)例講述了Java數(shù)據(jù)結(jié)構(gòu)之鏈表、棧、隊(duì)列、樹的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
最近無意中翻到一本書,閑來無事寫幾行代碼,實(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)文章
ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例
這篇文章主要為大家介紹了ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
高效數(shù)據(jù)傳輸?shù)拿孛芪淦鱌rotobuf的使用教程
Protobuf(Protocol?Buffers)是由?Google?開發(fā)的一種輕量級(jí)、高效的數(shù)據(jù)交換格式,它被用于結(jié)構(gòu)化數(shù)據(jù)的序列化、反序列化和傳輸,本文主要介紹了它的具體使用方法,需要的可以參考一下2023-05-05
詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
這篇文章主要介紹了詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
javaweb servlet生成簡(jiǎn)單驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了javaweb servlet生成簡(jiǎn)單驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Springboot讀取templates文件html代碼實(shí)例
這篇文章主要介紹了Springboot讀取templates文件html代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
淺談Java變量賦值運(yùn)算符及相關(guān)實(shí)例
這篇文章主要介紹了Java賦值運(yùn)算符的一些知識(shí),需要的朋友可以參考下。2017-09-09
Spring Boot 2.4版本前后的分組配置變化及對(duì)多環(huán)境配置結(jié)構(gòu)的影響(推薦)
這篇文章主要介紹了Spring Boot 2.4版本前后的分組配置變化及對(duì)多環(huán)境配置結(jié)構(gòu)的影響,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

