一 栈:是一种表,限制插入和删除只能在一个位置,也即是表的末端(也是栈的顶)进行。

基本操作:push 和 pop。

二 栈的数组实现

运用数组来存储元素,和栈操作先关的是theArray(一个数组实例)和topOfStack(指向栈顶元素,对于空栈,它的值是-1)。

push操作:将某个元素 item 推入栈中,使得 topOfStack 增1然后置 theArray[topOfStack] = item.

pop操作: 将栈顶严肃弹出,我们置返回值为 theArray[topOfStack],然后 topOfStack 减1.

三 时间复杂度

显然都是常数时间运行,O(1)。

java代码实现:

 package com.xuyunyu.demo;
/**
*
* generic type class MyStack to implement the stack with array
* @author Administrator
*
* @param <AnyType>
*/
public class MyStack<AnyType>
{
/**
* the capacity of the stack
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* the array to store the elements
*/
private AnyType[] theItems;
/**
* the pointer to point to the top of the stack
*/
private int TopOfStack;
/**
* constructor
*
* to initial the stack
*/
public MyStack ()
{
clear();
}
/**
* clear the stack
*/
public void clear()
{
theItems = ( AnyType [] ) new Object[DEFAULT_CAPACITY];
TopOfStack = -1;
}
/**
* get the capacity of the stack
* @return
*/
public int size()
{
return DEFAULT_CAPACITY;
}
/**
* get the actual counts of
* the element stored in the stack
* @return
*/
public int getCountOfElement()
{
return TopOfStack + 1;
}
/**
* get the top element of the stack
* @return
*/
public AnyType getTopElement()
{
return theItems[TopOfStack];
}
/**
* judge if the stack is empty
* @return true if the stack is empty,false or not
*/
public boolean isEmpty()
{
return TopOfStack == -1;
}
/**
* judge if the stack is full
* @return true if the stack is full,false or not
*/
public boolean isFull()
{
return (TopOfStack + 1) == theItems.length;
}
/**
* push an item to the stack
* the TopOfStack INC ,and then store the item
* @param item
*/
public void push(AnyType item)
{
if(isFull())
throw new IndexOutOfBoundsException();
TopOfStack++;
theItems[TopOfStack] = item;
}
/**
* return the item on the top and then make one decrement of the TopOfStack
* @return
*/
public AnyType pop()
{
if(isEmpty() )
throw new IndexOutOfBoundsException(); AnyType ItemPulled = theItems[TopOfStack];
theItems[TopOfStack] = null;
TopOfStack--;
return ItemPulled;
} }

注意:这是一个泛型类,更加具有复用性。

四 测试代码

 package com.xuyunyu.demo;

 public class test_MyStack
{
public static void main(String[] args)
{
MyStack<Integer> stack = new MyStack<Integer>();
System.out.println("counts of the stack: " + stack.getCountOfElement());
System.out.println("size of the stack: " + stack.size());
//System.out.println("top element of the stack: " + stack.getTopElement()); stack.push(17);
System.out.println("counts of the stack: " + stack.getCountOfElement());
System.out.println("top element of the stack: " + stack.getTopElement()); stack.push(30);
System.out.println("counts of the stack: " + stack.getCountOfElement());
System.out.println("top element of the stack: " + stack.getTopElement()); stack.clear();
System.out.println("counts of the stack: " + stack.getCountOfElement()); System.out.println("push to the stack:" );
for (int i = 0; i < stack.size(); i++)
{
System.out.print(" " + i);
stack.push(i);
}
System.out.println(); System.out.println("pop from the stack:" );
for (int i = 0; i < stack.size(); i++)
{
System.out.print(" " + stack.pop());
} } }

期待输出如下:

counts of the stack: 0
size of the stack: 10
counts of the stack: 1
top element of the stack: 17
counts of the stack: 2
top element of the stack: 30
counts of the stack: 0
push to the stack
 0 1 2 3 4 5 6 7 8 9
pop from the stack
 9 8 7 6 5 4 3 2 1 0

结论:

显然实现了stack的后进先出的性质。

栈可以应用到平衡符号,中缀到后缀的转换以及方法调用上。详情可参考Mark Allen Weisss的《数据结构与算法分析——java语言描述》。 本文部分内容页参考此书。

数据结构学习笔记——stack实现(数组篇)的更多相关文章

  1. ES6中Map数据结构学习笔记

    很多东西就是要细细的品读然后做点读书笔记,心理才会踏实- Javascript对象本质上就是键值对的集合(Hash结构),但是键只能是字符串,这有一定的限制. 1234 var d = {}var e ...

  2. RX学习笔记:JavaScript数组操作

    RX学习笔记:JavaScript数组操作 2016-07-03 增删元素 unshift() 在数组开关添加元素 array.unshift("value"); array.un ...

  3. Go语言学习笔记八: 数组

    Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...

  4. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

  5. C语言学习笔记之成员数组和指针

    成员数组和指针是我们c语言中一个非常重要的知识点,记得以前在大学时老师一直要我们做这类的练习了,但是最的还是忘记了,今天来恶补一下.     单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个 ...

  6. 【Java数据结构学习笔记之二】Java数据结构与算法之队列(Queue)实现

      本篇是数据结构与算法的第三篇,本篇我们将来了解一下知识点: 队列的抽象数据类型 顺序队列的设计与实现 链式队列的设计与实现 队列应用的简单举例 优先队列的设置与实现双链表实现 队列的抽象数据类型 ...

  7. BZOJ 1061: [Noi2008]志愿者招募 [单纯形法]【学习笔记看另一篇吧】

    1061: [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3975  Solved: 2421[Submit][Stat ...

  8. 【Java数据结构学习笔记之三】Java数据结构与算法之队列(Queue)实现

      本篇是数据结构与算法的第三篇,本篇我们将来了解一下知识点: 队列的抽象数据类型 顺序队列的设计与实现 链式队列的设计与实现 队列应用的简单举例 优先队列的设置与实现双链表实现 队列的抽象数据类型 ...

  9. matlab学习笔记12单元数组和元胞数组 cell,celldisp,iscell,isa,deal,cellfun,num2cell,size

    一起来学matlab-matlab学习笔记12 12_1 单元数组和元胞数组 cell array --cell,celldisp,iscell,isa,deal,cellfun,num2cell,s ...

随机推荐

  1. 关于vs2008使用oracleclient链接oracle数据库报报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用

    用vs2008链接oracle数据库出现问题,报错OCIEnvCreate 失败,返回代码为 -1,但错误消息文本不可用,从网上找了好久方法,有两种oracle客户端文件权限,和运行vs2008以管理 ...

  2. CocoaPods详解之----进阶篇

    作者:wangzz原文地址:http://blog.csdn.net/wzzvictory/article/details/19178709转载请注明出处如果觉得文章对你有所帮助,请通过留言或关注微信 ...

  3. Quartz-2D绘图之路径(Paths)详解

    在上篇文章中,我们简单的理解了绘图上下文,今天我们来认识一下Quartz-2D中另一个重要的概念,路径(Paths). 一.理解路径 路径定义了一个或多个形状,或是子路径.一个子路径可由直线,曲线,或 ...

  4. 总结javascript继承的两种方式的N中写法

    最近翻看博客园,总结了一下javascript的继承方式:prototype和copy继承方式. 一.prototype方式 当一个函数被创建时,Function构造函数产生的函数会隐式的被赋予一个p ...

  5. 让QMainWindow也表现出QDialog的exec函数的特征

    前几天在做毕业设计项目的时候,使用的PyQt4,想实现这么样一个功能: 场景描述:主窗口a(QMainWindow类型)和主窗口b(QMainWindow),b是通过a窗口中某一个按钮弹出来的. 功能 ...

  6. JavaScript学习总结【6】、JS BOM

    1.BOM 简介 所谓的 BOM 即浏览器对象模型(Browser Object Model).BOM 赋予了 JS 操作浏览器的能力,即 window 操作.DOM 则用于创建删除节点,操作 HTM ...

  7. js学习--浏览器对象计时器setInterval()与setTimeout()的使用与区别

    一.setInterval()与setTimeout()的定义: 二.setInterval()与setTimeout()的使用:    1.setInterval()与clearInterval() ...

  8. Ext.Date 方法

    1.Ext.Date.add(date,interval,value); 提供执行基本日期运算的简便方法; date 日期对象, interval 一个有效的日期间隔枚举值, value 向当前日期上 ...

  9. php 执行linux 命令函数

    php的内置函数exec,system都可以调用系统命令(shell命令),当然还有passthru,escapeshellcmd等函数. 在很多时候利用php的exec,system等函数调用系统命 ...

  10. ubuntu 14.04安装quickbuild buildagent (二)

    使用方法: /home/carloz/programfiles/quickbuild6/buildagent/bin/agent.sh start /home/carloz/programfiles/ ...