线性数据结构之栈——Stack
Linear data structures
linear structures can be thought of as having two ends, whose items are ordered on how they are added or removed.
What distinguishes one linear structure from another is the way in which items are added and removed, in particular the location where these additions and removals occur. these variations give rise to some of the most useful data structures in computer science:
stacks
queues
deques
lists
Stack
what is a stack?
A stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. This end is commonly referred to as the "top". The end opposite the top is known as the "base".
In a stack, the most recently added item is the one that is in position to be removed first. This ordering principle is sometimes called LIFO, last-in first-out.
Examples of stacks occur in everyday situations:
a stack of trays or plates
a stack of books on a desk
The reversal property of stack
Stacks are fundamentally important, as they can be used to reverse the order of items. The order of insertion is the reverse of the order of removal:

Examples:
when navigating web pages, the URLs are going on the stack
the Python data object stack
when running programs, the instructions are going on the stack
The stack abstract data type
Abstract data type (ADT)
What is ADT?
A logical description of how we view the data and the operations that are allowed.
Information hiding
The user interacts with the interface, using hte operations that have been specified by the abstract data type. The user is not concerned with the details of the implementation of the ADT:

data structure
The implementation of an ADT, often referred to as a data structure, will require that we provide a physical view of the data using some collection of programming constructs and primitive data types.
There will usually be many different ways to implement an ADT.
The big picture
The idea of abstraction data type provides an implementation-independent view of the data. The user can remain focused on the problem-solving process without getting lost in the details.
By creating models of the problem domain, we are able to utilize a better and more efficient problem-solving process.
The stack operations
Stacks are ordered LIFO, the operations includes:
- Stack()
- push()
- pop()
- peek()
- isEmpty()
- size()
| Stack Operation | Stack Content | Return Value |
|---|---|---|
s.isEmpty() |
[] |
True |
s.push(4) |
[4] |
|
s.push('dog') |
[4, 'dog'] |
|
s.peek() |
[4, 'dog'] |
'dog' |
s.push(True) |
[4, 'dog', True] |
|
s.size() |
[4, 'dog', True] |
3 |
s.isEmpty() |
[4, 'dog', True] |
False |
s.push(8.4) |
[4, 'dog', True, 8.4] |
|
s.pop() |
[4, 'dog', True] |
8.4 |
s.pop() |
[4, 'dog'] |
True |
s.size() |
[4, 'dog'] |
2 |
Implementing a Stack in Python
In any object-oriented programming language, the implementation of choice for an abstract data type is the creation of a new class. The operations of ADT are implemented as methods.
class Stack:
"""the implementation of stack structure in python"""
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
return
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
Simple Balanced Parentheses Checker
using stacks to solve a real computer science problem that how to check whether parentheses are correctly balanced or unbalanced in programming language structrues.
def is_par_balanced(str_with_par):
s = Stack()
for item in str_with_par:
if item == '(':
s.push(item)
elif item == ')':
if s.isEmpty():
return False
else:
s.pop()
else:
pass
return s.isEmpty()
线性数据结构之栈——Stack的更多相关文章
- Python与数据结构[1] -> 栈/Stack[0] -> 链表栈与数组栈的 Python 实现
栈 / Stack 目录 链表栈 数组栈 栈是一种基本的线性数据结构(先入后出FILO),在 C 语言中有链表和数组两种实现方式,下面用 Python 对这两种栈进行实现. 1 链表栈 链表栈是以单链 ...
- 数据结构之栈(Stack)
什么是栈(Stack) 栈是一种遵循特定操作顺序的线性数据结构,遵循的顺序是先进后出(FILO:First In Last Out)或者后进先出(LIFO:Last In First Out). 比如 ...
- 数据结构11: 栈(Stack)的概念和应用及C语言实现
栈,线性表的一种特殊的存储结构.与学习过的线性表的不同之处在于栈只能从表的固定一端对数据进行插入和删除操作,另一端是封死的. 图1 栈结构示意图 由于栈只有一边开口存取数据,称开口的那一端为“栈顶”, ...
- [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...
- Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...
- [置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)
栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表.栈是一种数据结构,它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据.栈 ...
- C# 数据结构 栈 Stack
栈和队列是非常重要的两种数据结构,栈和队列也是线性结构,线性表.栈和队列这三种数据结构的数据元素和元素的逻辑关系也相同 差别在于:线性表的操作不受限制,栈和队列操作受限制(遵循一定的原则),因此栈和队 ...
- 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现
本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是 ...
- 数据结构—栈(Stack)
栈的定义--Stack 栈是只允许在末端进行插入和删除的线性表.栈具有后进先出的特性(LIFO ,Last In Fast Out). 学过数据结构的人都知道:栈可以用两种方式来实现,一种方法是用数组 ...
随机推荐
- C#基础篇 - 理解委托和事件
1.委托 委托类似于C++中的函数指针(一个指向内存位置的指针).委托是C#中类型安全的,可以订阅一个或多个具有相同签名方法的函数指针.简单理解,委托是一种可以把函数当做参数传递的类型.很多情况下,某 ...
- 初步认识TDD
TDD,测试驱动开发(Test Driven Development)是极限编程中倡导的程序开发方法,以其倡导先写测试程序,然后编码实现其功能得名.本文将对TDD有一个较为系统的认识. 基础属性 ...
- SDWebImage源码解读 之 UIImage+GIF
第二篇 前言 本篇是和GIF相关的一个UIImage的分类.主要提供了三个方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name ----- 根据名 ...
- 前端HTML5几种存储方式的总结
接下来要好好总结一些知识,秋招来啦...虽然有好多知识都不大会,但是还是要努力一下,运气这种东西,谁知道呢~ 总体情况 h5之前,存储主要是用cookies.cookies缺点有在请求头上带着数据,大 ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- 【SAP业务模式】之ICS(六):发票输出类型
这篇开始主要讲述发票输出类型: 首先我们新建一个发票类型,用于公司间的发票MIV,而标准的发票类型还是F2保持不变: 一.新建发票类型: 目录:SPRO-销售与分销-出具发票-开票凭证-定义出具发票类 ...
- Atitit.软件开发的三层结构isv金字塔模型
Atitit.软件开发的三层结构isv金字塔模型 第一层,Implements 层,着重与功能的实现.. 第二次,spec层,理论层,设计规范,接口,等.流程.方法论 顶层,val层,价值观层,原则, ...
- Form 表单提交参数
今天因为要额外提交参数数组性的参数给form传到后台而苦恼了半天,结果发现,只需要在form表单对应的字段html空间中定义name = 后台参数名 的属性就ok了. 后台本来是只有模型参数的,但是后 ...
- 在Linux配置Nginx web服务器步骤
系统环境:centos7 需要软件:nginx-1.3.16.tar.gz libevent-2.0.21-stable.tar.gz Pcre 和 pcre-devel nginx下载地址:h ...