Basic Operations on a Circular Linked List
We can perform the following basic operation on a Circular Linked List:
- Define a Circular Linked List
- Insert an Element at the beginning
- Insert an Element at the end
- Insert an Element after a given position
- Delete an Element at the beginning
- Delete an Element at the end
- Delete an Element after a given position
- Traverse and Display the Elements/Nodes
Define a Circular Linked List
- CODE
Insert an Element at the beginning
- ALGORITHM
Step 1: if AVAIL = NULL
Write Overflow
Go to step 11
[END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL->next
Step 4: SET newNode->data = val
Step 5: SET ptr = start
Step 6: Repeat step 7 while ptr->next != start
Step 7: ptr = ptr->next
[END OF LOOP]
Step 8: SET newNode->next = start
Step 9: SET ptr->next = newNode
Step 10: SET start = newNode
Step 11: EXIT
- CODE
Insert an Element at the end
- ALGORITHM
Step 1: if AVAIL = NULL
Write Overflow
Go to step 10
[END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL->next
Step 4: SET newNode->data = val
Step 5: SET ptr = start
Step 6: Repeat step 7 while ptr->next != start
Step 7: ptr = ptr->next
[END OF LOOP]
Step 8: SET ptr->next = newNode
Step 9: SET newNode->next = start
Step 10: EXIT
- CODE
Insert an Element after a given position
- CODE
Delete an Element at the beginning
- ALGORITHM
Step 1: if start = NULL
Write Underflow
Go to step 5
[END OF if]
Step 2: SET ptr = start
Step 3: Repeat step 4 while ptr->next != start
Step 4: SET ptr = ptr->next
[END OF LOOP]
Step 5: SET ptr->next = start->next
Step 6: free start
Step 7: SET start = ptr->next
Step 8: EXIT
- CODE
Delete an Element at the end
- ALGORITHM
Step 1: if start = NULL
Write Underflow
Go to step 5
[END OF if]
Step 2: SET ptr = start
Step 3: Repeat step 4 and 5 while ptr->next != Start
Step 4: SET prePtr = ptr
Step 5: SET ptr = ptr->next
[END OF LOOP]
Step 6: SET prePtr->next = start
Step 7: free ptr
Step 8: EXIT
- CODE
Delete an Element after a given position
- ALGORITHM
Step 1: SET ptr = start
Step 2: SET prePtr = ptr
Step 3: Repeat step 4 and 5 while prePtr->data != val
Step 4: SET prePtr = ptr
Step 5: SET ptr = ptr->next
[END OF LOOP]
Step 6: SET prePtr->next = ptr->next
Step 7: if ptr = start
Step 8: start = prePtr->next
[END OF if]
Step 9: free ptr
Step 10: EXIT
- CODE
Traverse and Display the Elements/Nodes
- CODE