Disebabkan ruang artikel yang terhad, saya akan memberikan beberapa contoh pelaksanaan struktur dan algoritma data utama. Mula-mula, kami memperkenalkan beberapa struktur data teras dan algoritma, dan kemudian memberikan contoh kod Java yang sepadan.
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--; } }e
public 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; } }
melaksanakan Operasi baris gilir seperti beratur dan menyah gilir
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; } }
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; } }
Di atas adalah beberapa struktur data teras dan contoh algoritma pelaksanaan Java, saya harap ia akan membantu anda.
Atas ialah kandungan terperinci Struktur data teras JAVA dan pelaksanaan algoritma. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!