# Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.

## Presentation on theme: "Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes."— Presentation transcript:

Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes  Deleting a node  Traversal of the Circular Linked List  Program 2

Circular linked list  Circular linked list is a linked list where the last node of the linked list is pointing to the first node of the linked list. 3 2 1 3 4

Contd..  We need two pointers A head pointer – pointing to the first node A rear pointer – pointing to the last node 4 2 1 3 4 Front Rear

When is a circular LL is used?  When we need to traverse from the last node to first node with out having to traverse backwards.  Overcome the limitation of Doubly Linked List. 5 4 2 1 3 Front Rear From rear, traverse to the first link

Implementation  Can be implemented using Singly linked list Doubly linked list 6 2 1 3 4 2 1 4

Adding node in a empty linked list  Create a node and allocate memory  Set the front, rear to the new node  Set the next part of the new node to the same node As there is only one node, the next pointer should point to the same node 7

Addition of a node in a empty list  Set front = NULL  Set rear=NULL  Allocate memory to a new node 8 A node

Contd..  front =NULL  Rear=NULL  Set pointer q to point to new node  Set a value in data part,Example 12 9 q 12

Contd..  Now, point front and rear to the new node  Set link part of the new node to the address of the new node 10 q front rear 12 q front rear 12

Add a node in a non-empty list  Create a new node and allocate memory  Make the last node point to the new node  Make the new node point to the first node  Make the rear pointer point to the new node 11

Contd.. 12

Contd.. 13

Contd.. 14

Contd.. 15

Contd.. 16

Contd.. 17

Contd.. 18

Contd.. 19

Contd.. 20

Contd.. 21

Code class Link { public int iData; // data item (key) public Link next; // next link in list public Link(int dd) // constructor { iData = dd; // initialize data } public void displayLink() // display ourself { System.out.print("{" + iData + "} "); } } // end class Link 22

Contd.. private Link front; private Link rear; public LinkList() { front = null; rear = null; } public boolean isEmpty() // true if no links { return front==null; } 23

Deletion of a node – first node  Make the front pointer point to the second node  Free the memory allocated to the first node  Point the last node’s next to the second node 25

Contd.. 26

Contd.. 27

Contd.. 28

Contd.. 29

Contd.. 30

Contd.. 31

Deletion of the first node public void deleteFirst() { if(front.next == null) //if empty { front = null; rear = null; } else// if not empty { // move the front pointer to point to the next node front = front.next; rear.next = front; // set the rear to point to the new node } 32

Traversal of the Circular Linked List  If the condition for ending the traversal is checking with null,Circular linked list goes to an infinite loop.  There are Conditions to be checked using front and rear pointers. 33

Conditions  If front and rear is null => Empty List  If front=rear => Only one node in List 34

public void displayList() { System.out.print("List (first-->last): "); Link current = front; // start at beginning if( front == null) { System.out.println("List is empty"); } else if(front == rear) { front.displayLink(); } else { while(current != rear) // until rear is reached { current.displayLink(); // print data current = current.next; // move to next link } current.displayLink(); System.out.println(""); } 35

Applications of Circular LL  Round-Robin CPU Scheduling- operating system.  The OS must maintain a list of present tasks and must alternately allow each task to use a small slice of CPU time, one task at a time. 36

PROGRAM 37

References  Data Structure by Yashwanth Kanethkar.  Data Structures and Algorithms in Java, Robert Lafore 38

THANK YOU 39