欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > List集合、数据结构(栈、队列、数组、链表)

List集合、数据结构(栈、队列、数组、链表)

2024/10/24 7:21:50 来源:https://blog.csdn.net/Directionboy/article/details/140966881  浏览:    关键词:List集合、数据结构(栈、队列、数组、链表)

1.List集合

1.1List集合的概述和特点

  • List集合的概述
    • 有序集合,这里的有序指的是存取顺序
    • 用户可以精确控制列表中每个元素的插入位置,用户可以通过整数索引访问元素,并搜索列表中的元素
    • 与Set集合不同,列表通常允许重复的元素
  • List集合的特点
    • 存取有序
    • 可以重复
    • 有索引

1.2List集合的特有方法

  • 方法介绍

    方法名描述
    void add(int index,E element)在此集合中的指定位置插入指定的元素
    E remove(int index)删除指定索引处的元素,返回被删除的元素
    E set(int index,E element)修改指定索引处的元素,返回被修改的元素
    E get(int index)返回指定索引处的元素
  • 示例代码

    public class MyListDemo {public static void main(String[] args) {List<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");//method1(list);//method2(list);//method3(list);//method4(list);}private static void method4(List<String> list) {//        E get(int index)		返回指定索引处的元素String s = list.get(0);System.out.println(s);}private static void method3(List<String> list) {//        E set(int index,E element)	修改指定索引处的元素,返回被修改的元素//被替换的那个元素,在集合中就不存在了.String result = list.set(0, "qqq");System.out.println(result);System.out.println(list);}private static void method2(List<String> list) {//        E remove(int index)		删除指定索引处的元素,返回被删除的元素//在List集合中有两个删除的方法//第一个 删除指定的元素,返回值表示当前元素是否删除成功//第二个 删除指定索引的元素,返回值表示实际删除的元素String s = list.remove(0);System.out.println(s);System.out.println(list);}private static void method1(List<String> list) {//        void add(int index,E element)	在此集合中的指定位置插入指定的元素//原来位置上的元素往后

2. 数据结构(栈、队列、数组、链表)

        数据结构是计算机科学中存储、组织数据的方式,以便可以高效地访问和修改数据。以下是对栈、队列、数组和链表这四种常见数据结构的介绍:

2.1 栈(Stack)

  • 栈是一种后进先出(Last In First Out, LIFO)的数据结构。
  • 它只允许在一端(称为栈顶)进行添加(push)和删除(pop)操作。
  • 栈常用于实现递归算法、函数调用的内存管理等。

        在Java中实现上述数据结构通常涉及到定义类和接口来封装数据结构的行为和属性。以下是每种数据结构的基本实现过程:

public class Stack<T> {private Node<T> top;private int size;private class Node<T> {T data;Node<T> next;public Node(T data) {this.data = data;}}public void push(T data) {Node<T> newNode = new Node<>(data);newNode.next = top;top = newNode;size++;}public T pop() {if (isEmpty()) {throw new EmptyStackException();}T data = top.data;top = top.next;size--;return data;}public boolean isEmpty() {return size == 0;}public int size() {return size;}
}

2.2 队列(Queue)

  • 队列是一种先进先出(First In First Out, FIFO)的数据结构。
  • 它允许在一端添加元素(称为队尾),在另一端删除元素(称为队首)。
  • 队列常用于任务调度、缓冲处理等场景。

 

public class Queue<T> {private Node<T> front;private Node<T> rear;private int size;private class Node<T> {T data;Node<T> next;public Node(T data) {this.data = data;}}public void enqueue(T data) {Node<T> newNode = new Node<>(data);if (rear == null) {front = rear = newNode;} else {rear.next = newNode;rear = newNode;}size++;}public T dequeue() {if (isEmpty()) {throw new NoSuchElementException();}T data = front.data;front = front.next;if (front == null) {rear = null;}size--;return data;}public boolean isEmpty() {return size == 0;}public int size() {return size;}
}

2.3 数组(Array)

  • 数组是一种线性数据结构,可以存储相同类型的元素。
  • 它通过索引来访问元素,索引通常从0开始。
  • 数组提供了快速的随机访问能力,但插入和删除操作可能需要移动大量元素。

        Java本身提供了数组的实现,但你也可以自定义一个数组类: 

public class DynamicArray<T> {private Object[] array;private int size;public DynamicArray() {this.array = new Object[10];this.size = 0;}public void add(T element) {if (size == array.length) {resize();}array[size++] = element;}private void resize() {Object[] newArray = new Object[array.length * 2];System.arraycopy(array, 0, newArray, 0, array.length);array = newArray;}public T get(int index) {if (index >= size || index < 0) {throw new IndexOutOfBoundsException();}return (T) array[index];}public int size() {return size;}
}

2.4 链表(Linked List)

  • 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。
  • 链表允许动态地添加和删除节点,但访问特定元素需要从头开始遍历。
  • 链表的变体包括单向链表、双向链表和循环链表。

        Java提供了java.util.LinkedList类,但以下是自定义链表的一个简单实现: 

public class LinkedList<T> {private Node<T> head;private int size;private class Node<T> {T data;Node<T> next;public Node(T data) {this.data = data;this.next = null;}}public void add(T data) {Node<T> newNode = new Node<>(data);if (head == null) {head = newNode;} else {Node<T> current = head;while (current.next != null) {current = current.next;}current.next = newNode;}size++;}public T remove() {if (head == null) {throw new NoSuchElementException();}T data = head.data;head = head.next;if (head == null) {size = 0;} else {size--;}return data;}public boolean isEmpty() {return size == 0;}public int size() {return size;}
}

        这些实现提供了基本的添加、删除和访问操作,你可以根据需要扩展它们以包含更多的功能。Java标准库中的java.util包提供了更全面和优化的实现,包括ArrayDequeLinkedListArrayListStack等。 

        每种数据结构都有其特定的用途和优势,选择哪种数据结构通常取决于特定问题的需求和约束。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com