Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Stacks | Advanced Data Structures
Algorithms and Data Structures Overview

book
Stacks

A stack data structure is a so-called LIFO (Last In, First Out) data structure. It means that the last item inserted in the stack will be the first item removed. And, of course, it is very important that we can access only the last element in a data structure exclusively when dealing with the stack.

Stack implementation

import numpy as np

class Stack:
def __init__(self):
self.stack = np.array([]) # Initialize an empty NumPy array

def push(self, item):
self.stack = np.append(self.stack, item) # Append the item to the end of the array

def pop(self):
if self.is_empty():
print("Stack is empty.") # Handle empty stack case gracefully
return None # Return None if the stack is empty

return_value = self.stack[-1] # Store the value to return
self.stack = np.delete(self.stack, -1) # Remove the last item from the array
return return_value # Return the removed item

def is_empty(self):
return self.stack.size == 0 # Check if the stack is empty


# Example usage:
stack = Stack()

stack.push(1)
stack.push(2)
stack.push(3)

print("Stack:", stack.stack)

print("Pop:", stack.pop())
print("Stack after pop:", stack.stack)

# Attempt to pop from an empty stack
stack.pop()
1234567891011121314151617181920212223242526272829303132333435363738
import numpy as np class Stack: def __init__(self): self.stack = np.array([]) # Initialize an empty NumPy array def push(self, item): self.stack = np.append(self.stack, item) # Append the item to the end of the array def pop(self): if self.is_empty(): print("Stack is empty.") # Handle empty stack case gracefully return None # Return None if the stack is empty return_value = self.stack[-1] # Store the value to return self.stack = np.delete(self.stack, -1) # Remove the last item from the array return return_value # Return the removed item def is_empty(self): return self.stack.size == 0 # Check if the stack is empty # Example usage: stack = Stack() stack.push(1) stack.push(2) stack.push(3) print("Stack:", stack.stack) print("Pop:", stack.pop()) print("Stack after pop:", stack.stack) # Attempt to pop from an empty stack stack.pop() stack.pop() print("Pop from empty stack:", stack.pop()) # Should print 'Stack is empty.'
copy

It is important to remember that a stack is an abstract data type. It means that it only defines behavior but not a concrete implementation. The stack abstract data type can be implemented either with the help of the array or linked list data structure.

Usually, in concrete implementations of the stack, two basic methods are used:

  • push() - inserts an item into a stack;
  • pop() - removes the last inserted item from a stack.

Basic operations time complexity

question mark

In a stack, which element is at the bottom?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt