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

Circular linked list

A circular linked list is a data structure where the last node points back to the first node, allowing traversal from any node in any direction. It has applications in operating systems and web navigation, but can be complex to iterate. The document also details methods for inserting and deleting nodes at both the beginning and end of the list.

Uploaded by

nandanhs14
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Circular linked list

A circular linked list is a data structure where the last node points back to the first node, allowing traversal from any node in any direction. It has applications in operating systems and web navigation, but can be complex to iterate. The document also details methods for inserting and deleting nodes at both the beginning and end of the list.

Uploaded by

nandanhs14
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CIRCULAR LINKED LISTs

- In a circular linked list, the last node contains a pointer to the first node of the list.
- can have a circular singly linked list as well as a circular doubly linked list
- While traversing a circular linked list, we can begin at any node and traverse the list in
any direction, forward or backward, until we reach the same node where we started.
- Thus, a circular linked list has no beginning and no ending.

Drawbacks:
- circular linked list is the complexity of iteration.
- Note that there are no NULL values in the NEXT part of any of the nodes of list.
Applications:
- Circular linked lists are widely used in operating systems for task maintenance.
- Surfing the Internet: we can use the Back button and the Forward button to move to the
previous pages that we have already visited.
o A circular linked list is used to maintain the sequence of the Web pages visited.
Traversing this circular linked list either in forward or backward direction helps to
revisit the pages again using Back and Forward buttons.

Inserting a New Node in a Circular Linked List


- Case 1: The new node is inserted at the beginning of the circular linked list.
- Case 2: The new node is inserted at the end of the circular linked list.

Inserting a Node at the Beginning of a Circular Linked List

Step 1: IF AVAIL = NULL struct node *insert_beg(struct node *start)


Write OVERFLOW {
Go to Step 11 struct node *new_node, *ptr;
[END OF IF] int num;
Step 2: SET NEW_NODE= AVAIL
printf("\n Enter the data : ");
Step 3: SET AVAIL = AVAIL ->NEXT
Step 4: SET NEW_NODE-> DATA = VAL
scanf("%d", &num);
SET NEW_NODE-> NEXT = NULL new_node = (struct node *)
malloc(sizeof(struct node));
Step 5: SET PTR = START new_node -> data = num;
Step 6: Repeat Step 7 while PTR ->NEXT != START new_node -> next = NULL;
Step 7: PTR = PTR ->NEXT ptr = start;
[END OF LOOP] while(ptr -> next != start)
Step 8: SET NEW_NODE->NEXT = START ptr = ptr -> next;
Step 9: SET PTR NEXT = NEW_NODE ptr -> next = new_node;
new_node -> next = start;
Step 10 : SET START = NEW_NODE
start = new_node;
Step 11: EXIT
return start;
}
- First check whether memory is available for the new node. If the free memory has
exhausted, then an overflow message is printed.
- Otherwise, if free memory cell is available, then we allocate space for the new node.
- Set its data part with the given value
- Since the new node is added as the first node of the list, the start pointer variable will
now hold the address of the new_node.
- Use a while loop to traverse to the last node of the list using ptr.
- Because the last node ptr contains a pointer to start, its next field is pointed to start.

Inserting a Node at the End of a Circular Linked List


- Take a pointer variable PTR and initialize it with START. That is, PTR now points to the
first node of the linked list.
- Traverse through the linked list to reach the last node by PTR.
- Change the PTR’s NEXT to store the address of the new node.
- NEXT field of the new node should point to the address of the first node which is
denoted by START.

Step 1: IF AVAIL = NULL struct node *insert_end(struct node *start)


Write OVERFLOW {
Go to Step 1 struct node *ptr, *new_node;
[END OF IF] int num;
Step 2: SET = AVAIL
printf("\n Enter the data : ");
Step 3: SET AVAIL = AVAIL-> NEXT
Step 4: SET NEW_NODE->DATA = VAL
scanf("%d", &num);
SET NEW_NODE-> NEXT = NULL new_node = (struct node *)
malloc(sizeof(struct node));
Step 5: SET PTR = START new_node -> data = num;
Step 6: Repeat Step 8 while PTR ->NEXT != START newnode->next=NULL;
Step 7: SET PTR = PTR ->NEXT ptr = start;
[END OF LOOP] while(ptr -> next != start)
Step 8: SET PTR-> NEXT = NEW_NODE->NEXT ptr = ptr -> next;
SET NEW_NODE->NEXT = START ptr -> next = new_node;
Step 9 : EXIT new_node -> next = start;
return start;
}

Deleting a Node from a Circular Linked List


- Case 1: The first node is deleted.
- Case 2: The last node is deleted.

Deleting the First Node from a Circular Linked List


- Check if the linked list exists or not.
- If start = null, then it signifies that there are no nodes in the list and the control is
transferred to the last statement of the algorithm.
- Else, use a pointer variable ptr which will be used to traverse the list to ultimately reach
the last node.
- Change the next pointer of the last node to point to the second node of the circular linked
list.
- the memory occupied by the first node is freed.
- The second node now becomes the first node of the list and its address is stored in the
pointer variable start

Step 1: IF START = NULL struct node *delete_beg(struct node *start)


Write UNDERFLOW {
Go to Step 8 struct node *ptr;
[END OF IF] ptr = start;
Step 2: SET PTR = START
while(ptr -> next != start)
Step 3: Repeat Step 4 while PTR ->NEXT != START
Step 4: SET PTR = PTR ->NEXT
ptr = ptr -> next;
[END OF LOOP] ptr -> next = start -> next;
Step 5: SET PTR-> NEXT = START ->NEXT free(start);
Step 6: FREE START start = ptr -> next;
Step 7: SET START = PTR-> NEXT return start;
Step 8: EXIT }

Deleting the Last Node from a Circular Linked List


- Take a pointer variable PTR and initialize it with START
- In the while loop, take another pointer variable PREPTR , such that PREPTR always
points to one node before PTR.
- Once the last node and the second last node is reached, set the next pointer of the
PREPTR to START,
- The memory of the previous last node is freed and returned to the free pool

Step 1: IF START = NULL struct node *delete_end(struct node


Write UNDERFLOW *start)
Go to Step 8 {
[END OF IF] struct node *ptr, *preptr;
Step 2: SET PTR = START
ptr = start;
Step 3: Repeat Steps 4 and 5 while PTR-> NEXT != START
Step 4: SET PREPTR = PTR
while(ptr -> next != start)
Step 5: SET PTR = PTR ->NEXT {
[END OF LOOP] preptr = ptr;
Step 6: SET PREPTR ->NEXT = START ptr = ptr -> next;
Step 7: FREE PTR }
Step 8: EXIT preptr -> next = ptr -> next;
free(ptr);
return start;
}

You might also like