Concise Introduction to Stacks

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. Other operations include peek, which returns the most recently added element without removing it, and is_empty, which checks if the stack is empty.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/stacks_queues("`Stacks Queues`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") subgraph Lab Skills algorithm/stacks_queues -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/conditional_statements -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/tuples -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/function_definition -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/default_arguments -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/classes_objects -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/constructor -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/polymorphism -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/encapsulation -.-> lab-268889{{"`Concise Introduction to Stacks`"}} python/iterators -.-> lab-268889{{"`Concise Introduction to Stacks`"}} end

Stack

Problem

Implement a stack using a linked list in Python, with the following methods:

  • push: adds an element to the top of the stack
  • pop: removes and returns the element at the top of the stack. If the stack is empty, return None.
  • peek: returns the element at the top of the stack without removing it. If the stack is empty, return None.
  • is_empty: returns True if the stack is empty, False otherwise.

Requirements

The following requirements should be met:

  • When popping an empty stack, return None.
  • The implementation should use a linked list.
  • The implementation should be in Python.
  • The implementation should include the four methods: push, pop, peek, and is_empty.

Example Usage

Push

  • Push to empty stack: stack.push(1)
  • Push to non-empty stack: stack.push(2)

Pop

  • Pop on empty stack: stack.pop() -> None
  • Pop on single element stack: stack.pop() -> 1
  • Pop on multiple element stack: stack.pop() -> 2

Peek

  • Peek on empty stack: stack.peek() -> None
  • Peek on one or more element stack: stack.peek() -> 2

Is Empty

  • Is empty on empty stack: stack.is_empty() -> True
  • Is empty on one or more element stack: stack.is_empty() -> False

Summary

In this Python challenge, we implemented a stack using a linked list and four methods: push, pop, peek, and is_empty. We also provided example usage and requirements for the implementation.

Other Algorithm Tutorials you may like