Aufgrund des begrenzten Platzes des Artikels werde ich einige Implementierungsbeispiele für wichtige Datenstrukturen und Algorithmen bereitstellen. Zunächst werden mehrere Kerndatenstrukturen und Algorithmen vorgestellt und anschließend entsprechende Java-Codebeispiele gegeben.
Array
public class DynamicArray<T> { private Object[] array; private int size; private int capacity; public DynamicArray() { capacity = 10; array = new Object[capacity]; size = 0; } public void add(T element) { if (size == capacity) { capacity *= 2; Object[] newArray = new Object[capacity]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } array[size++] = element; } public T get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); return (T) array[index]; } public void remove(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); for (int i = index; i < size - 1; i++) { array[i] = array[i + 1]; } size--; } }
verknüpfte Liste
implementieren Sie die Operationen der verknüpften Liste hinzufügen, löschen, ändern und überprüfen. Operationpublic class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class LinkedList { private ListNode head; public void addAtHead(int val) { ListNode newHead = new ListNode(val); newHead.next = head; head = newHead; } public void addAtTail(int val) { if (head == null) { head = new ListNode(val); } else { ListNode current = head; while (current.next != null) { current = current.next; } current.next = new ListNode(val); } } public void deleteAtIndex(int index) { if (index == 0) { head = head.next; return; } int count = 0; ListNode current = head; ListNode prev = null; while (current != null && count < index) { prev = current; current = current.next; count++; } if (current != null) { prev.next = current.next; } } public ListNode get(int index) { ListNode current = head; int count = 0; while (current != null && count < index) { current = current.next; count++; } return current; } }
public class ArrayStack { private int[] array; private int top; private int capacity; public ArrayStack(int capacity) { this.capacity = capacity; array = new int[capacity]; top = -1; } public void push(int value) { if (top == capacity - 1) throw new IllegalStateException("Stack is full"); array[++top] = value; } public int pop() { if (top == -1) throw new IllegalStateException("Stack is empty"); return array[top--]; } public int peek() { if (top == -1) throw new IllegalStateException("Stack is empty"); return array[top]; } public boolean isEmpty() { return top == -1; } }
implement Bubble-Sortierung, Insertionssorte, Auswahlsorten, Schnellsorten und andere Algorithmen
public class ArrayQueue { private int[] array; private int front; private int rear; private int capacity; public ArrayQueue(int capacity) { this.capacity = capacity; array = new int[capacity]; front = 0; rear = -1; } public void enqueue(int value) { if (rear == capacity - 1) throw new IllegalStateException("Queue is full"); array[++rear] = value; } public int dequeue() { if (isEmpty()) throw new IllegalStateException("Queue is empty"); int value = array[front]; front++; return value; } public int peek() { if (isEmpty()) throw new IllegalStateException("Queue is empty"); return array[front]; } public boolean isEmpty() { return front > rear; } }
Das obige ist der detaillierte Inhalt vonJAVA-Kerndatenstruktur und Algorithmusimplementierung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!