Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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:

1 Circular Linked List

2 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

3 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

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

5 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 Front Rear From rear, traverse to the first link

6 Implementation  Can be implemented using Singly linked list Doubly linked list

7 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

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

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

10 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

11 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

12 Contd.. 12

13 Contd.. 13

14 Contd.. 14

15 Contd.. 15

16 Contd.. 16

17 Contd.. 17

18 Contd.. 18

19 Contd.. 19

20 Contd.. 20

21 Contd.. 21

22 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

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

24 Insertion public void insert(int dd) { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, { front = newLink; rear = newLink; // newLink <-- last newLink.next = front; // newLink --> old first } else { rear.next = newLink; // rear  newnode rear = newLink; // set rear to newnode newLink.next = front;// Using rear, point to the first node } 24

25 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

26 Contd.. 26

27 Contd.. 27

28 Contd.. 28

29 Contd.. 29

30 Contd.. 30

31 Contd.. 31

32 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

33 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

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

35 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

36 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

37 PROGRAM 37

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

39 THANK YOU 39


Download ppt "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."

Similar presentations


Ads by Google