线性数据结构之栈——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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
· 电商平台中订单未支付过期如何实现自动关单?
· .NET 阻止Windows关机以及阻止失败的一些原因
· 博客园2025新款「AI繁忙」系列T恤上架
· Avalonia跨平台实战(二),Avalonia相比WPF的便利合集(一)
· C# LINQ 快速入门实战指南,建议收藏学习!
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(6)