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

Algo

Uploaded by

Samra Nawabi
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)
19 views

Algo

Uploaded by

Samra Nawabi
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

Circular Linked List

Question 7:

Write a C ++ program to insert a node at the beginning in circular


linked list.
Algorithm:
Insertion at Start in Circular Linked List
1. Create a new node.
2. Set the data of the new node to the desired value.
3. If the circular linked list is empty, make the new node point to itself.
4. Otherwise, traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next
points to the head).
5. Set the next pointer of the new node to point to the head of the circular linked list.
6. Update the next pointer of the last node to point to the new node.
7. Update the head of the circular linked list to be the new node.
8. Exit.

Question 8:

Write a C ++ program to insert a node at the end in circular linked list.


Algorithm:
Insertion at End in Circular Linked List
1. Create a new node.
2. Set the data of the new node to the desired value.
3. If the circular linked list is empty, make the new node point to itself and set it as the
head.
4. Traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next
points to the head).
5. Set the next pointer of the new node to point to the head of the circular linked list.
6. Update the next pointer of the last node to point to the new node.
7. Exit.
Question 9:

Write a C ++ program to insert a node at specific location in circular


linked list.
Algorithm:
Insertion at Specific Location in Circular Linked List
1. Create a new node.
2. Set the data of the new node to the desired value.
3. If the circular linked list is empty, make the new node point to itself and set it as the head.
4. Traverse to the node at the (position - 1) in the circular linked list.
- If the position is 1, go to step 7.
- If the position is greater than the current size of the list, display an error message and exit.
5. Update pointers to insert the new node at the specified position.
- Set the next pointer of the new node to the next node.
- Set the next pointer of the current node to the new node.
6. Exit.
7. Update pointers to insert the new node at the beginning.
- Set the next pointer of the new node to point to the head.
- Update the next pointer of the last node to point to the new node.
- Update the head of the circular linked list to be the new node.
8. Exit.
Question 10:

Write a C ++ program to delete a node from beginning in circular


linked list.
Algorithm:
Deletion from Start in Circular Linked List
1. If the circular linked list is empty, display an error message and exit.
2. If the circular linked list has only one node, free the memory of the node, set head to
null, and exit.
3. Traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next
points to the head).
4. Update pointers to delete the first node.
- Set the next node as the new head of the circular linked list.
- Set the next pointer of the last node to point to the new head.
- Free the memory of the original head.
5. Exit.
Question 11:

Write a C ++ program to delete a node from end in circular linked list.


Algorithm:
Deletion from End in Circular Linked List
1. If the circular linked list is empty, display an error message and exit.
2. If the circular linked list has only one node, free the memory of the node, set head to null, and
exit.
3. Traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next points to
the head).
4. Traverse to the second-to-last node in the circular linked list.
5. Update pointers to delete the last node.
- Set the next pointer of the second-to-last node to the head.
- Free the memory of the original last node.
6. Exit.
Question 12:

Write a C ++ program to delete a node from any specific location in


circular linked list.
Algorithm:
Deletion from Specific Location in Circular Linked List
1. If the circular linked list is empty, display an error message and exit.
2. If the position is 1, delete the node from the beginning using the algorithm for deletion from
the start.
3. Traverse to the node at the (position - 1) in the circular linked list.
- If the position is greater than the current size of the list, display an error message and exit.
4. Update pointers to delete the node at the specified position.
- Set the next pointer of the current node to the next node.
- Free the memory of the original node.
5. Exit.

You might also like