Wednesday, March 9, 2022

Stacks And Queues

 Does this sound familiar? But we'll look into it more.

They are a group of items. The variation is between where to insert an item and which item to remove. Stacks operate on a last-in-first-out (LIFO) basis, whereas queues operate on a first-in-first-out (FIFO) basis.


Stacks

A stack's main operations are push, pop, and is Empty.

We use the terms push to insert an item on top of the stack and pop to remove the most recently inserted one.

(Source)

-Pop

Pop not only removes but also returns the most recently inserted item.

(Source)

Consideration Of Stack With Array Implementation

       Overflow: occurs when a user attempts to insert an item into a full-stack. So, either issue an exception or employ array resizing.

       Underflow: If a user attempts to delete from an empty stack, an exception is thrown.

       Null Items: Do we allow for the insertion of null items? Yes, we do in some circumstances.

       Loitering: is the act of retaining a reference to an object when it is no longer required. When we remove an item from the stack, we simply decrement N without assigning the item to null.

Stack Analysis - Linked List Vs Arrays

Time - Linked List techniques consume constant time, however, they are nonetheless inefficient owing to object definition and dealing with links. Accessing arrays is substantially faster; it takes O (1).

Memory - Because of the size of node objects, linked lists demand extra memory (see figure below). Arrays, on the other hand, demand less memory space.

(Source)

Stack Applications

       The web browser's back button

       Compiler

       Dijkstra’s two-stack algorithm

       …and the list continues on and on.

Queues

A queue's basic operations are enqueue, dequeue, and is Empty.

-Enqueue

(Source)

If the queue was empty before inserting an item, "first" should correspond to the newly generated node as well, and so "oldlast" will be null.

-Dequeue

Dequeue not only removes the initial item added but also returns it.

If the queue is empty after removing an item, "last" should be null as well.


(Source)

Consideration Of Queues With Array Implementation

It's the same consideration that was raised with Stack.

       Underflow & Overflow: We didn't think about what happens when we put an item into a full queue or delete an item from an empty queue.

       Null Items: we allow the user to enter null values.

       Loitering: We did not remove references to items that were no longer required.

Queue Analysis — Linked List Vs Arrays

       Time is the same as it is with Stack when utilizing Linked Lists and Arrays.

       Memory works in the same way as Stack, but with Linked Lists and Arrays. It just necessitates more memory for the two points.

Conclusion

Tutort Academy is one of the leading Data Structure Tutor that offers a comprehensive certification course in System Design, Data Science, Machine Learning, Artificial Intelligence and Data Structures and Algorithms Courses. Tutort Academy has helped hundreds of candidates get their desired job roles in Startups and tip-tier MNCs.


No comments:

Post a Comment

Master Data Science with Tutort Academy's Comprehensive DSA Courses Online

  In today's rapidly evolving digital landscape, proficiency in Data Science, Artificial Intelligence (AI), and Data Structures & Al...