Java实现自定义队列和树结构_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{
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
*/
class queuenode{
queuenode nextnode=null;
elem data;
public queuenode(elem data){
this.data=data;
}
}
2、队列操作类
/**
* @author colonel
*队列操作类
* @param
*/
class myqueue{
private queuenode headnode=null;
private queuenode tailnode=null;
private queuenode lastnode=null;
/**判断该队列是否为空
* @return 返回true or false
*/
public boolean isempty(){
return headnode==tailnode;
}
/**入队操作
* @param data 节点元素值
*/
public void put(elem data){
queuenode newnode=new queuenode(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 tempnode=headnode;
elem statelem=tempnode.data;
headnode=tempnode.nextnode;
return statelem;
}
/**返回队列长度
* @return 长度
*/
public int size(){
if (isempty()) {
return 0;
}
int length=0;
queuenode 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=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 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程序设计有所帮助。
希望与广大网友互动??
点此进行留言吧!
Java实现自定义队列和树结构_Java数据结构之链表、栈、队列、树的实现方法示例...相关推荐
- 平衡查找树C语言程序,C语言数据结构之平衡二叉树(AVL树)实现方法示例
本文实例讲述了C语言数据结构之平衡二叉树(AVL树)实现方法.分享给大家供大家参考,具体如下: AVL树是每个结点的左子树和右子树的高度最多差1的二叉查找树. 要维持这个树,必须在插入和删除的时候都检 ...
- 常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构)
常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构) 数据结构和算法作为程序员的基本功,一定得稳扎稳打的学习,我们常见的框架底层就是各类数据 ...
- 黑马程序员 C语言数据结构与算法之线性表(链表/栈/队列/顺序表)
C语言 链表基础知识清晰讲解(黑马) 讲的蛮好,就是音质不太好,有时听不清讲的啥! [黑马]数据结构与算法之线性表(链表/栈/队列/顺序表)[配套源码 嘛蛋,看错了,这是java的... 文章目录 链 ...
- 面试必备:高频算法题汇总「图文解析 + 教学视频 + 范例代码」必问之 链表 + 栈 + 队列 部分!
链表 链表是最基本的数据结构,面试官也常常用链表来考察面试者的基本能力,而且链表相关的操作相对而言比较简单,也适合考察写代码的能力.链表的操作也离不开指针,指针又很容易导致出错. 综合多方面的原因,链 ...
- java数据结构 队列_Java数据结构与算法[原创]——队列
声明:码字不易,转载请注明出处,欢迎文章下方讨论交流. 前言:Java数据结构与算法专题会不定时更新,欢迎各位读者监督.本文介绍数据结构中的队列(queue)的概念.存储结构.队列的特点,文末给出ja ...
- java判断栈中元素数目_Java数据结构与算法-栈和队列
(摘录加总结)------ 栈和队列不属于基础的数据结构,它们都属于线性表. 一.栈 对于栈存储操作元素只能在栈结构的一端进行元素的插入和删除,是一种性质上的线性表结构.按照"先进后出&qu ...
- java中定义一个栈容器_Java 容器之 Connection栈队列及一些常用
集合家族图 ---|Collection: 单列集合 ---|List: 有存储顺序 , 可重复 ---|ArrayList: 数组实现 , 查找快 , 增删慢 ---|LinkedList: 链表实 ...
- java链表的数据结构_Java数据结构 获取链表(LinkedList)的第一个和最后一个元素
Java数据结构 获取链表(LinkedList)的第一个和最后一个元素 以下实例演示了如何使用 LinkedList 类的 linkedlistname.getFirst() 和 linkedlis ...
- java 队列已满_JAVA中常见的阻塞队列详解
在之前的线程池的介绍中我们看到了很多阻塞队列,这篇文章我们主要来说说阻塞队列的事. 阻塞队列也就是 BlockingQueue ,这个类是一个接 口,同时继承了 Queue 接口,这两个接口都是在JD ...
最新文章
- HTML Viewer展示不同字体
- 计算机没有游戏扫雷,系统没有扫雷游戏怎么办 简单几步轻松安装
- 查看node的位置_升级Node版本RN项目运行报错cb.apply is not a function
- python list转map_Python 进阶之术 Map Filter Reduce
- 进入快速通道的委托(深入理解c#)
- 清北学堂 清北-Day1-R1-Count
- RHEL6.4上Samba/NFS服务器简单配置
- codevs 1048 石子归并
- JetBrains AppCode:用于 iOS/macOS 开发的智能 IDE
- 字体字号磅数大小对照表
- 开发过程中沟通的重要性
- 曾经的这家煎饼是怎么走红的?
- 罗技K375s重新配置和连接
- 欲望无限--《贝奥武夫》给我们的启示
- vuepress build error: window is not defined
- 刘海屏启动空白页适配全面屏
- Servlet 执行原理
- c++left right 和 setw() 函数的用法Alignment of Code
- 教程 | 使用小O地图制作文字标注地图
- 电脑开机蓝屏怎么解决