数据结构学习笔记——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 ...
随机推荐
- Visual C++ 编程实现Soft AP (HostedNetwork / 承载网络) 功能
从Windows 7开始,微软在操作系统中加入的Soft AP功能,使用户能够通过无线网卡,开启虚拟AP,从而实现网络共享.Soft AP又称HostedNetwork(承载网络),在Windows ...
- Fatal error: Uncaught SoapFault exception
Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in login\login.php ...
- Eclipse Git和sourceTree用法
Eclipse Git和sourceTree用法 Eclipse Git: 提交代码到git: 1.team->Repository->pull 若没有冲突: 2.team->com ...
- chromium安装flash
sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-nonfree --install Flash ...
- Dice (III) 概率dp
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...
- 探讨 yum 与 rpm 的安装包数量
安装包数量不相等 [root@localhost ~]# rpm -qa | wc –l #列出所有被安装的rpm package 422 [root@localhost ~]# yum list i ...
- linux 的一些 不常见的指标
1. linux 的理论 最大用户数 2^32 -1 数据来源 linux就是这个范 (没验证) 2. mv 竟然不能修改文件更新时间
- 1.1机器学习基础-python深度机器学习
参考彭亮老师的视频教程:转载请注明出处及彭亮老师原创 视频教程: http://pan.baidu.com/s/1kVNe5EJ 1. 课程介绍 2. 机器学习 (Machine Learning, ...
- 学习总结之Log4NET
通过在网上查找了一些资料,用了些时间学习了log4NET,做了一个小小的总结,说一下它的特点吧 首先呢log4NET是.Net下一个非常优秀的开源日志记录组件.它可以将日志分成不同等级,也可以按照我们 ...
- Hadoop 学习笔记 (十一) MapReduce 求平均成绩
china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五 84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...