java数据结构博客园_常见数据结构的Java实现
单链表的Java实现
首先参考wiki上的单链表说明,单链表每个节点包含数据和指向链表中下一个节点的指针或引用。然后看代码
import java.lang.*;
public class SinglyLinkeList
{
Node start;
public SinnglyLinkedList()
{
this.start=null;
}
public void addFront(Object newData)
{
Node cache = this.start; //store a reference to the current start node
this.start = new Node(newData,cache); //assign our start to a new node that has newData and points to our old start
}
public addRear(Object newData)
{
Node cache = start;
Node current = null;
while((current = cache.next) != null) //find the last Node
cache = cache.next;
cache.next = new Node(newData,null); //create a new node that has newData and points to null
}
public Object getFront()
{
return this.start.data; // return the front object's data
}
public class Node
{
public Object data; //the data stored in this node
public Node next; //store a reference to the next node in this singlylinkedlist
public Node(Object data,Node next){
this.data =data;
this.next =next;
}
}
}
单链表翻转的Java实现
循环方式
public static LinkedList reverse(LinkedList Node) {
LinkedList previous = null;
while (Node != null) {
LinkedList next = Node.next;
Node.next = previous;
previous = Node;
Node = next;
}
return previous;
}
package linkedlists;
public static LinkedList reverse(LinkedList node) {
LinkedList headNode = new LinkedList(1);
快速排序的Java实现
public class QuickSort {
public static int SIZE = 1000000;
public int[] sort(int[] input) {
quickSort(input, 0, input.length-1);
return input;
}
public static void main(String args[]){
int[] a = new int[SIZE];
for(int i=0;i
a[i] = (int)(Math.random()*SIZE);
}
QuickSort mNew = new QuickSort();
long start = System.currentTimeMillis();
mNew.sort(a);
long end = System.currentTimeMillis();
System.out.println("Time taken to sort a million elements : "+(end-start)+" milliseconds");
}
public void print(int[] inputData) {
for(int i:inputData){
System.out.print(i+" ");
}
System.out.println();
}
private void quickSort(int arr[], int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSort(arr, left, index - 1);
if (index < right)
quickSort(arr, index, right);
}
private int partition(int arr[], int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}
}
java数据结构博客园_常见数据结构的Java实现相关推荐
- python 面试题 博客园_常见的python面试问题1
雷锋网按:本文为 AI 研习社编译的技术博客,原文 Top 35 Python Interview Questions and Answers in 2018 ,作者 DataFlair Team. ...
- java list 博客园_Java集合系列(一)List集合
List的几种实现的区别与联系 List主要有ArrayList.LinkedList与Vector几种实现. ArrayList底层数据结构是数组, 增删慢.查询快; 线程不安全, 效率高; 不可以 ...
- python博客园_用Python向博客园发布新文章
最近在开发一个博客系统,经常把写的东西放在自己网站的博客上(之前写在Onenote),然后我在博客园也申请了一个博客,就有了同样一篇文章,我需要复制粘贴排版分别提交两次的情况.于是我就想能不能在我的网 ...
- java知识点博客园_JAVA基础知识回顾
JAVA基础知识回顾 一.背景介绍 JavaSE(J2SE)(Java2 Platform Standard Edition,java平台标准版) JavaEE(J2EE)(Java 2 Platfo ...
- java 入门 博客园_java入门基础
什么是java? java是一门编程语言 编程语言有很多种 你比如 C语言 等等 为什么学习java呢! 因为你要和计算机交互 当然了你用汉语跟她说她听不懂 所以你要学习编程语言 那么额咱们的ja ...
- java ee 博客园_JAVAEE 介绍
软件151 雷楷文 1. 为什么需要JavaEE 我们编写的JSP代码中,由于大量的显示代码和业务逻辑混淆在一起,彼此嵌套,不利于程序的维护和扩展.当业务需求发生变化的时候,对于程序员和美工都是一 ...
- java数据结构博客园_Java数据结构
一.线性数据结构 1.Java一维数组的创建 (1)预先定义数组的内存空间 int[] arr = new int[3]; // new int[3]是代表创建3个内存地址空间 // 地址空间的序号是 ...
- java orm框架 博客园_自己写ORM框架 DBUtils
ORM框架想必大家都比较熟知了,即对象关系映射(英语:Object Relation Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同 ...
- java 金融面试题目_面试题 - java金融 - 博客园
1,什么是线程安全 (参考书:https://book.douban.com/subject/10484692/) 不对共享变量进行修改 2,都说String是不可变的,为什么我可以这样做呢 Stri ...
最新文章
- 打造AI产教融合共赢生态,微软亚洲研究院扩大开放了这些资源
- 微型计算机系统采用三级存储器组织结构,微型计算机原理07级试卷B.doc
- 数据库的两种引擎Innodb和MyIASM
- Facebook构建高性能Android视频组件实践之路
- 2016ICPC青岛
- iview this.$modal 关闭所有的弹窗_一看会用TOB弹窗应用场景
- Python异常处理try...except...finally raise assert
- matlab 求向量的交集_MATLAB矩阵列向量单位化的最快代码
- 异地备份——windows 与 linux
- (第二部)程序员逆天改命之胜天半子
- NPP/VIIRS夜间灯光数据下载和介绍
- my games / BF3 / GTA5 / NFS18 / sanguowushuang6 / RA2 / KOF97 / FIFA
- python如何读取字符串最后一个字符_python 获取字符串最后一个字符
- iOS闪退日志的收集和解析
- 经典网络-InceptionV1论文及实践
- 用一年的数据预测下一年数据_一年的招聘数据中的经验教训
- SQL高级——PLSQL数据库编程
- ROCKCHIP PWM模块开发指南
- C语言教程基础训练(一):如何计算圆的周长
- html怎样使整个页面居中,如何使整个页面内容居中使高度适应内容自动伸缩