DS Assignment 1 (Answers)
DS Assignment 1 (Answers)
Accessing Accessing
elements is elements can be
straightforward. more complex.
pointers.
Suitable for
Suitable for simple complex,
sequential data. hierarchical data.
Q:2 For FIFO (First In, First Out) operations, the suitable data structure is
a Queue.
Queue Operations:
1. Enqueue (Insertion)
The process of adding an element to the end of the queue is
called enqueue.
The new element is added at the rear of the queue.
2. Dequeue (Deletion)
The process of removing an element from the front of the queue
is called dequeue.
The element is removed from the front of the queue.
3. Peek (Front Element)
This operation returns the element at the front of the queue
without removing it.
4. IsEmpty
This operation checks if the queue is empty.
5. Size
This operation returns the number of elements in the queue.
Example:
Suppose we have a queue with the following elements: [1, 2, 3, 4, 5]
Enqueue (6): The queue becomes [1, 2, 3, 4, 5, 6]
Dequeue: The queue becomes [2, 3, 4, 5, 6] and the removed
element is 1
Peek: The front element is 2
IsEmpty: Returns false because the queue is not empty
Size: Returns 5 because there are 5 elements in the queue
Q:3.
A Singly Linked List is a type of linear data structure where each node
contains data and a pointer to the next node in the list. The last node in the
list points to null.
1. **Insertion**:
- **Insert at the Beginning (Head)**: Create a new node and make it the
head of the list, with the previous head node as its next.
- **Insert at the End (Tail)**: Traverse the list to the last node and append
the new node.
- **Delete the Head (First Node)**: Update the head pointer to the next
node in the list.
- **Delete the Tail (Last Node)**: Traverse the list to the second-to-last
node and update its next pointer to null.
- **Delete a Specific Node**: Traverse the list to the node before the one to
be deleted, and update its next pointer to skip the node to be deleted.
3. **Traversal**:
- **Traverse the List**: Start from the head and follow the next pointers
until the end of the list is reached.
- **Search for a Value**: Traverse the list and check if each node's data
matches the search value.
4. **Other Operations**:
- **Get the Length**: Traverse the list and count the number of nodes.
- **Reverse the List**: Modify the next pointers to reverse the direction of
the list.