数据结构学习笔记——stack实现(数组篇)
一 栈:是一种表,限制插入和删除只能在一个位置,也即是表的末端(也是栈的顶)进行。
基本操作: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实现(数组篇)的更多相关文章
- ES6中Map数据结构学习笔记
很多东西就是要细细的品读然后做点读书笔记,心理才会踏实- Javascript对象本质上就是键值对的集合(Hash结构),但是键只能是字符串,这有一定的限制. 1234 var d = {}var e ...
- RX学习笔记:JavaScript数组操作
RX学习笔记:JavaScript数组操作 2016-07-03 增删元素 unshift() 在数组开关添加元素 array.unshift("value"); array.un ...
- Go语言学习笔记八: 数组
Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...
- 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现
本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是 ...
- C语言学习笔记之成员数组和指针
成员数组和指针是我们c语言中一个非常重要的知识点,记得以前在大学时老师一直要我们做这类的练习了,但是最的还是忘记了,今天来恶补一下. 单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个 ...
- 【Java数据结构学习笔记之二】Java数据结构与算法之队列(Queue)实现
本篇是数据结构与算法的第三篇,本篇我们将来了解一下知识点: 队列的抽象数据类型 顺序队列的设计与实现 链式队列的设计与实现 队列应用的简单举例 优先队列的设置与实现双链表实现 队列的抽象数据类型 ...
- BZOJ 1061: [Noi2008]志愿者招募 [单纯形法]【学习笔记看另一篇吧】
1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3975 Solved: 2421[Submit][Stat ...
- 【Java数据结构学习笔记之三】Java数据结构与算法之队列(Queue)实现
本篇是数据结构与算法的第三篇,本篇我们将来了解一下知识点: 队列的抽象数据类型 顺序队列的设计与实现 链式队列的设计与实现 队列应用的简单举例 优先队列的设置与实现双链表实现 队列的抽象数据类型 ...
- 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 ...
随机推荐
- web开发基础(同步更新中)
1/Get与Post的区别 GET是我们都熟悉的.它用于请求网页文本.当你在浏览器输入harvard.edu,它会直接访问Harvard的web服务器,去GET /. 第二个最有名的是POST,它经常 ...
- 转:探讨android更新UI的几种方法
本文转自:http://www.cnblogs.com/wenjiang/p/3180324.html 作为IT新手,总以为只要有时间,有精力,什么东西都能做出来.这种念头我也有过,但很快就熄灭了,因 ...
- GDB源代码查找路径
在gdb程序的时候,有时候会发现源代码文件找不到,对于那些带调试信息的系统库或者第三方库,很多时候当你真正想gdb去追他源代码的时候你会发现gdb根本找不到这些源代码路径.这个时候有两种选择: [1] ...
- 按钮效果 css
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- jquery fancybox ie6无法显示关闭按钮
解决办法: 打开jquery.fancybox-1.3.4.css 注释掉这行就行了: .fancybox-ie6 #fancybox-close { background: transparent; ...
- 【转】Hibernate各种主键生成策略与配置详解
原文转自:Fra~~kaka's Blog 1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据库都无 ...
- 监视/etc/passwd文件是否正常
帮助监视/etc/passwd文件是否正常(P90 练习6.7) 1)找出有UID0的所有项 2)找出有重复UID的所有项 3)找出有重复登录名的所有项 4)找出没有口令的所有项 5)找出没有作废日期 ...
- MAYA 多线程
''' Usage: def timerTest(): print 'Hello World!' #create and start a timer timer = Timer(30, timerTe ...
- C#获取硬盘空间信息
/// <summary> /// 获取指定驱动器的空间总大小(单位为B) /// </summary> /// <param name="str_HardDi ...
- c++的四种强制类型转换
http://hb.qq.com/a/20110722/001452.htm ...... C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:TYPE b = (TYPE)a ...