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

Dsa Assignment

Uploaded by

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

Dsa Assignment

Uploaded by

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

Name: Abhijeet Dwivedi

Scholar no: 2211301116


section I

Assignment 2:

Q1: Write a short note on singly, circular, and doubly linked lists. Also, list advantages and
disadvantages of singly, circular, and doubly linked lists.
 Singly Linked List (SLL):
o A singly linked list is a sequence of nodes where each node contains data and a
pointer to the next node.
o Advantages:
 Easy to implement and efficient in memory usage.
 Insertion and deletion are efficient at the front.
o Disadvantages:
 Traversal is only in one direction.
 Insertion and deletion at the end are inefficient.
 Circular Linked List (CLL):
o Similar to a singly linked list, but the last node points back to the first node,
forming a circle.
o Advantages:
 You can traverse the entire list starting from any node.
 Efficient for scenarios where the list is cyclic, like a round-robin scheduler.
o Disadvantages:
 Slightly complex to implement and manage.
 Traversing can be confusing due to the circular nature.
 Doubly Linked List (DLL):
o In a doubly linked list, each node has two pointers: one pointing to the next node
and another pointing to the previous node.
o Advantages:
 Traversal is possible in both directions.
 Efficient insertion and deletion from both ends.
o Disadvantages:
 Requires more memory because of the additional pointer.
 More complex to implement.

Q2: Discuss advantages and disadvantages of a linked list over an array.


 Advantages:
o Dynamic size: A linked list can grow or shrink dynamically.
o Efficient insertions and deletions: These operations are faster than arrays,
especially in the middle of the list.
 Disadvantages:
o Sequential access: You cannot access an element directly; you must traverse the
list.
o More memory usage: Due to pointers, linked lists use more memory than arrays.

Q3: What are the advantages and disadvantages of stack and queue implemented using linked
lists over arrays?
 Advantages:
o Dynamic size: Stack and queue can grow and shrink dynamically when using
linked lists.
o Efficient insertions/deletions: They are more efficient than arrays, especially for
enqueueing and dequeueing.
 Disadvantages:
o More memory usage: Due to the additional pointers in linked lists.
o Slower access times: Compared to arrays.
Q4: Write the implementation procedure of basic primitive operations of the stack using:
 i. Linear Array:
o Stack operations (push and pop) are done at one end.
o Limited by a fixed size array.
 ii. Linked List:
o Stack operations grow dynamically.
o No need to worry about size limitations.
Assignment 3:

Q1: Write the implementation procedure of basic primitive operations of the Queue using:
 i. Linear Array:
o Insert (enqueue) happens at the rear, and remove (dequeue) happens at the
front.
o A fixed-size array can lead to overflow, and a circular queue implementation is
often required.
 ii. Linked List:
o In a linked list, the queue can dynamically grow.
o Enqueue happens at the end, and dequeue happens at the front.

Q2: Write a pseudo code for the following:

i. Create a doubly linked list from N numbers given by the user:


1. Start
2. Input N (number of elements)
3. Set head = NULL
4. For each number from 1 to N, repeat steps 5 to 8:
5. Create a new node with the input value
6. If head is NULL (list is empty), set head = new node
7. Else, find the last node in the list
8. Set the last node’s next pointer to the new node, and the new node’s previous pointer to
the last node
9. Stop

ii. For a given singly linked list, delete a node:

a. Delete a node at the beginning:

1. Start
2. If head is NULL (list is empty), return
3. Set temp = head
4. Move head to the next node (head = head.next)
5. Free the memory of temp (delete temp)
6. Stop

b. Delete a node at the end:

1. Start
2. If head is NULL (list is empty), return
3. If head is the only node (head.next is NULL):
- Free the head node (delete head)
- Set head = NULL
4. Else:
- Find the second last node in the list
- Set second last node’s next pointer to NULL
- Free the memory of the last node (delete the last node)
5. Stop

c. Delete a node at a given position k:

1. Start
2. If head is NULL (list is empty), return
3. If k == 1 (delete the first node):
- Call DeleteAtBeginning
4. Else:
- Traverse to the k-th node
- Set the (k-1)-th node’s next pointer to point to the (k+1)-th node
- Free the memory of the k-th node (delete the node)
5. Stop

Q3: What data structure is used in solving the Josephus problem? How to solve the Josephus
problem?

 Data structure used: A circular linked list is used to solve the Josephus problem.
 Steps to solve the Josephus problem:
1. Create a circular linked list of size N (number of people).
2. Start at the first node (the first person).
3. Move forward M-1 steps (M is the number of steps to count).
4. Remove the current node (person) and continue counting from the next node.
5. Repeat until only one node (person) remains.

You might also like