前言

  听着天籁,我是个音乐迷。时间充实着,会过得很快。我马上也可以到傍晚的时候去乐室吹我心爱的萨克斯。

                    

                    嘟嘟嘟... 我会吹一首简单的歌咯,哈哈我想到了一个神奇的比喻,待会说。

栈ADT模型(又称LIFO表)

  栈(stack)插入和删除只能在一个位置上进行的表。该位置是表的末端但是叫做栈的顶(top)。基本操作:进栈(push相当于插入)和出栈(pop相当于删除)。又称LIFO表,后进先出。

      相当于

                    就想快速呼吸一样。先吸进来的空气,先呼出去。

                    你是否记住了?

栈的源码和数组实现

  java.util.Stack

   不得不申明下,小朽研究不深,如果大家看到了希望能指点指点我。有些时候,说错了,我马上会改正的。谢谢。先介绍类的结构图

   

    下面是源码 java.util.Stack

 /*
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/ package java.util; /**
* The <code>Stack</code> class represents a last-in-first-out
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
* operations that allow a vector to be treated as a stack. The usual
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
* method to <tt>peek</tt> at the top item on the stack, a method to test
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
* the stack for an item and discover how far it is from the top.
* <p>
* When a stack is first created, it contains no items.
*
* <p>A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre> {@code
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
*
* @author Jonathan Payne
* @since JDK1.0
*/
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
} /**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item); return item;
} /**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size(); obj = peek();
removeElementAt(len - 1); return obj;
} /**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size(); if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
} /**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
} /**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o); if (i >= 0) {
return size() - i;
}
return -1;
} /** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}

java.util.Stack

    ①Pushes an item onto the top of this stack.

public E push(E item) {
addElement(item);
return item;
}

    从类的结构图可以看出,addElement是Stack父类Vector封装,用于一切其子类的封装。

     #Adds the specified component to the end of this vector

public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}

ad

    ②Removes the object at the top of this stack and returns the onject that object as the value of this function

public synchronized E pop() {
E obj;
int len = size(); obj = peek();
removeElementAt(len - 1); return obj;
}

     同样,跟addElement一样removeElementAt存在Vector

       #Deletes the component at the specified index.

 public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}

     But,这个是什么 peek(),别慌它也是存在Stack类中下面我们讲这个

    ③Looks at the object at the top of this stack without remocing it

public synchronized E peek() {
int len = size(); if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}

      跟addElement,removeElementAt一样,elementAt存在Vector

        #Returns the component at the specified index.

public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
} return elementData(index);
}

   ④Tests if this stack is empty

public boolean empty() {
return size() == 0;
}

    size() 来源于Vector,用于获取大小

   ⑤Stack中其他,就不做介绍罗列下

    void Stack()  //create an empty stack
    int search(Object o)  //return the 1 - based position where an obj is on this stack

  数组实现

package sedion.jeffli.bean;

/**
* qq 1928348753
* blog http://www.cnblogs.com/Alandre/
* @author Jeff Li
*/
public class myS { Object[] data; int maxSize;
int top; public myS(int maxSize) {
this.maxSize = maxSize;
data = new Object[maxSize];
top = -1;
} /**
* 获取堆栈长度
* @return 堆栈长度
*/
public int getSize()
{
return maxSize;
} /**
* 返回栈中元素的个数
* @return 栈中元素的个数
*/
public int getElementCount()
{
return top;
} /**
* 判断栈空
* @return 栈空
*/
public boolean isEmpty()
{
return top == -1;
} /**
* 判断栈满
* @return 栈满
*/
public boolean isFull()
{
return top+1 == maxSize;
} /**
* 依次加入数据
* @param data 加入的数据
* @return 添加是否成功
*/
public boolean push(Object data) {
if(isFull())
{
System.out.println("栈已满!");
return false;
}
this.data[++top] = data;
return true;
} /**
* 从栈中取出数据
* @return 取出的数据
*/
public Object pop() throws Exception{
if(isEmpty())
{
throw new Exception("栈已空!");
}
return this.data[top--];
} /**
* 返回栈顶元素
* @return
*/
public Object peek()
{
return this.data[getElementCount()];
} public static void main(String[] args) throws Exception {
myS stack=new myS(1000);
stack.push(new String("1"));
stack.push(new String("2"));
stack.push(new String("3"));
stack.push(new String("4"));
stack.push(new String("5")); System.out.println("栈顶元素"+stack.peek()); while(stack.top>=0)
{
System.out.println("Position["+stack.top+"]:"+stack.pop());
}
}
}

栈的应用

    四则运算,计算器编程。这些,我想原理才是重要的。

    这是一些资料共享应用  

        http://blog.sina.com.cn/s/blog_69e8f4c20101308b.html

        http://justsee.iteye.com/blog/1125174   

  

寄读者,寄知识来源

   读者,你好!你我不相识,谢谢你们支持。我的梦想会越来越接近。keep on,共勉!

   知识来源 http://book.douban.com/doulist/3563883/  

        http://www.cnblogs.com/shitianzeng/articles/2336765.html

 

②泡茶看<数据结构>,喜欢看源码-栈ADT的更多相关文章

  1. 大家都能看得懂的源码 - ahooks useSet 和 useMap

    本文是深入浅出 ahooks 源码系列文章的第十篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 今天我们来聊聊 ahooks 中对 Map 和 Set 类型进行状 ...

  2. dubbo面试题,会这些说明你真正看懂了dubbo源码

    整理了一些dubbo可能会被面试的面试题,感觉非常不错.如果你基本能回答说明你看懂了dubbo源码,对dubbo了解的足够全面.你可以尝试看能不能回答下.我们一起看下有哪些问题吧? 1.dubbo中& ...

  3. 数据结构算法 - ConcurrentHashMap 源码解析

    五个线程同时往 HashMap 中 put 数据会发生什么? ConcurrentHashMap 是怎么保证线程安全的? 在分析 HashMap 源码时还遗留这两个问题,这次我们站在 Java 多线程 ...

  4. 工具 - 怎么看微信h5的源码?

    这个问题在我看网易的h5案例的时候萌生的.因为想看他的源码,但是手机微信打开肯定看不了. 以下几种看代码的方法:(页面案例用网易大大刷屏的h5<二零一六娱乐圈画卷>,真的是一个值得我等众生 ...

  5. FileInputFormat看这一段源码

    这是FileInputFormat中的一个方法,看一下它的功能,多看源码,理解hadoop,同时提高自己的java编程能力: private static String[] getPathString ...

  6. 面试官问我:看过sharding-jdbc的源码吗?我吧啦吧啦说了一通!!

    写在前面 在产品初期快速迭代的过程中,往往为了快速上线而占据市场,在后端开发的过程中往往不会过多的考虑分布式和微服务,往往会将后端服务做成一个单体应用,而数据库也是一样,最初会把所有的业务数据都放到一 ...

  7. 想要看懂鸿蒙OS源码?朱老师带你从框架分析开始

    HarmonyOS V2.0是面向轻量级设备的鸿蒙L0/L1级设备端操作系统,于2020.9开源至今已有2个多月,但是很多同学在学习鸿蒙源码时仍然感觉难以下手,找不到突破口. 2020.11.25(本 ...

  8. 小白都能看懂的Spring源码揭秘之IOC容器源码分析

    目录 前言 IOC 只是一个 Map 集合 IOC 三大核心接口 IOC 初始化三大步骤 定位 加载 注册 总结 前言 在 Spring 框架中,大家耳熟能详的无非就是 IOC,DI,Spring M ...

  9. 大家都能看得懂的源码(一)ahooks 整体架构篇

    本文是深入浅出 ahooks 源码系列文章的第一篇,该系列已整理成文档-地址.觉得还不错,给个 star 支持一下哈,Thanks. 第一篇主要介绍 ahooks 的背景以及整体架构. React h ...

随机推荐

  1. 【微信小程序开发之坑】javascript创建date对象

    最近开发中用到date,开始以如下方式来创建: var date = new Date('2018-01-30 11:00:00'); 在开发工具上,调试,ios 和 android都好好的. 在真机 ...

  2. ThinkPHP 3.2.3+ORACLE插入数据BUG修复及支持获取自增Id的上次记录

    TP+ORACLE插入数据BUG修复以及获取自增Id支持getLastInsID方法 这些天在做Api接口时候,发现用TP操作Oracle数据库,发现查询修改删除都能执行, 但一旦执行插入操作老是报错 ...

  3. 《Linux就该这么学》第十五天课程

    本次课所学习的是DNS域名解析服务! 下面提供一些DNS有关的内容 如需进一步学习,请前往https://www.linuxprobe.com/chapter-13.html 工作模式: 1.主服务器 ...

  4. apt-get update 出现错误“ AppStream cache update completed, but some metadata was ignored due to errors. ”

    只需要 执行 sudo rm /var/lib/dpkg/lock;     之后再次执行:sudo apt-get update

  5. 在香港用什么软件可以唱歌?香港K歌app推荐

    KTV的源头来自于日本,KTV是Karaok TV的缩写.随着互联网时代越来越发达,手机K歌成了很多人会选择的方式,那么在香港有什么好用的K歌软件呢?这里qt6小编给大家推荐几款好用的,让你足不出户即 ...

  6. Solaris:你好奇的十件事

    想想你周围的人,看看他们正在使用的操作系统.绝大部分人的电脑都在用主流操作系统:Windows,MacOS,甚至是Ubuntu.当说到Solaris,Unix和BSD的时候,其他人还以为你说鸟语呢.除 ...

  7. 数据库导出sql

    mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql

  8. Forward团队-爬虫豆瓣top250项目-最终程序

    托管平台地址:https://github.com/xyhcq/top250 小组名称:Forward团队 小组成员合照: 程序运行方法: 在python中打开程序并运行:或者直接执行程序即可运行 程 ...

  9. maven 项目快速下载jar方式

    maven仓库默认在国外,使用难免很慢,尤其是下载依赖的时候,换为国内镜像,让你感受飞一般的感觉.国内支持maven镜像的有阿里云,开源中国等,这里换为阿里云的. 修改maven配置文件setting ...

  10. sort()方法的应用(二)

    引用:函数作为参数 var fn_by = function(id) { return function(o, p) { var a, b; if (typeof o === "object ...