0% found this document useful (0 votes)
31 views17 pages

Ads Assignment No. 3

Uploaded by

202AKSHATA BAGAL
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)
31 views17 pages

Ads Assignment No. 3

Uploaded by

202AKSHATA BAGAL
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/ 17

ADS ASSIGNMENT NO.

Q.1.) Define stack.


Ans :- A stack is a linear data structure that follows the Last-In, First-Out (LIFO)
principle. This means that the last item added (or "pushed") to the stack is the first
item to be removed (or "popped") from it. Imagine a stack of plates; the last plate
placed on top is the first one taken off.
Basic Operations
• Push: Add an element to the top of the stack.
• Pop: Remove the element from the top of the stack.
• Peek: View the element on the top without removing it.
• IsEmpty: Check if the stack has any elements.
Use Cases
Stacks are commonly used in:
• Undo functionality in software applications
• Backtracking algorithms, such as in mazes or games
• Expression evaluation and syntax parsing in compilers
• Call stack management in programming languages

Q.2.) Define queue.


Ans :- A queue is a linear data structure that follows the First-In, First-Out (FIFO)
principle. This means that the first item added (or "enqueued") to the queue is the
first item to be removed (or "dequeued"). Think of a line of people waiting at a
checkout; the first person in line is the first to be served.
Basic Operations
• Enqueue: Add an element to the end of the queue.
• Dequeue: Remove the element from the front of the queue.
• Peek: View the element at the front without removing it.
• IsEmpty: Check if the queue has any elements.
Use Cases
Queues are widely used in:
• Task scheduling in operating systems
• Order processing systems, such as in ticketing or reservations
• Breadth-first search (BFS) in graph traversal algorithms
• Networked data transfer where requests are handled in order

Q.3.) What is doubly linked list?


Ans :- A doubly linked list is a type of linked list in which each node contains three
parts:
1. Data: The actual value stored in the node.
2. Next Pointer: A pointer to the next node in the sequence.
3. Previous Pointer: A pointer to the previous node in the sequence.
This structure allows traversal of the list in both directions (forward and
backward), unlike a singly linked list, which can only be traversed in one
direction.
Key Characteristics
• Bidirectional Traversal: Nodes can be accessed in both forward and
backward directions.
• Flexible Insertion and Deletion: Insertion and deletion are more flexible
since each node has a pointer to its previous node, making certain
operations more efficient than in a singly linked list.
Basic Operations
• Insertions: New nodes can be added at the beginning, end, or between
nodes by adjusting both the next and previous pointers.
• Deletions: Nodes can be removed similarly by updating the next and
previous pointers of neighboring nodes.
• Traversal: You can traverse the list in both directions, starting from either
the head or tail node.
Use Cases
Doubly linked lists are used in:
• Navigation systems where bidirectional traversal is needed (like browser
history or playlist management).
• Undo/redo functionality in applications.
• Deque (double-ended queue) implementations, where elements can be
added or removed from both ends efficiently.

Q.4.) What is circular linked list?


Ans :- A circular linked list is a variation of a linked list in which the last node
points back to the first node, forming a continuous loop. This structure can be
implemented with either singly or doubly linked nodes:
• Singly Circular Linked List: The last node’s next pointer links back to the
head node, allowing traversal to loop back to the start after reaching the
end.
• Doubly Circular Linked List: Both the next pointer of the last node and the
previous pointer of the head node link to each other, creating a fully circular
structure that allows traversal in both directions.
Key Characteristics
• Circular Nature: There is no clear beginning or end, as nodes are
connected in a continuous loop.
• Traversal: Traversal can continue infinitely by looping through the nodes, so
an explicit termination condition is needed (such as reaching the starting
node again).
Basic Operations
• Insertion: New nodes can be added before the head, after the tail, or at any
position within the loop.
• Deletion: Nodes can be removed by updating pointers to skip the node to
be deleted, keeping the loop intact.
Use Cases
Circular linked lists are useful in scenarios like:
• Round-robin scheduling, where each process gets an equal share of time
in a circular order.
• Circular buffers for data streaming, where data is continuously produced
and consumed.
• Game design for implementing circular player turns, such as in board
games.

Q.5.) Write any two applications of stack.


Ans :- Two common applications of stacks are:
1. Expression Evaluation and Parsing: Stacks are used to evaluate and parse
mathematical expressions, especially in postfix (Reverse Polish
Notation) and prefix (Polish Notation) formats. Operators and operands
are pushed and popped from the stack as needed to compute the result,
allowing the expressions to be evaluated without the need for parentheses.
2. Function Call Management (Call Stack): In programming, a stack is used
to manage function calls. When a function is called, its local variables and
return address are pushed onto the stack. When the function completes,
these values are popped, and control is returned to the calling function. This
enables recursive functions to keep track of their place in the sequence of
calls.
Q.6.) Write an algorithm to create a singly linked list.
Ans :- Here’s an algorithm to create a singly linked list by adding new nodes at the
end of the list.
Algorithm to Create a Singly Linked List
1. Define a Node Structure:
o Create a node structure with two parts:
▪ Data: Stores the actual value.
▪ Next: Stores the reference (or pointer) to the next node.
2. Initialize the Linked List:
o Set the head of the linked list to NULL (or None in Python) since the
list is initially empty.
3. Insert a New Node at the End:
o Input the value to be inserted.
o Create a new node with this value and set its next pointer to NULL.
4. Check if the List is Empty:
o If the head is NULL, make the new node the head of the list.
o If the list is not empty, perform the following steps to add the node at
the end:
▪ Start from the head node.
▪ Traverse through the list until you reach the last node (where
next is NULL).
▪ Set the next pointer of the last node to the new node.
5. Repeat Step 3 for Each New Node:
o Repeat this process whenever a new value needs to be added.
Example Code for Reference (in Python)
Here's a sample implementation of this algorithm in Python:
In this example:
• A singly linked list is created by inserting nodes at the end.
• The linked list nodes will be structured as: 10 -> 20 -> 30 -> None.
Q.7.) Explain operations on doubly ended queue with an example.
Ans :- A deque (doubly-ended queue) is a data structure that allows insertion and
deletion at both ends: the front and the rear. This flexibility makes it useful for a
range of applications, including scenarios where both FIFO (first-in, first-out) and
LIFO (last-in, first-out) access patterns are needed.
Basic Operations on a Deque
1. Insert at the Front: Adds an element at the front of the deque.
2. Insert at the Rear: Adds an element at the rear of the deque.
3. Delete from the Front: Removes the element from the front of the deque.
4. Delete from the Rear: Removes the element from the rear of the deque.
5. Peek at Front: Views the element at the front without removing it.
6. Peek at Rear: Views the element at the rear without removing it.
7. IsEmpty: Checks if the deque is empty.
8. IsFull (in a bounded deque): Checks if the deque is full (for fixed-size
implementations).
Example of Operations on a Deque
Consider a deque initially empty. We’ll perform a series of operations to
demonstrate its functionality:
1. Insert at Rear: Add 10 at the rear.
o Deque: 10
2. Insert at Rear: Add 20 at the rear.
o Deque: 10, 20
3. Insert at Front: Add 5 at the front.
o Deque: 5, 10, 20
4. Delete from Rear: Remove the element at the rear (20).
o Deque: 5, 10
5. Insert at Front: Add 1 at the front.
o Deque: 1, 5, 10
6. Delete from Front: Remove the element at the front (1).
o Deque: 5, 10
7. Peek at Front: View the element at the front (5) without removing it.
o Front element: 5
8. Peek at Rear: View the element at the rear (10) without removing it.
o Rear element: 10
Implementation Example in Python (using collections.deque)
The collections.deque library in Python supports deque operations efficiently.
This code illustrates the basic operations on a deque and how elements can be
added or removed from either end.

Q.8.) Write an algorithm to search a given element in a singly linked list.


Ans :- Here’s an algorithm to search for a specific element in a singly linked list.
The algorithm traverses the list from the head node to the end, comparing each
node’s data with the target element.
Algorithm to Search an Element in a Singly Linked List
1. Initialize:
o Start from the head of the linked list.
o Set a position counter to 1 (or 0, depending on indexing) to keep
track of the node's position.
2. Traverse the List:
o While the current node is not NULL:
▪ If the data in the current node matches the target element:
▪ Return the position of the current node.
▪ Otherwise, move to the next node and increment the position
counter.
3. Element Not Found:
o If you reach the end of the list (i.e., the current node is NULL) without
finding the target element, return a message indicating that the
element is not found.
Example Code for Reference (in Python)
Here’s a Python implementation based on this algorithm:
Python
Explanation of the Code:
• append(data): Adds new nodes at the end of the list.
• search(target): Traverses the list to find the target element, returning its
position if found, or -1 if not.
In this example:
• The linked list contains nodes with values 10, 20, and 30.
• Searching for 20 returns position 2 since it is the second node in the list.

Q.9.) Draw the stack status for the flowing given operations:
i. Push(a)
ii. Push(b)
iii. Push(c)
iv. Push(d)
v. Pop()
vi. Pop()
vii Pop()
Ans :- Here is the stack status after each operation in sequence:
Stack Operations and Status:
1. Push(a)
o Stack

2. Push(b)
• Stack

3. Push(c)
• Stack
4. Push(d)
• Stack

5. Pop()
• Removes d (top element).
• Stack

6. Pop()
• Removes c (top element).
• Stack

7. Pop()
• Removes b (top element).
• Stack

Final Stack Status


After all operations, only a remains in the stack.

Q.10.) Write the algorithm to insert and delete an element in a simple queue.
Ans :- Here’s an algorithm for inserting and deleting elements in a simple queue
(FIFO - First-In, First-Out). A simple queue allows insertion at the rear and deletion
from the front.
Queue Insertion (Enqueue) Algorithm
1. Check for Queue Overflow:
o If the rear pointer is at the maximum capacity (MAX - 1), the queue is
full. Print an error message: "Queue Overflow".
2. Insert the Element:
o If the queue is empty (when front is -1), set front to 0.
o Increment the rear pointer by 1.
o Insert the element at the position indicated by rear.
3. End.
Queue Deletion (Dequeue) Algorithm
1. Check for Queue Underflow:
o If front is -1 (or front > rear), the queue is empty. Print an error
message: "Queue Underflow".
2. Delete the Element:
o Retrieve the element at the position indicated by front.
o If front is equal to rear (i.e., there’s only one element left), set both
front and rear to -1 to reset the queue.
o Otherwise, increment the front pointer by 1 to move to the next
element in the queue.
3. End.
Example Code for Reference (in Python)
Explanation of the Code:
• enqueue(element): Checks for overflow, then adds an element to the rear.
• dequeue(): Checks for underflow, then removes an element from the front.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy