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左 ...
随机推荐
- Spring MVC中文文档翻译发布
前后经过九个月,我翻译的Spring MVC官方4.2.4版本中文文档可以发布第一个较为完整的版本了.译文上尽量做到准确并且符合中文习惯,让人能读懂,能理解.现全文发布如下,也希望它能够给出其价值,并 ...
- VS2015突然报错————Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value 'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper
Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with ...
- 【转】zigbee终端无法重连的问题解决
zigbee终端无法重连的问题解决 1.zigbee重连的原因 (1)zigbee由于各种原因的干扰导致信号太差而掉线. (2)协调器重启. 2.zigbee终端重连的处理 (1)zigbee掉线后会 ...
- 静态代理和利用反射形成的动态代理(JDK动态代理)
代理模式 代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 静态代理 1.新建 ...
- 来玩Play框架01 简介
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 说到网络框架,Ruby的Ruby on Rail和Python的Django都相当 ...
- GJM :Unity集成Leap Motion
Demo演示视频
- 共享Excel编辑的一些资源
官方资料: 使用共享工作簿进行协作 相关介绍文章: http://www.ittribalwo.com/article/678.html http://www.wuji8.com/meta/85646 ...
- SQL Server游标(转)
清晰地介绍了SQL游标,很好的学习资料. 转自 http://www.cnblogs.com/knowledgesea/p/3699851.html 什么是游标 结果集,结果集就是select查询之后 ...
- 嵌入式:J-link刷固件(坑)
1.上电,短接ERASE,>10秒后,拔USB. 2.短接TST,上电,>10秒后,拔USB. 3.安装驱动.(看别人教程,下载到INF文件,WIN7不能右击安装,好,换虚拟机XP) 4. ...
- UIBezierPath-完善曲线
override func draw(_ rect: CGRect) { let path = UIBezierPath() // 起点 path.move(to: CGPoint(x: , y: ) ...