0% found this document useful (0 votes)
23 views20 pages

Lec 4 - 5 - Stack and Queue 2

Uploaded by

Kaisar Soomro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views20 pages

Lec 4 - 5 - Stack and Queue 2

Uploaded by

Kaisar Soomro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Stack and Queue

Stack
• A stack is an ordered list in which insertion and deletion are done
at one end, called top.
• The last element inserted is the first one to be deleted. Hence, it is
called the Last In, First Out (LIFO) or First In, Last Out (FILO) list.
• You can visualize a stack as a collection of items that are stacked
on top of one another, like a pile of plates in a cafeteria or a stack
of books.
• When you add a new plate, it goes on the top, and when you take
one off, you take the one from the top as well.
Stack
• Special names are given to the two changes that can be made to a
stack.
• When an element is inserted in a stack, the operation is called
push, and when an element is removed from the stack, the
operation is called pop.
Stack

This Photo by Unknown Author is licensed under CC BY-SA-NC


Stack
Stack - Operations
Basic Operations
• push: Inserts data onto the stack.
• pop: Removes and returns the last (or most recent) inserted
element from the stack.
Stack - Operations
Auxiliary Operations
• peek or top: Returns the last (or most recent) inserted element
without removing it.
• size or getSize: Returns the number of elements stored in the
stack.
• isEmpty: Checks if the stack is empty
• isFull: Checks if the stack is full (for fixed-size implementations).
Stack - Operations
Time complexity
• For a well-implemented stack, the time complexity of push, pop,
and peek (and other operations like isFull, isEmpty, getSize) is
O(1) - constant time.
• The time complexity for 𝒏 push operations (or creating an entire
stack) will be O(𝒏).
• The space complexity for 𝒏 push operations (or creating an entire
stack) will be O(𝒏).
Stack - Operations
• Performing a pop operation on an empty stack will result in an
error/exception called “Empty Stack Exception” or “Stack
Underflow Exception”
• Similarly, performing a push operation on a full stack (when using
fixed array-based implementation) will result in an error/exception
called “Full Stack Exception” or “Stack Overflow Exception”.
Stack - Applications
• Function call management: Programming language runtimes use
a call stack to keep track of function calls, local variables, and
return addresses.
• Expression evaluation: Stacks are used to evaluate arithmetic
expressions, particularly in converting infix notation to postfix
notation and then evaluating it.
• Undo functionality: Many applications use a stack to implement
undo/redo features, storing previous states or actions.
• Balanced parentheses checking: Stacks are ideal for checking if
parentheses in an expression are balanced.
Stack - Applications
• Depth-First Search (DFS) in graph algorithms: DFS uses a stack
(either explicitly or through recursion) to explore paths in a graph.
• Page-visited history in a web browser.
• Matching Tags in HTML and XML.
Stack - Implementation
• A stack can be implemented using an array as well as a linked list.
• Array-based implementation
• In this approach, the stack is represented as an array where the top
pointer keeps track of the last element.
• Simple and easy to use, but the size of the stack is fixed
• Linked list-based implementation
• Here, each element in the stack is stored in a node, and each node points
to the next one (usually push is implemented as addToFront and pop as
removeFront).
• The advantage is that the stack can grow or shrink dynamically, but it has
more overhead due to pointer management.
Queue
• A queue is an ordered list in which insertions are done at one end
(called the rear or tail) and deletions are done at the other end
(called the front or head).
• It follows the First In, First Out (FIFO) or Last In, Last Out (LILO)
principle, which means the element added first will be the one to
be removed first (or the element added last will be the one to be
removed last).
• You can think of it as a line of people waiting to be served, where
the person who enters the line first is the first to be served, and
the others must wait their turn.
Queue
• Similar to Stacks, special names are given to the two changes that
can be made to a queue.
• When an element is inserted in a queue, the operation is called
enQueue, and when an element is removed from the queue, the
operation is called deQueue.
Queue - Operations
Basic Operations
• enQueue: Inserts an element at the end/rear of the queue.
• deQueue: Removes and returns the element at the front of the
queue.
Queue - Operations
Auxiliary Operations
• front or getFront or peek: Returns the element at the front
without removing it.
• getSize: Returns the number of elements stored in the queue.
• isEmpty: Check if the queue is empty.
• isFull: Checks if the queue is full (for fixed-size implementations).
Queue - Operations
Time complexity
• The time complexity of enQueue, deQueue, and front/peek (and
other operations like isFull, isEmpty, getSize) is O(1) - constant
time.
• The time complexity for 𝒏 enQueue operations (or creating an
entire queue) will be O(𝒏).
• The space complexity for 𝒏 enQueue operations (or creating an
entire queue) will be O(𝒏).
Queue - Operations
• Performing a deQueue operation on an empty queue will result in
an error/exception called “Empty Queue Exception” or “Underflow
Exception”
• Similarly, performing an enQueue operation on a full queue (when
using fixed array-based implementation) will result in an
error/exception called “Full Queue Exception” or “Overflow
Exception”.
Queue - Applications
• Task Scheduling: Operating systems use queues to manage
processes and tasks.
• Print Job Spooling: Printer queues manage the order of print jobs.
• Buffering: In computer networks, queues are used to handle data
packets.
• Customer Service Systems: Managing customer support tickets or
call center requests.
• Simulations: Modeling real-world queuing systems like traffic flow
or customer lines.
• Web Servers: Handling incoming HTTP requests.
Queue - Implementation
• A queue can be implemented using an array or a linked list.
• Array-based implementation:
• Uses a fixed-size array to store elements.
• Keeps track of front and rear indices.
• Can be implemented as a circular queue to utilize space efficiently.
• Linked list-based implementation:
• Uses a singly linked list with pointers to the front and rear nodes.
• Allows for dynamic sizing.

You might also like