Set of 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. In some cases, we may need to implement a set of stacks, where each stack has a limited capacity. When a stack reaches its capacity, a new stack is created to store additional elements. In this challenge, we will implement a SetOfStacks class that wraps a list of stacks, where each stack is bound by a capacity.


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/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/stacks_queues("`Stacks Queues`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") 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/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/stacks_queues -.-> lab-268886{{"`Set of Stacks`"}} python/conditional_statements -.-> lab-268886{{"`Set of Stacks`"}} python/lists -.-> lab-268886{{"`Set of Stacks`"}} python/tuples -.-> lab-268886{{"`Set of Stacks`"}} python/function_definition -.-> lab-268886{{"`Set of Stacks`"}} python/default_arguments -.-> lab-268886{{"`Set of Stacks`"}} python/classes_objects -.-> lab-268886{{"`Set of Stacks`"}} python/constructor -.-> lab-268886{{"`Set of Stacks`"}} python/polymorphism -.-> lab-268886{{"`Set of Stacks`"}} python/encapsulation -.-> lab-268886{{"`Set of Stacks`"}} python/raising_exceptions -.-> lab-268886{{"`Set of Stacks`"}} python/iterators -.-> lab-268886{{"`Set of Stacks`"}} python/build_in_functions -.-> lab-268886{{"`Set of Stacks`"}} end

Set Of Stacks

Problem

Implement a SetOfStacks class that wraps a list of stacks, where each stack is bound by a capacity. The class should have the following functionalities:

  • Push an element onto the top of the last stack in the list. If the last stack is full, create a new stack and add the element to the new stack.
  • Pop the top element from the last stack in the list. If the last stack is empty, remove it from the list and pop the top element from the new last stack in the list. If the list is empty, return None.
  • Pop an element from a specific stack in the list. If the stack is empty, remove it from the list. If the list is empty, return None.

Requirements

The SetOfStacks class should meet the following requirements:

  • The class should use an existing stack class.
  • All stacks in the list should be bound by the same capacity.
  • If a stack becomes full, a new stack should be created automatically to store additional elements.
  • If a stack becomes empty, it should be removed from the list.
  • If we pop on an empty stack, the method should return None.
  • The implementation should fit in memory.

Example Usage

The SetOfStacks class can be used in the following scenarios:

  • Push and pop on an empty stack.
  • Push and pop on a non-empty stack.
  • Push on a capacity stack to create a new one.
  • Pop on a stack to destroy it.

Summary

In this challenge, we implemented a SetOfStacks class that wraps a list of stacks, where each stack is bound by a capacity. The class provides functionalities to push and pop elements onto and from the stacks, and automatically creates or removes stacks as needed. The implementation meets the requirements of using an existing stack class, binding all stacks by the same capacity, and fitting in memory. The SetOfStacks class can be used in various scenarios, such as pushing and popping elements on empty or non-empty stacks, creating new stacks when a capacity is reached, and destroying stacks when they become empty.

Other Algorithm Tutorials you may like