Java数据结构之链表、栈、队列、树的实现方法示例

yizhihongxing

Java数据结构之链表、栈、队列、树的实现方法示例

链表

链表是一种线性数据结构,由节点的集合构成。每个节点包含两部分,数据部分和指针部分。数据部分用于存储数据,指针部分用于指向下一个节点。

单向链表示例

public class LinkedList<E>{
  private Node<E> head;
  private int size;

  private static class Node<E>{
    E item;
    Node<E> next;
    public Node(E item,Node<E> next){
      this.item = item;
      this.next = next;
    }
  }

  public void add(E item){
    head = new Node<>(item,head);
    size++;
  }

  public E remove(){
    if(head == null) throw new NoSuchElementException();
    E item = head.item;
    head = head.next;
    size--;
    return item;
  }
}

双向链表示例

public class DoublyLinkedList<E>{
  private Node<E> head;
  private Node<E> tail;
  private int size;

  private static class Node<E>{
    E item;
    Node<E> next;
    Node<E> prev;
    public Node(E item,Node<E> next,Node<E> prev){
      this.item = item;
      this.next = next;
      this.prev = prev;
    }
  }

  public void addFirst(E item){
    Node<E> newNode = new Node<>(item,head,null);
    if(head == null){
      head = tail = newNode;
    } else {
      head.prev = newNode;
      head = newNode;
    }
    size++;
  }

  public void addLast(E item){
    Node<E> newNode = new Node<>(item,null,tail);
    if(tail == null){
      head = tail = newNode;
    } else {
      tail.next = newNode;
      tail = newNode;
    }
    size++;
  }

  public E removeFirst(){
    if(head == null) throw new NoSuchElementException();
    E item = head.item;
    head = head.next;
    head.prev = null;
    size--;
    return item;
  }

  public E removeLast(){
    if(tail == null) throw new NoSuchElementException();
    E item = tail.item;
    tail = tail.prev;
    tail.next = null;
    size--;
    return item;
  }
}

栈是一种线性数据结构,只能在一端插入、删除元素,这一端被称为栈顶。后进先出(LIFO)是它的典型特点。

数组实现示例

public class ArrayStack<E>{
  private E[] stack;
  private int top;
  private int capacity;

  @SuppressWarnings("unchecked")
  public ArrayStack(int capacity){
    this.capacity = capacity;
    stack = (E[]) new Object[capacity];
    top = -1;
  }

  public void push(E item){
    if(top == capacity - 1) throw new StackOverflowError();
    stack[++top] = item;
  }

  public E pop(){
    if(top == -1) throw new EmptyStackException();
    return stack[top--];
  }

  public E peek(){
    if(top == -1) throw new EmptyStackException();
    return stack[top];
  }

  public boolean isEmpty(){
    return top == -1;
  }

  public boolean isFull(){
    return top == capacity - 1;
  }
}

链表实现示例

public class LinkedListStack<E>{
  private Node<E> top;
  private int size;

  private static class Node<E>{
    E item;
    Node<E> next;
    public Node(E item,Node<E> next){
      this.item = item;
      this.next = next;
    }
  }

  public void push(E item){
    top = new Node<>(item,top);
    size++;
  }

  public E pop(){
    if(top == null) throw new EmptyStackException();
    E item = top.item;
    top = top.next;
    size--;
    return item;
  }

  public E peek(){
    if(top == null) throw new EmptyStackException();
    return top.item;
  }

  public boolean isEmpty(){
    return top == null;
  }

  public int size(){
    return size;
  }
}

队列

队列是一种线性数据结构,支持在队尾插入元素,在队首删除元素,先进先出(FIFO)是它的典型特点。

数组实现示例

public class ArrayQueue<E>{
  private E[] queue;
  private int front;
  private int rear;
  private int capacity;

  @SuppressWarnings("unchecked")
  public ArrayQueue(int capacity){
    this.capacity = capacity;
    queue = (E[]) new Object[capacity];
    front = rear = -1;
  }

  public void enqueue(E item){
    if(rear == capacity - 1) throw new RuntimeException("Queue full");
    queue[++rear] = item;
    if(front == -1) front = 0;
  }

  public E dequeue(){
    if(front == -1 || front > rear) throw new RuntimeException("Queue empty");
    E item = queue[front++];
    if(front > rear) front = rear = -1;
    return item;
  }

  public E peek(){
    if(front == -1 || front > rear) throw new RuntimeException("Queue empty");
    return queue[front];
  }

  public boolean isEmpty(){
    return front == -1 || front > rear;
  }

  public boolean isFull(){
    return rear == capacity - 1;
  }

  public int size(){
    if(front == -1) return 0;
    return rear - front + 1;
  }
}

链表实现示例

public class LinkedListQueue<E>{
  private Node<E> head;
  private Node<E> tail;

  private static class Node<E>{
    E item;
    Node<E> next;
    public Node(E item,Node<E> next){
      this.item = item;
      this.next = next;
    }
  }

  public void enqueue(E item){
    Node<E> newNode = new Node<>(item,null);
    if(head == null){
      head = tail = newNode;
    } else {
      tail.next = newNode;
      tail = newNode;
    }
  }

  public E dequeue(){
    if(head == null) throw new NoSuchElementException();
    E item = head.item;
    head = head.next;
    if(head == null) tail = null;
    return item;
  }

  public E peek(){
    if(head == null) throw new NoSuchElementException();
    return head.item;
  }

  public boolean isEmpty(){
    return head == null;
  }
}

树是一种非线性数据结构,由节点和边组成。每个节点只有一个父节点,但是可以有多个子节点。根节点是没有父节点的特殊节点。

二叉搜索树示例

public class BinarySearchTree<E extends Comparable<? super E>>{
  private TreeNode<E> root;

  private static class TreeNode<E>{
    E item;
    TreeNode<E> left;
    TreeNode<E> right;
    public TreeNode(E item){
      this.item = item;
    }
  }

  public void insert(E item){
    root = insert(root,item);
  }

  private TreeNode<E> insert(TreeNode<E> node,E item){
    if(node == null) return new TreeNode<>(item);
    int compareResult = item.compareTo(node.item);
    if(compareResult < 0){
      node.left = insert(node.left,item);
    } else if(compareResult > 0){
      node.right = insert(node.right,item);
    }
    return node;
  }

  public void remove(E item){
    root = remove(root,item);
  }

  private TreeNode<E> remove(TreeNode<E> node,E item){
    if(node == null) return null;
    int compareResult = item.compareTo(node.item);
    if(compareResult < 0){
      node.left = remove(node.left,item);
    } else if(compareResult > 0){
      node.right = remove(node.right,item);
    } else {
      if(node.left != null && node.right != null){
        node.item = findMin(node.right).item;
        node.right = remove(node.right,node.item);
      } else {
        node = node.left != null ? node.left : node.right;
      }
    }
    return node;
  }

  private TreeNode<E> findMin(TreeNode<E> node){
    if(node == null) return null;
    while(node.left != null){
      node = node.left;
    }
    return node;
  }

  private TreeNode<E> findMax(TreeNode<E> node){
    if(node == null) return null;
    while(node.right != null){
      node = node.right;
    }
    return node;
  }

  public boolean contains(E item){
    return contains(root,item);
  }

  private boolean contains(TreeNode<E> node,E item){
    if(node == null) return false;
    int compareResult = item.compareTo(node.item);
    if(compareResult < 0){
      return contains(node.left,item);
    } else if(compareResult > 0){
      return contains(node.right,item);
    } else {
      return true;
    }
  }
}

以上就是Java数据结构之链表、栈、队列、树的实现方法示例的详细攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java数据结构之链表、栈、队列、树的实现方法示例 - Python技术站

(0)
上一篇 2023年5月17日
下一篇 2023年5月17日

相关文章

  • Lua学习笔记之数据结构

    下面开始对”Lua学习笔记之数据结构”的完整攻略进行详细说明。 一、前言 在学习Lua时,数据结构是非常重要的一个方面,掌握了数据结构,就可以更好地编写Lua程序,提高程序的性能和可读性。本篇攻略主要介绍四种Lua数据结构:数组、表、字符串和函数,分别介绍其含义、特点、创建方法以及基本操作。 二、数组 2.1 数组的定义和创建 Lua中的数组是一种类似于C语…

    数据结构 2023年5月17日
    00
  • C语言数据结构与算法之排序总结(二)

    C语言数据结构与算法之排序总结(二) 本篇文章是关于C语言数据结构与算法中排序算法的详细讲解,涉及了八种排序算法。 冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法。在排序过程中,它会重复地交换相邻的元素,这是它名称的由来。 示例代码: c void bubble_sort(int arr[], int n) { for (int i = 0…

    数据结构 2023年5月17日
    00
  • C++如何实现BitMap数据结构

    下面我将详细讲解C++如何实现BitMap数据结构的完整攻略,包含以下几个方面: 什么是BitMap数据结构 如何使用C++实现BitMap数据结构 BitMap数据结构的应用示例说明 1. 什么是BitMap数据结构 BitMap数据结构也叫位图,是一种非常简单而高效的数据结构,它主要是用来对大量数字进行存储和操作的。所谓BitMap,就是将一个数字序列通…

    数据结构 2023年5月17日
    00
  • java实现队列queue数据结构详解

    Java实现队列(Queue)数据结构详解 什么是队列(Queue) 队列(Queue),是一种先进先出(First In First Out, FIFO)的数据结构,即最先进入队列的元素也会最先出队列。 队列具备两个基本操作:入队(enqueue)和出队(dequeue)。入队操作将元素加入到队列的尾部,出队操作将元素从队列头部取出并删除。 Java中的Q…

    数据结构 2023年5月17日
    00
  • C语言数据结构实例讲解单链表的实现

    C语言数据结构实例讲解单链表的实现 单链表是一种线性的数据结构,它由一系列节点组成,每个节点都包含一个数据域和一个指向下一个节点的指针域。单链表常用于需要频繁插入删除元素的场景中。 单链表的数据结构设计 在C语言中,我们可以使用结构体来定义单链表的节点: typedef struct node { int data; // 数据域 struct node* …

    数据结构 2023年5月17日
    00
  • 常用内核架构

      本文分享自天翼云开发者社区《常用内核架构》,作者:JackW   宏内核 应用程序调用内存分配的 API(应用程序接口)函数。 处理器切换到特权模式,开始运行内核代码。 内核里的内存管理代码按照特定的算法,分配一块内存。 把分配的内存块的首地址,返回给内存分配的 API 函数。 内存分配的 API 函数返回,处理器开始运行用户模式下的应用程序,应用程序就…

    算法与数据结构 2023年4月22日
    00
  • Java数据结构的十大排序

    Java数据结构的十大排序攻略 简介 在计算机科学中,排序算法是一种将一串数据按照特定顺序进行排列的方法,其中常见的排序算法有很多种,不同的算法适用于不同的数据类型和数据规模。Java是一种常见的编程语言,也提供了很多实现排序算法的类和方法。 本文将介绍Java数据结构的十大排序算法,分别为:插入排序、希尔排序、选择排序、冒泡排序、快速排序、归并排序、堆排序…

    数据结构 2023年5月17日
    00
  • Java常见基础数据结构

    Java常见基础数据结构攻略 Java是一种面向对象的编程语言,拥有丰富的数据结构,大多数基础数据结构都包含在Java API中。在本文中,我们将讨论Java中常见的基础数据结构,包括数组、链表、栈、队列、集合和映射。我们将探讨每种数据结构的定义、用法和基本操作,并提供两个示例说明。 数组 数组是Java中最基本的数据结构之一。它是一个有序的集合,可以包含任…

    数据结构 2023年5月17日
    00
合作推广
合作推广
分享本页
返回顶部