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

Circular Linked List

The document provides an overview of circular linked lists, including their structure and basic operations such as traversal, searching, insertion, and deletion. It details algorithms for managing both singly and doubly circular linked lists, including how to insert and delete nodes at various positions. The document emphasizes the unique characteristics of circular linked lists, such as having no definitive start or end point.

Uploaded by

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

Circular Linked List

The document provides an overview of circular linked lists, including their structure and basic operations such as traversal, searching, insertion, and deletion. It details algorithms for managing both singly and doubly circular linked lists, including how to insert and delete nodes at various positions. The document emphasizes the unique characteristics of circular linked lists, such as having no definitive start or end point.

Uploaded by

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

Data Structures

CIRCULAR LINKED LIST


List Overview
■ CIRCULAR LINKED LIST:
■ Introduction

■ BASIC OPERATIONS ON A CIRCULAR LIST:

■ Traverse
■ Search

■ Insert

■ Delete
INTRODUCTION
▪ A header linked list is a special type of linked list which contains
a header node at the beginning of the list. So, in a header linked
list START will not point to the first node of the list but START
will contain the address of the header node. There are basically
two variants of a header linked list-

▪ Grounded header linked list which stores NULL in the next field
of the last node
▪ Circular header linked list which stores the address of the
header node in the next field of the last node. Here, the
header node will denote the end of the list.

Header Node
1 2 3 4 5 6 X

START

Header Node
1 2 3 4 5 6

START
INTRODUCTION
• In a circular linked list, the last node contains a pointer to the first
node of the list. We can have a circular singly listed list as well as
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
had started. Thus, a circular linked list has no beginning and no
ending.
START

1 2 3 4 5 6 7
– Circular linked list are of two types:

– Single Circular Linked Lists – Doubly Circular


Linked Lists

– Single Circular linked list (Only one pointer is used)


• In diagram pointer from the last element in the list points
back to the first element.
head

A B C

Circular Linked List: Basic Operations

TRAVERSE : A GIVEN LIST


Algorithm to traverse a Circular Header Linked
List

Step 1: SET PTR = START->NEXT


Step 2: Repeat Steps 3 and 4 while PTR != START
Step 3: Apply PROCESS to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Circular Linked List: Basic Operations

SEARCH : ANY DATA IN A GIVEN LIST

Algorithm to search any data in a Circular Linked


List

Step 1: SET PTR = START->NEXT


Step 2: SET KEY = DATA
Step 3: Repeat Steps 3 and 4 while PTR != START
Step 4: CHECK IF KEY = PTR->DATA
Step 5: DISPLAY ‘DATA FOUND’ AND GOTO STEP 8
Step 6: ELSE GOTO NEXT STEP
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: EXIT
Circular Linked List: Basic Operations
INSERT : AT FRONT END OF LIST

1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5

START

9 1 7 3 4 2 6 5

START
Algorithm to insert a new node in the beginning of
circular the linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 7 while PTR->NEXT != START
Step 7: PTR = PTR->NEXT
Step 8: SET New_Node->Next = START
Step 8: SET PTR->NEXT = New_Node
Step 6: SET START = New_Node
Step 7: EXIT

Circular Linked List: Basic Operations

INSERT : AT REAR END OF LIST


1 7 3 4 2 6 5

START, PTR

1 7 3 4 2 6 5 9

PTR
START

Algorithm to insert a new node at the end of the circular


linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != START
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR ->NEXT = New_Node
Step 10: EXIT

Circular Linked List: Basic Operations

INSERT : AT AFTER A GIVEN NODE OF LIST


Algorithm to insert a new node after a node that
has value NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT

Circular Linked List: Basic Operations

DELETE : FIRST NODE OF THE LIST


1 7 3 4 2 6 5
START, PTR

1 7 3 4 2 6 5

PTR
START

7 3 4 2 6 5

Algorithm to delete the first node from the circular linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[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 IF]
Step 5: SET PTR->NEXT = START->NEXT
Step 6: FREE START
Step 7: SET START = PTR->NEXT
Step 8: EXIT

Circular Linked List: Basic Operations

DELETE : LAST NODE OF THE LIST


1 7 3 4 2 6 5

START, PREPTR, PTR

1 7 3 4 2 6 5
PREPTR
PTR
START

1 7 3 4 2 6

START

Algorithm to delete the last node of the circular


linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 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
Circular Linked List: Basic Operations

DELETE : AFTER A GIVEN NODE OF THE LIST


1 7 3 4 2 6 5

START, PREPTR, PTR

1 7 3 4 2 6 5

START PREPTR PTR

1 7 3 4 6 5

START

Algorithm to delete the node after a given node from the circular linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 7: SET PREPTR->NEXT = PTR->NEXT
Step 8: FREE PTR
Step 9: EXIT

Doubly Circular Linked List: Basic Operations

INSERT : AT FRONT OF THE LIST


1 7 3 4 2

START

9 1 7 3 4 2

START

Algorithm to insert a new node in the beginning of the circular doubly


linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 6: SET START->PREV->NEXT = new_node;
Step 7: SET New_Node->PREV = START->PREV;
Step 8: SET START->PREV= new_Node;
Step 9: SET new_node->next = START;
Step 10: SET START = New_Node
Step 11: EXIT

Doubly Circular Linked List: Basic Operations

INSERT : AT LAST OF THE LIST


Algorithm to insert a new node at the end of the
circular doubly linked list

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET New_Node->PREV = START->PREV
Step 7: EXIT
1 7 3 4 2
START

1 7 3 4 2 9

START

Doubly Circular Linked List: Basic Operations

INSERT : AFTER A GIVEN NODE OF THE LIST


Algorithm to insert a new node after a node that
has value NUM

Step 1: IF AVAIL = NULL, then


Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: Repeat Step 8 while PTR->DATA != NUM
Step 7: SET PTR = PTR->NEXT
[END OF LOOP]
Step 8: New_Node->NEXT = PTR->NEXT
Step 9: SET PTR->NEXT->PREV = New_Node
Step 9: SET New_Node->PREV = PTR
Step 10: SET PTR->NEXT = New_Node
Step 11: EXIT

Doubly Circular Linked List: Basic Operations


DELETE : FIRST NODE OF THE LIST
Algorithm to delete the first node from the circular
doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PTR->PREV=>NEXT= PTR->NEXT
Step 4: SET PTR->NEXT->PREV = PTR->PREV
Step 5: SET START = START->NEXT
Step 6: FREE PTR
Step 7: EXIT
Doubly Circular Linked List: Basic Operations

DELETE : LAST NODE OF THE LIST


Algorithm to delete the last node of the circular
doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START->PREV
Step 5: SET PTR->PREV->NEXT = START
Step 6: SET START->PREV = PTR->PREV
Step 7: FREE PTR
Step 8: EXI
1 3 5 7 8 9
1
START PTR
T

X 1 3 5 7 8

START

Doubly Circular Linked List: Basic Operations


DELETE : AFTER A GIVEN NODE OF THE LIST
Algorithm to delete the node after a given node from
the circular doubly linked list

Step 1: IF START = NULL, then


Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR->DATA != NUM
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET TEMP = PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT

You might also like