0% found this document useful (0 votes)
35 views4 pages

DS Assignment 1 (Answers)

Valuable answers for data structures

Uploaded by

mujeeransari96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

DS Assignment 1 (Answers)

Valuable answers for data structures

Uploaded by

mujeeransari96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q:1 A data structure is a way of organizing and storing data in a computer so

that it can be accessed and used efficiently. It is a particular arrangement of


data elements designed to facilitate their storage, retrieval, and
manipulation.

Linear Data Non-Linear Data


Structures Structures

Elements are Elements are not


stored in a stored
sequential manner. sequentially.

Accessing Accessing
elements is elements can be
straightforward. more complex.

Examples: Arrays, Examples: Trees,


Linked Lists, Graphs, Hash
Stacks, Queues Tables

Typically traversed Can be traversed


from start to end. in various ways.

Generally use less Can use more


memory. memory due to
additional links or
Linear Data Non-Linear Data
Structures Structures

pointers.

Simpler to More complex to


implement and implement and
understand. understand.

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.

The main operations for a Singly Linked List are:

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.

- **Insert at a Specific Position**: Traverse the list to the desired position


and insert the new node.
2. **Deletion**:

- **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.

You might also like