• Stack & Queue Implementations
  1. FixedCapacityQueue
    1. package cn.edu.tsinghua.stat.mid_term;
      
      import java.util.Objects;
      
      /**
      * Created by shuaiyi on 04/11/2017.
      */
      public class FixedCapacityQueue<T> {
      private int size;
      private int head;
      private int tail;
      private T[] items; FixedCapacityQueue(int cap) {
      this.size = cap;
      this.head = this.tail = 0;
      this.items = (T[]) new Object[cap];
      } private void enqueue(T item) throws Exception {
      if ((this.tail + 1) % this.size == this.head) {
      throw new Exception();
      } this.items[this.tail] = item;
      this.tail = (this.tail + 1) % this.size;
      } private T dequeue() throws Exception {
      if (this.head >= this.size || this.head == this.tail) {
      throw new Exception();
      }
      T node = this.items[this.head];
      this.head = (this.head + 1) % this.size;
      return node;
      } public static void main(String[] args) throws Exception {
      FixedCapacityQueue<String> queue = new FixedCapacityQueue<>(4);
      String[] tstCases = new String[]{"Lydia", "Tina", "Alex"};
      for (String s :
      tstCases) {
      queue.enqueue(s);
      } queue.dequeue();
      queue.enqueue("Bob"); String[] outputVerified = new String[tstCases.length];
      outputVerified[0] = queue.dequeue();
      outputVerified[1] = queue.dequeue();
      outputVerified[2] = queue.dequeue(); tstCases = new String[]{"Tina", "Alex", "Bob"}; for (int i = 0; i < 3; ++i) {
      if (Objects.equals(outputVerified[i], tstCases[i])) {
      System.out.println("Test " + i + "#: succeeded");
      } else {
      System.out.println("Test " + i + "#: failed");
      }
      }
      }
      }
  2. FixedCapacityStack
    1. package cn.edu.tsinghua.stat.mid_term;
      
      import java.util.Objects;
      
      /**
      * Created by shuaiyi on 04/11/2017.
      */
      public class FixedCapacityStack<T> {
      private int size;
      private int index;
      private T[] items; FixedCapacityStack(int cap) {
      this.size = cap;
      this.index = 0;
      this.items = (T[]) new Object[cap];
      } private void push(T item) throws Exception {
      if (this.index == this.size) {
      throw new Exception();
      } this.items[this.index++] = item;
      } private T pop() throws Exception {
      if (this.index == 0) {
      throw new Exception();
      } return this.items[--this.index];
      } public static void main(String[] args) throws Exception {
      FixedCapacityStack<String> stack = new FixedCapacityStack<>(3);
      String[] tstCases = new String[]{"Lydia", "Tina", "Alex"};
      for (String s :
      tstCases) {
      stack.push(s);
      } String[] outputVerified = new String[tstCases.length];
      outputVerified[0] = stack.pop();
      outputVerified[1] = stack.pop();
      outputVerified[2] = stack.pop(); for (int i = 0; i < 3; ++i) {
      if (Objects.equals(outputVerified[i], tstCases[2 - i])) {
      System.out.println("Test " + i + "#: succeeded");
      } else {
      System.out.println("Test " + i + "#: failed");
      }
      }
      }
      }
  3. ResizingArrayQueue
    1. package cn.edu.tsinghua.stat.mid_term;
      
      import java.util.Objects;
      
      import static java.lang.Math.min;
      
      /**
      * Created by shuaiyi on 04/11/2017.
      */
      public class ResizingArrayQueue<T> {
      private int size;
      private int head;
      private int tail;
      private T[] items; ResizingArrayQueue(int cap) {
      this.size = cap;
      this.head = this.tail = 0;
      this.items = (T[]) new Object[cap];
      } private void resize(int newCapacity) {
      T[] newItems = (T[]) new Object[newCapacity];
      for (int i = 0; i < min(this.size, newCapacity); ++i) {
      newItems[i] = this.items[i];
      }
      this.size = newCapacity;
      this.items = newItems;
      } private void enqueue(T item) throws Exception {
      if (this.tail == this.size) {
      resize(this.size * 2);
      } this.items[this.tail++] = item;
      } private T dequeue() throws Exception {
      if (this.head >= this.size || this.head == this.tail) {
      throw new Exception();
      } int length = this.tail - this.head;
      if (length * 4 <= this.size) {
      for (int i = this.head; i < this.tail; ++i) {
      this.items[i - this.head] = this.items[i];
      }
      resize(this.size / 2);
      this.tail = this.tail - this.head;
      this.head = 0;
      } return this.items[this.head++];
      } public static void main(String[] args) throws Exception {
      ResizingArrayQueue<String> queue = new ResizingArrayQueue<>(3);
      String[] tstCases = new String[]{"Lydia", "Tina", "Alex", "Lilith", "Bob"};
      for (String s :
      tstCases) {
      queue.enqueue(s);
      } queue.dequeue();
      queue.dequeue();
      queue.dequeue();
      queue.dequeue();
      queue.dequeue(); tstCases = new String[]{"Lydia", "Tina", "Alex", "Lilith", "Bob"};
      for (String s :
      tstCases) {
      queue.enqueue(s);
      } String[] outputVerified = new String[tstCases.length];
      outputVerified[0] = queue.dequeue();
      outputVerified[1] = queue.dequeue();
      outputVerified[2] = queue.dequeue();
      outputVerified[3] = queue.dequeue();
      outputVerified[4] = queue.dequeue(); for (int i = 0; i < 5; ++i) {
      if (Objects.equals(outputVerified[i], tstCases[i])) {
      System.out.println("Test " + i + "#: succeeded");
      } else {
      System.out.println("Test " + i + "#: failed");
      }
      }
      }
      }
  4. ResizingArrayStack
    1. package cn.edu.tsinghua.stat.mid_term;
      
      import java.util.Objects;
      
      import static java.lang.Math.min;
      
      /**
      * Created by shuaiyi on 04/11/2017.
      */
      public class ResizingArrayStack<T> {
      private int size;
      private int index;
      private T[] items; ResizingArrayStack(int cap) {
      this.size = cap;
      this.index = 0;
      this.items = (T[]) new Object[cap];
      } private void resize(int newCapacity) {
      T[] newItems = (T[]) new Object[newCapacity];
      for (int i = 0; i < min(this.size, newCapacity); ++i) {
      newItems[i] = this.items[i];
      }
      this.size = newCapacity;
      this.items = newItems;
      } private void push(T item) throws Exception {
      if (this.index == this.size) {
      resize(this.size * 2);
      } this.items[this.index++] = item;
      } private T pop() throws Exception {
      if (this.index == 0) {
      throw new Exception();
      } if (this.index * 4 <= this.size) {
      resize(this.size / 2);
      } return this.items[--this.index];
      } public static void main(String[] args) throws Exception {
      ResizingArrayStack<String> stack = new ResizingArrayStack<>(5);
      String[] tstCases = new String[]{"Lydia", "Tina", "Alex", "Bob", "Lilith"};
      for (String s :
      tstCases) {
      stack.push(s);
      } stack.pop();
      stack.pop();
      stack.pop();
      stack.pop();
      stack.pop(); tstCases = new String[]{"Lydia", "Tina", "Alex", "Bob", "Lilith"};
      for (String s :
      tstCases) {
      stack.push(s);
      } String[] outputVerified = new String[tstCases.length];
      outputVerified[0] = stack.pop();
      outputVerified[1] = stack.pop();
      outputVerified[2] = stack.pop();
      outputVerified[3] = stack.pop();
      outputVerified[4] = stack.pop(); for (int i = 0; i < 5; ++i) {
      if (Objects.equals(outputVerified[i], tstCases[4 - i])) {
      System.out.println("Test " + i + "#: succeeded");
      } else {
      System.out.println("Test " + i + "#: failed");
      }
      }
      }
      }
  5. LinkedListQueue
    1. package cn.edu.tsinghua.stat.mid_term;
      
      import java.util.Objects;
      
      /**
      * Created by shuaiyi on 04/11/2017.
      */
      public class LinkedListQueue<T> {
      private class Node{
      private T item;
      private Node next; Node(T item, Node next) {
      this.item = item;
      this.next = next;
      }
      } private Node head;
      private Node tail; LinkedListQueue() {
      this.head = this.tail = null;
      } private void enqueue(T item) {
      Node newNode = new Node(item, null); if (this.tail == null) {
      this.head = this.tail = newNode;
      } else {
      this.tail.next = newNode;
      this.tail = newNode;
      }
      } private T dequeue() throws Exception {
      if (this.head == null) {
      throw new Exception();
      } Node retNode = this.head;
      this.head = this.head.next;
      return retNode.item;
      } public static void main(String[] args) throws Exception {
      LinkedListQueue<String> queue = new LinkedListQueue<>();
      String[] tstCases = new String[]{"Lydia", "Tina", "Alex"};
      for (String s :
      tstCases) {
      queue.enqueue(s);
      } String[] outputVerified = new String[tstCases.length];
      outputVerified[0] = queue.dequeue();
      outputVerified[1] = queue.dequeue();
      outputVerified[2] = queue.dequeue(); for (int i = 0; i < 3; ++i) {
      if (Objects.equals(outputVerified[i], tstCases[i])) {
      System.out.println("Test " + i + "#: succeeded");
      } else {
      System.out.println("Test " + i + "#: failed");
      }
      }
      }
      }
  6. LinkedListStack
    1.   

      package cn.edu.tsinghua.stat.mid_term;
      
      import java.util.Objects;
      
      /**
      * Created by shuaiyi on 04/11/2017.
      */
      public class LinkedListStack<T> {
      private class Node{
      private T item;
      private Node next; Node(T item, Node next) {
      this.item = item;
      this.next = next;
      }
      } private Node head; private void push(T item) {
      this.head = new Node(item, this.head);
      } private T pop() {
      T value = this.head.item;
      this.head = this.head.next;
      return value;
      } public static void main(String[] args) {
      LinkedListStack<String> stack = new LinkedListStack<>();
      String[] tstCases = new String[]{"Lydia", "Tina", "Alex"};
      for (String s :
      tstCases) {
      stack.push(s);
      } String[] outputVerified = new String[tstCases.length];
      outputVerified[0] = stack.pop();
      outputVerified[1] = stack.pop();
      outputVerified[2] = stack.pop(); for (int i = 0; i < 3; ++i) {
      if (Objects.equals(outputVerified[i], tstCases[2 - i])) {
      System.out.println("Test " + i + "#: succeeded");
      } else {
      System.out.println("Test " + i + "#: failed");
      }
      }
      }
      }
  • Basic Sorting Algorithm Implementations
  1. package cn.edu.tsinghua.stat.mid_term;
    
    /**
    * Created by shuaiyi on 04/11/2017.
    */
    public class InsertionSort {
    private void swap(Comparable[] a, int i, int j) {
    Comparable tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
    } private static boolean less(Comparable v, Comparable w) {
    return v.compareTo(w) < 0;
    } private void sort(Comparable[] a) {
    for (int i = 1; i < a.length; ++i) {
    for (int j = i; j > 0 && less(a[j], a[j - 1]); --j) {
    swap(a, j, j - 1);
    }
    }
    } public static void main(String[] args) {
    InsertionSort insertionSort = new InsertionSort(); String[] a = new String[]{"54", "23", "98", "67", "12", "32", "23", "55", "98", "94"}; insertionSort.sort(a); for (String item :
    a) {
    System.out.print(item + ", ");
    }
    }
    }

    InsertionSort

  2. package cn.edu.tsinghua.stat.mid_term;
    
    import static java.lang.Math.min;
    
    /**
    * Created by shuaiyi on 04/11/2017.
    */
    public class SelectionSort { private void swap(Comparable[] a, int i, int j) {
    Comparable tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
    } private void sort(Comparable[] a) {
    int i, j;
    for (i = 0; i < a.length; ++i) {
    int curMinIndex = i;
    for (j = i + 1; j < a.length; ++j) {
    if (less(a[j], a[curMinIndex])) {
    curMinIndex = j;
    }
    } if (curMinIndex != i) {
    swap(a, i, curMinIndex);
    }
    }
    } private static boolean less(Comparable v, Comparable w) {
    return v.compareTo(w) < 0;
    } public static void main(String[] args) {
    SelectionSort selectionSort = new SelectionSort(); String[] a = new String[]{"54", "23", "98", "67", "12", "32", "23", "55", "98", "94"}; selectionSort.sort(a); for (String item :
    a) {
    System.out.print(item + ", ");
    }
    } }

    SelectionSort

  3. package cn.edu.tsinghua.stat.mid_term;
    
    /**
    * Created by shuaiyi on 04/11/2017.
    */
    public class MergeSort { private Comparable[] aux; public MergeSort(int cap) {
    aux = new Comparable[cap];
    } private void swap(Comparable[] a, int i, int j) {
    Comparable tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
    } private static boolean less(Comparable v, Comparable w) {
    return v.compareTo(w) < 0;
    } private void sort(Comparable[] a) {
    sort(a, 0, a.length - 1);
    } private void sort(Comparable[] a, int low, int high) {
    if (low >= high) {
    return;
    }
    int mid = (low + high) / 2;
    sort(a, low, mid);
    sort(a, mid + 1, high);
    merge(a, low, high);
    } private void merge(Comparable[] a, int low, int high) {
    int mid = (low + high) / 2;
    int lft_index = low;
    int right_index = mid + 1; for (int k = low; k <= high; ++k) {
    this.aux[k] = a[k];
    } int flag = lft_index;
    while(lft_index <= mid && right_index <= high) {
    if (less(this.aux[lft_index], this.aux[right_index])) {
    a[flag++] = this.aux[lft_index++];
    } else {
    a[flag++] = this.aux[right_index++];
    }
    } while(lft_index <= mid) {
    a[flag++] = this.aux[lft_index++];
    } while(right_index <= high) {
    a[flag++] = this.aux[right_index++];
    }
    } private void officialMerge(Comparable[] a, int low, int high) {
    int mid = (low + high) / 2;
    int i = low;
    int j = mid + 1; for (int k = low; k <= high; ++k) {
    this.aux[k] = a[k];
    } for (int k = low; k <= high; ++k) {
    if (i > mid) {
    a[k] = this.aux[j++];
    } else if (j > high) {
    a[k] = this.aux[i++];
    } else if (less(this.aux[j], this.aux[i])) {
    a[k] = this.aux[j++];
    } else {
    a[k] = this.aux[i++];
    }
    }
    } public static void main(String[] args) { String[] a = new String[]{"8", "3", "1", "4", "6", "5", "9", "7", "2", "0"}; MergeSort mergeSort = new MergeSort(a.length); mergeSort.sort(a); for (String item :
    a) {
    System.out.print(item + ", ");
    }
    }
    }

    MergeSort

  4. package cn.edu.tsinghua.stat.mid_term;
    
    /**
    * Created by shuaiyi on 04/11/2017.
    */
    public class QuickSort {
    private void swap(Comparable[] a, int i, int j) {
    Comparable tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
    } private static boolean less(Comparable v, Comparable w) {
    return v.compareTo(w) < 0;
    } private void sort(Comparable[] a) {
    sort(a, 0, a.length - 1);
    } private void sort(Comparable[] a, int lft, int rht) {
    if (lft >= rht) {
    return;
    }
    int mid = partition(a, lft, rht);
    sort(a, lft, mid);
    sort(a, mid + 1, rht);
    } private int partition(Comparable[] a, int lft, int rht) {
    int mid = (lft + rht) / 2;
    int cnt = 0;
    for (int k = lft; k <= rht; ++k) {
    if (less(a[k], a[mid])) {
    ++cnt;
    }
    } if (cnt + lft != mid) {
    swap(a, cnt + lft, mid);
    } mid = cnt + lft;
    int less_index = lft;
    int more_index = mid + 1;
    while (less_index < mid && more_index <= rht) {
    if (less(a[less_index], a[mid])) {
    less_index++;
    } else{
    swap(a, less_index, more_index);
    more_index++;
    }
    } return mid;
    } private int officalPartition(Comparable[] a, int lo, int hi) {
    int i = lo, j = hi + 1;
    Comparable v = a[lo];
    while (true) {
    while (less(a[++i], v)) if (i == hi) break;
    while (less(v, a[--j])) if (j == lo) break;
    if (i >= j) break;
    swap(a, i, j);
    } swap(a, lo, j);
    return j;
    } public static void main(String[] args) { String[] a = new String[]{"8", "3", "1", "4", "6", "5", "9", "7", "2", "0"}; QuickSort quickSort = new QuickSort(); quickSort.sort(a); for (String item :
    a) {
    System.out.print(item + ", ");
    }
    }
    }

    QuickSort

  5. package cn.edu.tsinghua.stat.mid_term;
    
    /**
    * Created by shuaiyi on 04/11/2017.
    */
    public class MaxPQ<T extends Comparable<T>> { private int N = 0;
    private T[] pq; MaxPQ(int cap) {
    this.pq = (T[]) new Comparable[cap + 1];
    } public void insert(T item) {
    this.pq[++N] = item;
    swim(N);
    } public T delMax() {
    T maxNode = this.pq[1];
    swap(this.pq, 1, N);
    this.pq[N] = null;
    N--;
    sink(1);
    return maxNode;
    } public void swap(Comparable[] a, int i, int j) {
    Comparable tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
    } public static boolean less(Comparable v, Comparable w) {
    return v.compareTo(w) < 0;
    } public void swim(int k) {
    while(k > 1 && less(this.pq[k / 2], this.pq[k])) {
    swap(this.pq, k/2, k);
    k /= 2;
    }
    } public void sink(int k) {
    while(k * 2 <= N) {
    int child = k * 2;
    if (child + 1 <= N && less(this.pq[child], this.pq[child + 1])) {
    child = k * 2 + 1;
    }
    if (less(this.pq[k], this.pq[child])) {
    swap(this.pq, k, child);
    k = child;
    } else {
    break;
    }
    }
    } public void officialSink(int k) {
    while(2 * k <=N) {
    int j = 2 * k;
    if (j < N && less(pq[j], pq[j + 1])) j++;
    if (!less(pq[k], pq[j])) break;
    swap(pq, k, j);
    k = j;
    }
    } public String[] sort() {
    String[] res = new String[this.N];
    int flag = 0;
    while (N > 0) {
    res[flag++] = (String)delMax();
    }
    return res;
    } public static void main(String[] args) { String[] a = new String[]{"8", "3", "1", "4", "6", "5", "9", "7", "2", "0"}; MaxPQ<String> maxPQ = new MaxPQ<>(a.length); for (String s :
    a) {
    maxPQ.insert(s);
    } String[]b = maxPQ.sort(); for (String item :
    b) {
    System.out.print(item + ", ");
    }
    }
    }

    MaxPQ

      

Data structure basics - Java Implementation的更多相关文章

  1. Implementing the skip list data structure in java --reference

    reference:http://www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html The link ...

  2. [Data Structure] Linked List Implementation in Python

    class Empty(Exception): pass class Linklist: class _Node: # Nonpublic class for storing a linked nod ...

  3. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  4. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  5. LeetCode Two Sum III - Data structure design

    原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...

  6. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  7. LeetCode 笔记27 Two Sum III - Data structure design

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  8. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  9. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. Spring MVC学习总结

    Spring MVC学习总结 Spring MVC学习路(一) 下载配置文件 Spring MVC学习路(二) 设置配置文件 Spring MVC学习路(三) 编写第一个demo Spring MVC ...

  2. C#入门篇6-3:字符串操作 string的ToString() Split()和Copy()方法

    //ToString()方法 public static void OutPut() { //字符型转换 转为字符串 Console.WriteLine(.ToString("n" ...

  3. IOS开发---菜鸟学习之路--(二十三)-直接利用键值对的方式来处理数据的感想

    首先声明,本文纯粹只是做为本人个人新手的理解.文中的想法我知道肯定有很多地方是错的. 但是这就是我作为一个新人的使用方法,对于大牛非常欢迎指导,对于喷子请绕道而行. 由于这是早上跟我学长讨论数据处理时 ...

  4. Bit Operation妙解算法题

    5道巧妙位操作的算法题. ***第一道*** 题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. ...

  5. unity c# 代码示例

    1. using UnityEngine; using System.Collections; public class AnimatorMove : MonoBehaviour { public f ...

  6. call、apply与bind在理解

    call() 方法在使用一个指定的 this 值和若干个指定的参数值的前提下调用某个函数或方法. fun.call(thisArg[, arg1[, arg2[, ...]]]) apply() 方法 ...

  7. 多线程(继承Thread)

    /** *Thread的常用方法 *1.start(),启动线程再执行run方法 *2.run():子线程要执行的代码放入run()方法中 *3.currentThread()静态的,调取当前线程,返 ...

  8. C#的一些基本问题

    静态类和静态变量静态类的定义:static class 类名 静态方法和变量必须使用类名来引用,而不能使用实例化后的对象,因为,静态变量不属于任何实例,而是共有的. 非静态类里面既可以定义静态方法也可 ...

  9. cf 843 A Sorting by Subsequences [建图]

    题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边 ...

  10. BZOJ4540 [Hnoi2016]序列 【莫队 + ST表 + 单调栈】

    题目 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称a[s:t]是a[ ...