Stack

Stack

  • A linear data structure that follows the Last In, First Out (LIFO) or First In, Last Out (FILO) principle.
  • Elements are added and removed from only one end, called the top of the stack.

Common Operations

  • push: add an element
  • pop: remove the top element
  • peek: retrieve the top element without removing it

Advantages

  • Simple Implementation: Easy to implement and use.
  • Efficient Operations: Push and pop operations have O(1) time complexity.
  • Memory Management: Utilised in memory management for function calls (call stack).

Disadvantages

  • Limited Access: Only the top element can be accessed, limiting direct access to other elements.
  • Fixed Size (Array-based Stacks): For stacks implemented using arrays, the size is fixed and can lead to overflow.

Real-Life Applications

  • Function Call Management: Managing function calls and recursion in programming languages.
  • Expression Evaluation: Evaluating expressions in compilers and calculators.
  • Backtracking: Algorithms that use backtracking (e.g., maze solving, depth-first search).
  • Undo Mechanism: Implementing undo features in text editors and applications.

Implementation

class Stack:
    def __init__(self):
        self.items = []

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

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

    def pop(self):
		if self.is_empty():
			return None
        return self.items.pop()

    def peek(self):
        if self.is_empty():
            return None
        return self.items[-1]

    def size(self):
        return len(self.items)        

More code implementations (in all of your favourite languages - Java, C++, Swift) can be found here.

That's a wrap. Get subscribed if you aren't already. Come back tomorrow for more.

If you found this useful, share it with your friends.

要查看或添加评论,请登录

Kabir Asani的更多文章

  • Linked Lists

    Linked Lists

    A data structure consisting of a sequence of elements, each containing a reference (link) to the next element in the…

    1 条评论
  • Is this the Firebase killer?

    Is this the Firebase killer?

    In the landscape of app development, managing backend services efficiently can be a daunting task, particularly for…

  • Is this where developers hang out?

    Is this where developers hang out?

    In the fast-paced world of software development, collaboration and version control are not just helpful; they're…

  • What on earth are LLMs & ChatGPT?

    What on earth are LLMs & ChatGPT?

    In the ever-evolving landscape of artificial intelligence, one of the most transformative developments has been the…

  • All your actions on LinkedIn are eventually consistent!

    All your actions on LinkedIn are eventually consistent!

    ?? Important Announcement For the past couple of weeks, I have been hard at building a product that I'm so proud of…

  • Secret to Spotify's lightning fast streaming?

    Secret to Spotify's lightning fast streaming?

    In this fast-paced world of digital music, Spotify stands out as a leader, delivering millions of tracks to users…

  • Is this why UPI transactions feel so delightful?

    Is this why UPI transactions feel so delightful?

    In a world driven by speed and efficiency, capturing users' attention is crucial. One way that tech companies achieve…

  • Without this UPI wouldn't have been possible!

    Without this UPI wouldn't have been possible!

    In the digital world of payments, reliable and consistent transactions are paramount. Whether you're buying groceries…

  • Are CDNs making YouTube lightning-fast?

    Are CDNs making YouTube lightning-fast?

    In today's streaming era, seamless video delivery is essential. How do apps like YouTube or Instagram ensure latest…

  • Why doesn't Instagram's reel feed ever end?

    Why doesn't Instagram's reel feed ever end?

    You might have noticed that Instagram's reel feed can be scrolled infinitely. It's as if you simply can't reach its end.