美文网首页
Java数据结构-栈

Java数据结构-栈

作者: 沐兮_d64c | 来源:发表于2018-04-12 00:21 被阅读0次

1,Java中的栈

1)Java栈是一种特殊的线性表。
栈:只能在一端进行push和pop操作eg:Stack
线性表:可以在任意位置add和remove操作eg:Vertor

2,Stack

1)Java数据结构-栈Stack
java.util.Stack<E>represents a last-in-first-out (LIFO) stack of objects一个后进先出的栈。
底层是数组protected Object[] elementData;
继承Vertor类,包含了5中栈的操作。with five operations that allow a vector to be treated as a stack.

image.png
2)public E push(E item) 入栈,调用父类Vertor中的同步方法addElement
Pushes an item onto the top of this stack.
image.png
image.png
3)public synchronized E pop() 出栈,调用父类Vertor中的removeElementAt(int index)同步方法
Removes the object at the top of this stack and returns that object
image.png
4)public synchronized E peek() 获取栈顶元素,不会移除,调用父类的elementAt方法
Looks at the object at the top of this stack without removing it from the stack.
image.png
5) public boolean empty() Tests if this stack is empty.
6)public synchroinzed int search(Object o) 栈中查找一个元素,返回第一个位置。the 1-based position from the top of the stack, return -1 indicates that the object is not on the stack. image.png

3,Vector简介

1)Vector底层是数组protected Object[] elementData;
2)线程安全的成员方法。

image.png
3)扩容elementData = Arrays.copyOf(elementData, newCapacity); image.png

4,LinkedList模拟栈

1)底层实现非线程安全的

image.png
头节点transient Node<E> first;
尾节点transient Node<E> last;
长度transient int size = 0;
2)添加一个元素
image.png image.png
3)移除一个元素
image.png
final Node<E> next = x.next;
final Node<E> prev = x.prev;
prev.next = next;
next.prev = prev;
4)获取一个元素
image.png
5)模拟栈
push: image.png
pop: image.png
peek: image.png

相关文章

  • 2019-07-11—栈

    栈:Java数据结构和算法(四)——栈 string和char一般这么转化: 21、定义栈的数据结构,请在该类型中...

  • Java数据结构算法(三)树

    本文旨作于收集整理使用!! 导航 Java数据结构算法(一)链表 Java数据结构算法(二)栈和队列 Java数据...

  • Java数据结构算法(四)图

    本文旨作于收集整理使用!! 导航 Java数据结构算法(一)链表 Java数据结构算法(二)栈和队列 Java数据...

  • Java数据结构算法(五)排序

    算法这点粗略整理一下,后面完善 Java数据结构算法(一)链表 Java数据结构算法(二)栈和队列 Java数据结...

  • Android面试题总结(题目+复习链接)

    数据结构 1.栈实现原理 java数据结构与算法之栈(Stack)设计与实现 - CSDN博客 2.链表实现原理 ...

  • 《数据结构与算法之美》- 栈

    栈,在这里说的是一种数据结构。 你还可能知道的栈 提到“栈”,做Java的同学还会想起Java内存模型中的“栈”,...

  • Java核心类库—— 数据结构

    Java核心类库-------数据结构体系图 1.数据结构 2.栈 3.哈希表

  • 复习

    数据结构 数据结构 集合常见数据结构:集合,链表,队列,数组,栈,映射java中:List列表,Set集合,Map...

  • Java数据类型

    Java数据结构中常用的数据结构包含如下8种: 1:数组(Array) 2:栈(Stack) 3:队列(Queue...

  • Java工程师面试题

    JavaOOP Java的数据结构有哪些? 线性表(ArrayList) 链表(LinkedList) 栈(Sta...

网友评论

      本文标题:Java数据结构-栈

      本文链接:https://www.haomeiwen.com/subject/zkmzhftx.html