Содержание
Стек в питоне программа
- Convert Infix expression to Postfix expression
- Prefix to Infix Conversion
- Prefix to Postfix Conversion
- Postfix to Prefix Conversion
- Postfix to Infix
- Convert Infix To Prefix Notation
- Check for Balanced Brackets in an expression (well-formedness)
- Arithmetic Expression Evaluation
- Evaluation of Postfix Expression
- How to Reverse a Stack using Recursion
- Reverse individual words
- How to Reverse a String using Stack
- Reversing a Queue
- How to create mergeable stack?
- The Stock Span Problem
- Next Greater Element (NGE) for every element in given Array
- Next Greater Frequency Element
- Maximum product of indexes of next greater on left and right
- Iterative Tower of Hanoi
- Sort a stack using a temporary stack
- Reverse a stack without using extra space in O(n)
- Delete middle element of a stack
- Check if a queue can be sorted into another queue using a stack
- Check if an array is stack sortable
- Largest Rectangular Area in a Histogram using Stack
- Find maximum of minimum for every window size in a given array
- Find index of closing bracket for a given opening bracket in an expression
- Find maximum difference between nearest left and right smaller elements
- Delete consecutive same words in a sequence
- Check mirror in n-ary tree
- Reverse a number using stack
- Reversing the first K elements of a Queue
- The Celebrity Problem
- Print next greater number of Q queries
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Print ancestors of a given binary tree node without recursion
- Length of the longest valid substring
- Expression contains redundant bracket or not
- Find if an expression has duplicate parenthesis or not
- Find next Smaller of next Greater in an array
- Iterative method to find ancestors of a given binary tree
- Stack Permutations (Check if an array is stack permutation of other)
- Spaghetti Stack
- Remove brackets from an algebraic string containing + and – operators
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Convert Infix expression to Postfix expression
- Prefix to Infix Conversion
- Prefix to Postfix Conversion
- Postfix to Prefix Conversion
- Postfix to Infix
- Convert Infix To Prefix Notation
- Check for Balanced Brackets in an expression (well-formedness)
- Arithmetic Expression Evaluation
- Evaluation of Postfix Expression
- How to Reverse a Stack using Recursion
- Reverse individual words
- How to Reverse a String using Stack
- Reversing a Queue
- How to create mergeable stack?
- The Stock Span Problem
- Next Greater Element (NGE) for every element in given Array
- Next Greater Frequency Element
- Maximum product of indexes of next greater on left and right
- Iterative Tower of Hanoi
- Sort a stack using a temporary stack
- Reverse a stack without using extra space in O(n)
- Delete middle element of a stack
- Check if a queue can be sorted into another queue using a stack
- Check if an array is stack sortable
- Largest Rectangular Area in a Histogram using Stack
- Find maximum of minimum for every window size in a given array
- Find index of closing bracket for a given opening bracket in an expression
- Find maximum difference between nearest left and right smaller elements
- Delete consecutive same words in a sequence
- Check mirror in n-ary tree
- Reverse a number using stack
- Reversing the first K elements of a Queue
- The Celebrity Problem
- Print next greater number of Q queries
- Iterative Postorder Traversal | Set 2 (Using One Stack)
- Print ancestors of a given binary tree node without recursion
- Length of the longest valid substring
- Expression contains redundant bracket or not
- Find if an expression has duplicate parenthesis or not
- Find next Smaller of next Greater in an array
- Iterative method to find ancestors of a given binary tree
- Stack Permutations (Check if an array is stack permutation of other)
- Spaghetti Stack
- Remove brackets from an algebraic string containing + and – operators
- Range Queries for Longest Correct Bracket Subsequence Set | 2
Стек в Python
Стек в Python — это линейная структура данных «последним вошел — первым ушел», т.е. элемент, введенный последним, будет первым удаляемым элементом.
Операции, связанные со стеком:
Операция push в стеке
Метод push() method используется для добавления элементов в стек. Этот метод добавляет элемент в верхнюю часть стека. Мы можем использовать метод append() для добавления элементов в стек.
stack_store = [] print('current stack :', stack_store) for x in range(3): # push items into stack stack_store.append(x) print('current stack :', stack_store,'\tstack size :', len(stack_store))
pop метод
Метод pop() method используется для удаления элементов из стека. Этот метод при вызове удаляет самый верхний элемент.
stack_store= [] print('\ncurrent stack :', stack_store) print('\nPushing/Adding items to the stack. ') for x in range(3): stack_store.append(x) print('current stack :', stack_store,'\tstack size :', len(stack_store)) print('\nRemoving items from the stack. ') while len(stack_store) > 0: stack_store.pop() print('current stack after pop() operation :', stack_store)
Способы реализации
Ниже приведены способы реализации стека в Python:
Метод 1: с использованием списка
# stack using list stack_list = ["English", "Gujarati", "Hindi"] stack_list.append("Marathi") stack_list.append("Kannada") print(stack_list) print(stack_list.pop()) print(stack_list) print(stack_list.pop()) print(stack_list)
Метод 2: с использованием Deque Collection
from collections import deque stack_store = deque() stack_store.append('Delhi') stack_store.append('Satara') stack_store.append('Gujarat') print(stack_store) stack_store.pop() print(stack_store) stack_store.pop() print(stack_store) stack_store.pop() print(stack_store)
Метод 3: очереди
from queue import LifoQueue # Initializing a stack stack_store = LifoQueue(maxsize = 3) print(stack_store.qsize()) stack_store.put('1') stack_store.put('2') stack_store.put('3') print("Is the stack full?: ", stack_store.full()) print("The size of the stack is: ", stack_store.qsize()) print('\nItems poped/removed from the stack: ') print(stack_store.get()) print(stack_store.get()) print(stack_store.get()) print("\nIs the stack empty?: ", stack_store.empty())