博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线性数据结构之栈——Stack
阅读量:4914 次
发布时间:2019-06-11

本文共 3886 字,大约阅读时间需要 12 分钟。

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:

The reversal property of stack

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:

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()

转载于:https://www.cnblogs.com/fangzq/p/data-structures-stack.html

你可能感兴趣的文章
HTML嵌套Flash播放视频
查看>>
四则运算——单元测试(测试方法:Right-BICEP )
查看>>
解压版mysql安装
查看>>
Travel on the TranzAlpine Rail Line in New Zealand
查看>>
多c文件的shelloce框架探讨
查看>>
List、Map、Set
查看>>
关于iOS多线程
查看>>
[转]Linux之type命令
查看>>
flash画图API:解析obj格式
查看>>
近日国家几个简单的思考
查看>>
JBoss 7/WildFly Domain 模式怎样配置 Server 启动的 JVM 參数
查看>>
(转)通往(革命性的、不做恶的、疯狂赚钱的、自我毁灭的)Google核心之路
查看>>
hubust 1339Touring (最短路Dijkstra算法)
查看>>
实验四 小学生四则运算需求分析结对报告
查看>>
转换CLOB字段类型为VARCHAR2, lob类型不支持的sql语句
查看>>
用nRF52的RTC实现万年历
查看>>
mysql 外键约束
查看>>
CSS设置透明背景
查看>>
APP自動化測試腳本3
查看>>
[git] branch 分支操作
查看>>