单链表的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实现的更多相关文章

  1. [py]软件编程知识骨架+py常见数据结构

    认识算法的重要性 - 遇到问题? 学完语言,接到需求,没思路? 1.学会了语言,能读懂别人的代码, 但是自己没解决问题的能力,不能够把实际问题转换为代码,自己写出来.(这是只是学会一门语言的后果),不 ...

  2. 转 Python常见数据结构整理

    http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...

  3. 数据结构与算法——常用高级数据结构及其Java实现

    前文 数据结构与算法--常用数据结构及其Java实现 总结了基本的数据结构,类似的,本文准备总结一下一些常见的高级的数据结构及其常见算法和对应的Java实现以及应用场景,务求理论与实践一步到位. 跳跃 ...

  4. 8种常见数据结构及其Javascript实现

    摘要: 面试常问的知识点啊... 原文:常见数据结构和Javascript实现总结 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. 做前端的同学不少都是自学成才或者半路出家, ...

  5. 常见数据结构之JavaScript实现

    常见数据结构之JavaScript实现 随着前端技术的不断发展,投入到前端开发的人数也越来越多,招聘的前端职位也越来越火,大有前几年iOS开发那阵热潮.早两年,前端找工作很少问到关于数据结构和算法的, ...

  6. 数据结构-堆 Java实现

    数据结构-堆 Java实现. 实现堆自动增长 /** * 数据结构-堆. 自动增长 * */ public class Heap<T extends Comparable> { priva ...

  7. 20172332 2017-2018-2 《程序设计与数据结构》Java哈夫曼编码实验--哈夫曼树的建立,编码与解码

    20172332 2017-2018-2 <程序设计与数据结构>Java哈夫曼编码实验--哈夫曼树的建立,编码与解码 哈夫曼树 1.路径和路径长度 在一棵树中,从一个结点往下可以达到的孩子 ...

  8. 数据结构(java语言描述)

    概念性描述与<数据结构实例教程>大同小异,具体参考:http://www.cnblogs.com/bookwed/p/6763300.html. 概述 基本概念及术语 数据 信息的载体,是 ...

  9. 数据结构:JAVA实现二叉查找树

    数据结构:JAVA实现二叉查找树 写在前面 二叉查找树(搜索树)是一种能将链表插入的灵活性与有序数组查找的高效性结合在一起的一种数据结构. 观察二叉查找树,我们发现任何一个节点大于左子节点且小于其右子 ...

随机推荐

  1. AngularJS实现文件异步上传

    实现:前端AngularJs+后端servelt 依赖包:commons-fileupload.x.x.jar commons-io-x.x.jar 下载: http://commons.apache ...

  2. REST WebService与SOAP WebService的比较

    在SOA的基础技术实现方式中WebService占据了很重要的地位,通常我们提到WebService第一想法就是SOAP消息在各种传输协议上交互.近几年REST的思想伴随着SOA逐渐被大家接受,同时各 ...

  3. (CodeForces 548B 暴力) Mike and Fun

    http://codeforces.com/problemset/problem/548/B Mike and some bears are playing a game just for fun. ...

  4. hdu 1576 A/B 【扩展欧几里德】

    题目 A/9973=n 那么:n= A - A / 9973 * 9973   --① 设:A/B=x  则A=B*x,代入①  得  n=B*x-A/9973*9973 然后这个方程中的A/9973 ...

  5. 普通用户开放 sudo 权限

    大家都知道 linux 每个目录都是有权限的,所以如果要想在此目录下读写,则要有这个目录的权限,或者就是有 sudo 权限,那怎么给普通用户赋予 sudo 权限呢,下面我们来看一下: 1.先用 roo ...

  6. win10专业版激活工具很不错!

    下载链接:   http://www.yishimei.cn/?=0xj20

  7. poj3253哈夫曼树

    Fence Repair Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...

  8. springmvc配置文件web.xml详解各方总结。

    Spring分为多个文件进行分别的配置,其中在servlet-name中如果没有指定init-param属性,那么系统自动寻找的spring配置文件为[servlet-name]-servlet.xm ...

  9. Spring下配置几种常用连接池

    1.连接池概述 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池正是 ...

  10. node-webkit学习(4)Native UI API 之window

    node-webkit学习(4)Native UI API 之window 文/玄魂 目录 node-webkit学习(4)Native UI API 之window 前言 4.1  window a ...