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

基本操作: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. 使用methodSignatureForSelector与forwardInvocation实现消息转发 (转)

    转自:http://blog.sina.com.cn/s/blog_8c87ba3b0102v006.html 在给程序添加消息转发功能以前,必须覆盖两个方法,即methodSignatureForS ...

  2. UITextField 对输入金额的约束

    [2016/1/18更新] -- 五个人辛辛苦苦干了一年的项目终于上线了,今天有空看了一下正则表达式教程,然后开始rebuild之前的种种对字符串的约束,首先就从这个金额输入框开始吧,修改后的代码如下 ...

  3. Javascript中bind()方法的使用与实现

    对于bind,我愣了下,这个方法常用在jquery中,用于为被选元素添加一个或多个事件处理程序. 查了下手册,发现bind的作用和apply,call类似都是改变函数的execute context, ...

  4. 【原创】Android 对话框的使用

    对话框即Dialog .google的官方解释:A dialog is usually a small window that appears in front of the current Acti ...

  5. 在win10 64位下编译,提示[C++ Error] E2075 Incorrect project override option: (x86)\borland\cbuilder6\lib\vcl60.csm

    options->compiler  右边有个file name 改下就好了从$(BCB)\lib\vcl60.csm 改为c:\PROGRA~1\borland\CBUILD~1\lib\vc ...

  6. QFTP走了以后QNetworkAccessManager出现了

    QNetworkAccessManager Class Header:    #include <QNetworkAccessManager>qmake:     QT += networ ...

  7. Shell之test

    test命令用法.功能:检查文件和比较值 1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2                  两个表达式 ...

  8. Silverlight中在MVVM模式下对DatagridRow选择控件封装

    在项目中,凡是涉及到表格的地方用的最多的控件,自然少不了DataGrid的身影,它明了的展示各种数据让人十分喜欢.现在要实现一个功能,使DataGrid具有全选和项选中的功能,如果在传统后台代码中完成 ...

  9. 2016030203 - 首次将内容推送到github中

    参考网址:http://www.cnblogs.com/plinx/archive/2013/04/08/3009159.html 和当你在你的github上创建repository后的提示信息如下 ...

  10. WPF DataGrid

    前台代码 <DataGrid Name="gv_GetWork" AutoGenerateColumns="False" CanUserSortColum ...