DS Theory Assignment
DS Theory Assignment
1. write an algorithm to make a linked list and search a user given data.
Algorithm to Create a Linked List
create_linked_list():
Step 1: Initialize head and tail as None.
Step 2: Start a loop
Step 3: Read user input for data.
Step 4: If input is 'q', break the loop.
Step 5: Create a new node with the input data.
Step 6: If head is None
Step 7: Set the new node as head and tail.
Step 8: Else
Step 9: Set the next pointer of tail to the new node.
Step 10: Update tail to the new node.
Step 11: Repeat steps 3 to 10.
Step 12: Return head.
Algorithm to Search for Data in a Linked List
search_data(head, target):
Step 1: Set current as head.
Step 2: Start a loop until current is None.
Step 3: If current's data is equal to target
Step 4: Return true (data exists in the linked list).
Step 5: Set current to the next node.
Step 6: Return false (data doesn't exist in the linked list).
2. write an algorithm to make a doubly linked list and display its data.
Algorithm to Create a Doubly Linked List
create_doubly_linked_list():
Step 1: Initialize head and tail as None.
Step 2: Start a loop
Step 3: Read user input for data.
Step 4: If input is 'q', break the loop.
Step 5: Create a new node with the input data.
Step 6: If head is None
Step 7: Set the new node as head and tail.
Step 8: Else
Step 9: Set the next pointer of tail to the new node.
Step 10: Set the previous pointer of the new node to tail.
Step 11: Update tail to the new node.
Step 12: Repeat steps 3 to 11.
Step 13: Return head.
Algorithm to Display Data in a Doubly Linked List
display_doubly_linked_list(head):
Step 1: Set current as head.
Step 2: Start a loop until current is None.
Step 3: Print current's data.
Step 4: Set current to the next node.
Step 5: End loop.
3. write an algorithm to implement the queue and display its data.
Algorithm to Enqueue and Dequeue in a Queue
enqueue(queue, data):
Step 1: Create a new node with the input data.
Step 2: If the queue is empty
Step 3: Set the new node as both head and tail.
Step 4: Else
Step 5: Set the next pointer of the current tail node to the new node.
Step 6: Update tail to the new node.
dequeue(queue):
Step 1: If the queue is empty
Step 2: Return an error or a special value.
Step 3: Store the data of the current head node.
Step 4: Update the head to the next node.
Step 5: If head is None
Step 6: Update tail to None.
Step 7: Return the stored data.
Algorithm to Display Data in a Queue
display(queue):
Step 1: Set current as the head of the queue.
Step 2: Start a loop until current is None.
Step 3: Print current's data.
Step 4: Set current to the next node.
Step 5: End loop.
4. write an algorithm to delete a node from the beginning of a singly linked list.
Algorithm to Delete Node from the Beginning of a Singly Linked List
delete_node_from_beginning(head):
Step 1: If head is None
Step 2: Return None.
Step 3: Set new_head as the next node of head.
Step 4: Set head's next pointer to None.
Step 5: Return new_head.