常见数据结构的Java实现

单链表的Java实现

首先参考wiki上的单链表说明,单链表每个节点包含数据和指向链表中下一个节点的指针或引用。然后看代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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实现

循环方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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实现

quick-sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
 }
}

posted on   Mainz  阅读(822)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统

统计

点击右上角即可分享
微信分享提示