What does python stack mean?

藏色散人
Release: 2019-06-24 11:26:29
Original
7810 people have browsed it

What does python stack mean?

What does python stack mean?

The stack is a special list. The elements in the stack can only Access is through one end of the list, which is called the top of the stack. The stack is called a last-in-first-out (LIFO, last-in-first-out) data structure.

Since the stack has the last-in-first-out characteristic, any element that is not at the top of the stack cannot be accessed. In order to get the element at the bottom of the stack, the element above must first be removed.

The two main operations on the stack are pushing an element onto the stack and popping an element off the stack. Use the push() method to push onto the stack, and pop() to pop out of the stack.

Another commonly used operation is to preview the element at the top of the stack. Although the pop() method can access the element on the top of the stack, after calling this method, the element on the top of the stack is also permanently deleted from the stack. The peek() method only returns the top element of the stack without deleting it.

In order to record the position of the top element of the stack, and also to mark where new elements can be added, we use the variable top. When an element is pushed into the stack, the variable increases; when an element is popped from the stack, The variable decreases.

push(), pop() and peek() are the three main methods of the stack, but the stack has other methods and attributes.

stack Usual operations:

Stack()    建立一个空的栈对象
push()     把一个元素添加到栈的最顶层
pop()      删除栈最顶层的元素,并返回这个元素
peek()     返回最顶层的元素,并不删除它
isEmpty()  判断栈是否为空
size()     返回栈中元素的个数
Copy after login

Simple case and operation results:

Use python list here Implementation of object simulation stack:

class Stack: 
    """模拟栈""" 
    def __init__(self): 
        self.items = [] 

    def isEmpty(self): 
        return len(self.items)==0  

    def push(self, item): 
        self.items.append(item) 

    def pop(self): 
        return self.items.pop()  

    def peek(self): 
        if not self.isEmpty(): 
            return self.items[len(self.items)-1] 

    def size(self): 
        return len(self.items)
Copy after login

Create a stack object and add operation methods:

s=Stack() 
print(s.isEmpty()) 
s.push(4) 
s.push('dog') 
print(s.peek()) 
s.push(True) 
print(s.size()) 
print(s.isEmpty()) 
s.push(8.4) 
print(s.pop()) 
print(s.pop()) 
print(s.size())
Copy after login

Related recommendations: "Python Tutorial"

The above is the detailed content of What does python stack mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!