Java数据结构之链表、栈、队列、树的实现方法示例
作者:0colonel0 发布时间:2021-10-07 10:40:29
标签:Java,数据结构,链表,栈,队列,树
本文实例讲述了Java数据结构之链表、栈、队列、树的实现方法。分享给大家供大家参考,具体如下:
最近无意中翻到一本书,闲来无事写几行代码,实现几种常用的数据结构,以备后查。
一、线性表(链表)
1、节点定义
/**链表节点定义
* @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;
/*给链表添加界节点
* @param data 链表节点数据
*/
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;
}
/**删除节点
* @param 删除节点的位置
*
*/
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;
}
/**按照值域对链表数据排序
* @return 返回排序后的链表头节点
*/
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;
}
/**
* 去除链表中值域重复的元素
*/
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;
}
}
/**倒序输出链表中所有的数据
* @param hNode 链表头节点
*/
public void reversePrint(Node hNode){
if (hNode!=null) {
reversePrint(hNode.next);
System.out.println(hNode.data);
}
}
/**
* 从头节点开始到为节点结尾打印出值
*/
public void printList(){
Node tmpNode=headNode;
while (tmpNode!=null) {
System.out.println(tmpNode.data);
tmpNode=tmpNode.next;
}
}
}
二、栈
1、该栈使用数组实现,具体的栈操作类
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];
}
}
三、队列
该队列使用链式实现
1、队节点定义
/**
* @author colonel
*队节点定义
* @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 节点元素值
*/
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、节点定义
/**树节点定义
* @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 待插节点数据
*/
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;
}
}
}
}
}
/**构建二叉排序树
* @param item 元素数组
*/
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);
}
}
}
五、总结
更好的理解数据结构为何物,还需继续探索,谨记。by:colonel
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/sinat_34322082/article/details/53694315
0
投稿
猜你喜欢
- java.util.concurrent包中的工具实现核心都是AQS,了解ReentrantLock的实现原理,需要先分析AQS以及AQS与
- 一、什么是过滤器过滤器是对数据进行过滤,预处理过程,当我们访问网站时,有时候会发布一些敏感信息,发完以后有的会用*替代,还有就是登陆权限控制
- 这篇文章主要介绍了Java如何实现自定义异常类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- instanceof关键字的使用1. 语法格式x instanceof A:检验x是否为类A的对象,返回值为boolean类型,如果是,返回
- MyBatis插入Insert、InsertSelective的区别逆向自动生成的mybatis对应配置Mapper文件里面,有两个方法,分
- 本文实例讲述了C++实现的链表类。分享给大家供大家参考。具体如下:#include <iostream>using namesp
- SpringAOP的介绍:传送门demo介绍主要通过自定义注解,使用SpringAOP的环绕通知拦截请求,判断该方法是否有自定义注解,然后判
- Java 用反射设置对象的属性值实例详解/** * 用反射设置对象的属性值 * @param obj 需要設置值的對象 * @param f
- 一、问题描述在使用idea Jrebel续期的时候,修改idea激活服务器地址时,遇到报错:Cannot reactivate, offli
- 1. 继承1. 子类继承了父类,获得父类的全部Field和方法。子类Student类继承父类,将可以获得父类的全部Field和方法publi
- Java泛型是JDK 5引入的一个特性,它允许我们定义类和接口的时候使用参数类型,泛型在集合框架中被广泛使用。类型擦除是泛型中最让人困惑的部
- 题目一链表题——链表合并根据给定的两个升序链表合并为一个新的升序链表具体题目如下解法/** * Definition for singly-
- 本文实例讲述了C#中HttpWebRequest的用法。分享给大家供大家参考。具体如下:HttpWebRequest类主要利用HTTP 协议
- 本文实例为大家分享了java实现马踏棋盘的具体代码,供大家参考,具体内容如下马踏棋盘算法介绍8X8棋盘,马走日字,要求每个方格只进入一次,走
- Class.forName(xxx.xx.xx) 返回的是一个类一.首先你要明白在java里面任何class都要装载在虚拟机上才能运行。1.
- 提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档前言这两天在项目中使用到Java的导入导出功能,以前对这块有一定了解,但是没
- Android手势解锁本文讲述的是一个手势解锁的库,可以定制显示隐藏宫格点、路径、并且带有小九宫格显示图,和震动!让你学会使用这个简单,高效
- Android 显示GIF图片实例详解gif图动画在Android中还是比较常用的,比如像新浪微博中,有很多gif图片,而且展示非常好,所以
- 1 StringString:字符串常量,字符串长度不可变。2 StringBufferStringBuffer:字符串变量(Synchro
- 前几天在跟公司大佬讨论一个问题时,看到他使用Handler的一种方式,旁边的同事在说:以前不是这么用的啊。这个问题引发了我的好奇,虽然当时翻