0% found this document useful (0 votes)
15 views5 pages

Bilal Dsa Lab

Uploaded by

eshanaliprivate
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)
15 views5 pages

Bilal Dsa Lab

Uploaded by

eshanaliprivate
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/ 5

Lab Report 06: Queue Implementation with Applications

NAME: Muhammad Bilal


REGISTRATION NUMBER: FA23-BCE-061
COURSE DSA
INSTRUCTOR: Dr. Umer Ahmed
DATE: November 19, 2024
In-Lab Task 1: Implement a priority queue. Implement a priority queue with following
functions.
1. void pr_enqueue(struct node ** front, struct node ** rear, struct element new_data)
;
2. struct element pr_dequeue(struct node ** front);
3. int pr_isEmpty(struct node ** front);
Output:

Description:
• PR_ENQUEUE

• Adds an element to the queue based on its priority.


• If the queue is empty, the new node becomes both the front and rear.
• If the new element has a higher priority than the current front, it is added at the front.
• Otherwise, the function traverses the queue to find the correct position for insertion,
maintaining the priority order.

• PR_DEQUEUE

• Removes the element at the front of the queue and returns it.
• If the queue is empty, it returns a placeholder element and prints an error message.
• Handles the case where the last element is removed by setting both front and rear to null.
• PR_ISEMPTY

• Returns 1 if the queue is empty, otherwise 0.

Task 02: Find the Shortest Path in Graphs Using BFS and Queues.
Code:

Description:
The function find_shortest_path implements a Breadth-First Search (BFS) algorithm to find the
shortest path in a grid from a source cell (src_x, src_y) to a destination cell (dst_x, dst_y), ensuring
traversal is restricted to cells with a value of 0. It first validates the source and destination
coordinates, returning -1 if they are invalid or inaccessible. Using two arrays (dx and dy), it defines
four possible movement directions (up, down, left, and right). A `visited` array ensures cells are
not revisited, and a queue is used to explore cells layer by layer, where each queue entry contains
the cell's coordinates and the path cost to reach it. The algorithm explores neighboring cells and
stops once the destination is reached, returning the path cost. If no path exists, it returns -1. This
function is efficient for finding the shortest path in unweighted grid-based scenarios.

Output:
Conclusion:

In this lab, we implemented a breadth-first search (BFS) algorithm to find the shortest path in a
grid, where only cells marked with 0 are traversable. The program successfully navigated the
grid, using a queue to explore neighboring cells and determining the shortest path or returning -1
if no path existed. The lab highlighted the importance of input validation and the use of auxiliary
arrays to track visited cells. Overall, it demonstrated BFS as an effective method for solving
pathfinding problems in grid-based environments.

You might also like