>  기사  >  Java  >  JAVA 핵심 데이터 구조 및 알고리즘 구현

JAVA 핵심 데이터 구조 및 알고리즘 구현

PHPz
PHPz원래의
2023-11-08 12:35:151106검색

JAVA 핵심 데이터 구조 및 알고리즘 구현

기사 공간이 제한되어 있으므로 주요 데이터 구조와 알고리즘의 구현 예를 몇 가지 제공하겠습니다. 먼저 몇 가지 핵심 데이터 구조와 알고리즘을 소개한 다음 해당 Java 코드 예제를 제공합니다. ArrayplIMPLEMENT 동적으로 확장 된 배열 형상 배열의 추가, 삭제, 수정 및 확인 작업의 작업은 단일 링크 된 목록 이식을 사용합니다. 연결된 목록의 추가, 삭제, 수정 및 확인 작업

    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--;
        }
    }
  1. stack
    • 배열 기반 스택 구현
    • 스택 푸시, 팝 및 기타 작업 구현
    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;
        }
    }
  1. queue
    • 배열 기반 큐 구현
    • 큐 엔큐, 큐 제거 및 기타 작업 구현
    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;
        }
    }
  1. 정렬 알고리즘
    • 버블 정렬, 삽입 정렬, 선택 정렬, 퀵 정렬 및 기타 알고리즘 구현
  2. 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;
        }
    }
위 내용은 핵심 데이터 구조와 알고리즘의 Java 구현 예입니다. 도움이 되길 바랍니다.

    위 내용은 JAVA 핵심 데이터 구조 및 알고리즘 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

    성명:
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.