常见数据结构的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<SIZE;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实现的更多相关文章
- [py]软件编程知识骨架+py常见数据结构
认识算法的重要性 - 遇到问题? 学完语言,接到需求,没思路? 1.学会了语言,能读懂别人的代码, 但是自己没解决问题的能力,不能够把实际问题转换为代码,自己写出来.(这是只是学会一门语言的后果),不 ...
- 转 Python常见数据结构整理
http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...
- 数据结构与算法——常用高级数据结构及其Java实现
前文 数据结构与算法--常用数据结构及其Java实现 总结了基本的数据结构,类似的,本文准备总结一下一些常见的高级的数据结构及其常见算法和对应的Java实现以及应用场景,务求理论与实践一步到位. 跳跃 ...
- 8种常见数据结构及其Javascript实现
摘要: 面试常问的知识点啊... 原文:常见数据结构和Javascript实现总结 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 做前端的同学不少都是自学成才或者半路出家, ...
- 常见数据结构之JavaScript实现
常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...
- 数据结构-堆 Java实现
数据结构-堆 Java实现. 实现堆自动增长 /** * 数据结构-堆. 自动增长 * */ public class Heap<T extends Comparable> { priva ...
- 20172332 2017-2018-2 《程序设计与数据结构》Java哈夫曼编码实验--哈夫曼树的建立,编码与解码
20172332 2017-2018-2 <程序设计与数据结构>Java哈夫曼编码实验--哈夫曼树的建立,编码与解码 哈夫曼树 1.路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子 ...
- 数据结构(java语言描述)
概念性描述与<数据结构实例教程>大同小异,具体参考:http://www.cnblogs.com/bookwed/p/6763300.html. 概述 基本概念及术语 数据 信息的载体,是 ...
- 数据结构:JAVA实现二叉查找树
数据结构:JAVA实现二叉查找树 写在前面 二叉查找树(搜索树)是一种能将链表插入的灵活性与有序数组查找的高效性结合在一起的一种数据结构. 观察二叉查找树,我们发现任何一个节点大于左子节点且小于其右子 ...
随机推荐
- css 兼容ie8 rgba()用法
今天遇到了一个问题,要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如下的属性即可: background: rgba(255,255,255,.1); 但是要兼容到 ...
- #微码分享#C++变参字符串格式化函数format_string
在C和C++中,变参格式化函数虽然非类型安全,但却十分便利,因为得到广泛使用.对于常见的size_t类型要用“%zu”,ssize_t用”%zd“,int64_t用“% ”PRId64,uint64_ ...
- 笔记 Bioinformatics Algorithms Chapter2
Chapter2 WHICH DNA PATTERNS PLAY THE ROLE OF MOLECULAR CLOCKS 寻找模序 一. 转录因子会结合基因上游的特定序列,调控基因的转录表达,但是在 ...
- checked 选择框选中
移除属性,两种方式都可 $browsers.removeAttr("checked"); $browsers.attr("checked",false); // ...
- hibernate之查询
Query对象 方便的对数据库和持久化对象进行查询,两种表达方式:HQL和SQL; Query经常用来绑定查询参数,限制查询条数.并最终执行查询语句. HQL 查询一个简单类(查询所有) @Test ...
- 大道至简第一章和java理论学时第一节。感受。
这周上了本学期的第一节java课程.课件上说了一些学习java的基本思想.举了个“愚公移山”的例子.这可能就像刚接触一门新的语言,来练习输出“HelloWorld”一样,已成惯例. “愚公移山”的这个 ...
- webView自适应及缩放
WebView wv=(WebView) findViewById(R.id.webView); wv.setVisibility(WebView.VISIBLE); WebSettings ws = ...
- CentOS下双网卡绑定-bond0
网卡绑定就是多张网卡逻辑上作为一张网卡用.可分为,负载均衡绑定和冗余绑定两种. 加载bonding驱动 #modprobe bonding 1.编辑虚拟网络接口配置文件 [root@test~]# ...
- 【转】jQuery.validate 用法
名称 返回类型 描述 validate(options) 返回:Validator ...
- Linux分区之parted命令
之前使用最多的分区命令无疑是fdisk了,大多数情况下fdisk可以满足日常工作上的需求,极个别情况就需要使用parted命令了,至于及个别情况就要从MBR和GPT说起. MBR主引导扇区 主 ...