Java各种数据结构实现
1、单向链表
实现思路:创建Node类,包括自己的数据和指向下一个;创建Node类,包括头尾节点,实现添加、删除、输出等功能。
tips:n = n.next不破坏链表结果,而n.next = n.next.next就等于是n节点的next属性变成了再下一个,即指向n+1个节点的指针丢失,但实际上n+1节点仍在,只不过从链表中去除。
具体代码:
public class NodeList<Integer> {
class Node<Integer> {
Node<Integer> next = null; //指向下一节点
Integer data; //节点所存数据
//Node类构造方法
public Node(Integer d) {
this.data = d;
}
}
Node<Integer> head; //链表的头节点
Node<Integer> last; //链表的尾节点
int length; //链表长度
//链表的无参构造方法
public NodeList() {
this.head = new Node<Integer>(null); //头节点为空
}
//创建链表的同时添加第一个数据
public NodeList(Integer d) {
this.head = new Node<Integer>(d);
this.last = head; //此时头尾节点一样
length++;
}
//尾部添加节点
public void add(Integer d) {
if (head == null) {
head = new Node<Integer>(d);
last = head;
length++;
} else {
Node<Integer> newNode = new Node<Integer>(d);
last.next = newNode; //令之前的尾节点指向新节点
last = newNode; //新节点成为尾节点
length++;
}
}
//删除指定数据
public boolean del(Integer d) {
if (head == null) {
return false;
}
Node<Integer> n = head; //从头开始判断
if (n.data == d) {
head = head.next;
length--;
return true;
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next; //n节点指向了n+2节点
length--;
return true;
}
n = n.next; //正常移动不破坏链表
}
return false;
}
public void print() {
if (head == null) {
System.out.println("此链表为空!");
}
Node<Integer> n = head;
while (n != null) {
System.out.println(n.data+",");
n = n.next;
}
}
}
2016-12-13 16:22:13
2、栈(链式结构)
实现思路:仍然使用节点,最重要的是栈顶节点top,利用其完成压、弹栈操作。
tips:出栈就是改栈顶为下一个,压栈就是新栈顶与原栈顶建立链接。
具体代码:
public class Stack {
class Node {
Object data;
Node next = null;
public Node(Object d) {
this.data = d;
}
}
Node top; //创建栈顶节点
//出栈
public Object pop() {
if (top != null) {
Object d = top.data;
top = top.next; //栈顶改为下一个
return d;
}
return null;
}
//压栈
public void push(Object d) {
Node n = new Node(d);
n.next = top; //与原栈进行连接
top = n;
}
//输出栈顶的元素
public Object peek() {
return top.data;
}
}
2016-12-14 14:14:12
3、队列(链式结构)
实现思路:创建首尾节点first、last,实现入队出队操作。
tips:记得判断是否为空。
具体代码:
public class Queue {
class Node {
Object data;
Node next;
public Node(Object d) {
this.data = d;
}
}
//创建队首队尾指针
Node first;
Node last;
//入队
public void enQueue(Object d) {
if (first == null) {
first = new Node(d);
last = first;
} else {
last.next = new Node(d); //原队尾指向新节点
last = last.next; //更改队尾
}
}
//出队
public Object deQueue() {
if (first != null) {
Object item = first.data; //保存原队首数据
first = first.next; //更改队首
return item;
}
return null;
}
}
2016-12-14 14:27:16
4、树的遍历
4.1、前序preorder
顺序:根左右
非递归:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
List<Integer> preorder = new ArrayList<Integer>();
if (root == null) {
return preorder;
}
stack.push(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
preorder.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return preorder;
}
}
Traverse:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> preorder = new ArrayList<Integer>();
helper(root, preorder);
return preorder;
}
private void helper(TreeNode root, ArrayList<Integer> preorder) {
if (root == null) {
return;
}
preorder.add(root.val);
helper(root.left, preorder);
helper(root.right, preorder);
}
Divide & Conquer:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> preorder = new ArrayList<Integer>();
if (root == null) {
return preorder;
}
//Divide
ArrayList<Integer> left = preorderTraversal(root.left);
ArrayList<Integer> right = preorderTraversal(root.right);
//Conquer
preorder.add(root.val);
preorder.addAll(left);
preorder.addAll(right);
return preorder;
}
}
4.2、中序inorder
顺序:左根右
非递归:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> inorder = new ArrayList<Integer>();
TreeNode curt = root;
while (curt != null || !stack.empty()) {
while (curt != null) {
stack.add(curt);
curt = curt.left;
}
curt = stack.peek();
stack.pop();
inorder.add(curt.val);
curt = curt.right;
}
return inorder;
}
}
Traverse:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> inorder = new ArrayList<Integer>();
helper(root, inorder);
return inorder;
}
private void helper(TreeNode root, ArrayList<Integer> inorder) {
if (root == null) {
return;
}
helper(root.left, inorder);
inorder.add(root.val);
helper(root.right, inorder);
}
}
Divide & Conquer:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> inorder = new ArrayList<Integer>();
if (root == null) {
return inorder;
}
ArrayList<Integer> left = inorderTraversal(root.left);
ArrayList<Integer> right = inorderTraversal(root.right);
inorder.addAll(left);
inorder.add(root.val);
inorder.addAll(right);
return inorder;
}
}
4.3、后序postorder
顺序:左右根
非递归:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> postorder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null; //前一个遍历节点
TreeNode curr = root;
if (root == null) {
return postorder;
}
stack.push(root);
while (!stack.empty()) {
curr = stack.peek();
if (prev == null || prev.left == curr || prev.right == curr) {
//往下遍历
if (curr.left != null) {
stack.push(curr.left);
} else if (curr.right != null) {
stack.push(curr.right);
}
} else if (curr.left == prev) {
//从左往上遍历
if (curr.right != null) {
stack.push(curr.right);
}
} else {
//从右往上遍历
postorder.add(curr.val);
stack.pop();
}
prev = curr;
}
return postorder;
}
}
递归与上面一样
2017-01-27
Java各种数据结构实现的更多相关文章
- java项目——数据结构实验报告
java项目——数据结构总结报告 20135315 宋宸宁 实验要求 1.用java语言实现数据结构中的线性表.哈希表.树.图.队列.堆栈.排序查找算法的类. 2.设计集合框架,使用泛型实现各类. ...
- JAVA常用数据结构及原理分析
JAVA常用数据结构及原理分析 http://www.2cto.com/kf/201506/412305.html 前不久面试官让我说一下怎么理解java数据结构框架,之前也看过部分源码,balaba ...
- (6)Java数据结构-- 转:JAVA常用数据结构及原理分析
JAVA常用数据结构及原理分析 http://www.2cto.com/kf/201506/412305.html 前不久面试官让我说一下怎么理解java数据结构框架,之前也看过部分源码,balab ...
- 【转】Java学习---Java核心数据结构(List,Map,Set)使用技巧与优化
[原文]https://www.toutiao.com/i6594587397101453827/ Java核心数据结构(List,Map,Set)使用技巧与优化 JDK提供了一组主要的数据结构实现, ...
- 【转载】图解Java常用数据结构(一)
图解Java常用数据结构(一) 作者:大道方圆 原文:https://www.cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, 系统化看了下Jav ...
- java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表 数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...
- Java同步数据结构之ConcurrentSkipListMap/ConcurrentSkipListSet
引言 上一篇Java同步数据结构之Map概述及ConcurrentSkipListMap原理已经将ConcurrentSkipListMap的原理大致搞清楚了,它是一种有序的能够实现高效插入,删除,更 ...
- java之数据结构之链表及包装类、包
链表是java中的一种常见的基础数据结构,是一种线性表,但是不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针.与线性对应的一种算法是递归算法:递归算法是一种直接或间接的调用自身算法的过 ...
- java核心数据结构总结
JDK提供了一组主要的数据结构的实现,如List.Set.Map等常用结构,这些结构都继承自java.util.collection接口. List接口 List有三种不同的实现,ArrayList和 ...
- 关于Java的数据结构HashMap,ArrayList的使用总结及使用场景和原理分析
使用,必须要知道其原理,在课堂上学过散列函数的用法及其原理.但一直不知道怎么实践. 后来,在实际项目中,需要做一个流量分析预处理程序.每5分钟会接收到现网抓来的数据包并解析,每个文本文件大概200M左 ...
随机推荐
- 初识ASP.NET MVC
我们首先从创建ASP.NET MVC项目开始.打开Visual Studio,在文件菜单中选择新建-> 项目,然后在模板中选择Web,接着选择ASP.Net Web应用程序,更改项目名称,点击确 ...
- C#开发微信门户及应用(15)-微信菜单增加扫一扫、发图片、发地理位置功能
前面介绍了很多篇关于使用C#开发微信门户及应用的文章,基本上把当时微信能做的接口都封装差不多了,微信框架也积累了不少模块和用户,最近发现微信公众平台增加了不少内容,特别是在自定义菜单里面增加了扫一扫. ...
- [函數] Firemonkey Android 取得系统参数设定的字型大小
Android 系统参数设定内,可以设定字型大小: 可以透过下面代码来取得字型大小比例: function FontScale: Single; var Resources: JResources; ...
- Servlet3.0的可插拔功能
如果说 3.0 版本新增的注解支持是为了简化 Servlet/ 过滤器 / 监听器的声明,从而使得 web.xml 变为可选配置, 那么新增的可插性 (pluggability) 支持则将 Servl ...
- spring源码:学习线索(li)
一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...
- safari cookie设置中文失败
最近用H5进行手机端开发,由于是window操作系统,为了方便开发和调试,直接在chrome浏览器上进行测试,然后在android机上进行手机端测试,当功能基本完工后,原来在android上运行正常的 ...
- 基于rem的移动端自适应解决方案
代码有更新,最好直接查看github: https://github.com/finance-sh/adaptive adaptivejs原理: 利用rem布局,根据公式 html元素字体大小 = d ...
- Bootstrap之选项卡
<div class="container"> <!-- nav-tabs作为选项卡头部样式 --> <ul class="nav nav- ...
- ArcGIS Engine开发之空间查询
空间查询功能是通过用户选择的空间几何体以及该几何体与当前地图中要素之间的几何关系进行空间查找,从而得到查询结果的操作. 相关类与接口 空间查询相关的类主要是SpatialFilter类,其实现的接口主 ...
- iOS之获取经纬度并通过反向地理编码获取详细地址
_locationManager = [[CLLocationManager alloc] init]; //期望的经度 _locationManager.desiredAccuracy = kCLL ...