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

DS Theory Assignment

The document outlines algorithms for implementing common data structures like linked lists, queues, stacks using nodes. This includes algorithms to create, insert, delete nodes and display the data for each of these data structures.

Uploaded by

manyme336
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)
14 views

DS Theory Assignment

The document outlines algorithms for implementing common data structures like linked lists, queues, stacks using nodes. This includes algorithms to create, insert, delete nodes and display the data for each of these data structures.

Uploaded by

manyme336
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/ 3

Assignment: Data Structures and Algorithms

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.

5. write an algorithm to insert a node at the beginning of a singly linked list.


Algorithm to Insert Node at the Beginning of a Singly Linked List
insert_node_at_beginning(head, data):
Step 1: Create a new node with the given data.
Step 2: Set the next pointer of the new node to head.
Step 3: Set the new node as the new head.
Step 4: Return the new head.
6. write an algorithm to implement the stack and display its data.
Algorithm to Push and Pop in a Stack
push(stack, data):
Step 1: Create a new node with the input data.
Step 2: Set the next pointer of the new node to the current top of the stack.
Step 3: Set the new node as the new top of the stack.
pop(stack):
Step 1: If the stack is empty
Step 2: Return an error or a special value.
Step 3: Store the data of the current top node.
Step 4: Update the top to the next node.
Step 5: Return the stored data.
Algorithm to Display Data in a Stack
display(stack):
Step 1: Set current as the top of the stack.
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.

You might also like