0% found this document useful (0 votes)
9 views

Stack and Queues

Uploaded by

mahazafaar786
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Stack and Queues

Uploaded by

mahazafaar786
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

A Stack is a linear data structure that follows a particular order in which

the operations are performed. The order may be LIFO(Last In First


Out) or FILO(First In Last Out). LIFO implies that the element that is
inserted last, comes out first and FILO implies that the element that is
inserted first, comes out last.
What is Stack Data Structure?
A stack is a linear data structure that follows the Last-In-First-Out
(LIFO) principle. It behaves like a stack of plates, where the last plate added is the
first one to be removed.
Think of it this way:
•Pushing an element onto the stack is like adding a new plate on top.
•Popping an element removes the top plate from the stack.
Key Operations on Stack Data Structures
•Push: Adds an element to the top of the stack.
•Pop: Removes the top element from the stack.
•Peek: Returns the top element without removing it.
•IsEmpty: Checks if the stack is empty.
•IsFull: Checks if the stack is full (in case of fixed-size arrays)
Queue Data Structure

is a fundamental concept in computer science used for storing and


managing data in a specific order. It follows the principle of "First in,
First out" (FIFO), where the first element added to the queue is the
first one to be removed. Queues are commonly used in various
algorithms and applications for their simplicity and efficiency in
managing data flow.
Basic Operations of Queue
A queue is an object (an abstract data structure - ADT) that
allows the following operations:
•Enqueue: Add an element to the end of the queue
•Dequeue: Remove an element from the front of the queue
•IsEmpty: Check if the queue is empty
•IsFull: Check if the queue is full
•Peek: Get the value of the front of the queue without removing
it
Limitations of Queue
As you can see in the image below, after a bit of
enqueuing and dequeuing, the size of the queue
has been reduced.
Complexity Analysis
The complexity of enqueue and dequeue operations in a queue

using an array is O(1). If you use pop(N) in python code, then the

complexity might be O(n) depending on the position of the item to be

popped
A priority queue is a type of queue that arranges

elements based on their priority values. Elements

with higher priority values are typically retrieved or

removed before elements with lower priority

values. Each element has a priority value

associated with it. When we add an item, it is

inserted in a position based on its priority value.


Properties of Priority Queue
So, a priority Queue is an extension of the
queue with the following properties.
•Every item has a priority associated with it.
•An element with high priority is dequeued before
an element with low priority.
•If two elements have the same priority, they are
served according to their order in the queue.
Operations of a Priority Queue:
A typical priority queue supports the following operations:
1) Insertion in a Priority Queue
When a new element is inserted in a priority queue, it moves to the empty slot from top to
bottom and left to right. However, if the element is not in the correct place then it will be
compared with the parent node. If the element is not in the correct order, the elements are
swapped. The swapping process continues until all the elements are placed in the correct
position.
2) Deletion in a Priority Queue
As you know that in a max heap, the maximum element is the root node. And it will remove the
element which has maximum priority first. Thus, you remove the root node from the queue.
This removal creates an empty slot, which will be further filled with new insertion. Then, it
compares the newly inserted element with all the elements inside the queue to maintain the
heap invariant.
3) Peek in a Priority Queue
Divide and Conquer Algorithm is a problem-solving
technique used to solve problems by dividing the main
problem into subproblems, solving them individually and
then merging them to find solution to the original problem. In
this article, we are going to discuss how Divide and Conquer
Algorithm is helpful and how we can use it to solve problems.
1. Divide:
•Break down the original problem into smaller subproblems.
•Each subproblem should represent a part of the overall problem.
•The goal is to divide the problem until no further division is possible.
2. Conquer:
•Solve each of the smaller subproblems individually.
•If a subproblem is small enough (often referred to as the “base case”), we
solve it directly without further recursion.
•The goal is to find solutions for these subproblems independently.
3. Merge:
•Combine the sub-problems to get the final solution of the whole problem.
•Once the smaller subproblems are solved, we recursively combine their
solutions to get the solution of larger problem.
•The goal is to formulate a solution for the original problem by merging
the results from the subproblems.

You might also like