KEMBAR78
U-3.1 Linked List singly_doubly_circular.ppt
Module-3
 Singly Linked List
 Doubly Linked List
 Circular Linked List
 Representing Stack with Linked List.
 Representing Queue with Linked List.
In array, elements are stored in consecutive memory locations.
To occupy the adjacent space, block of memory that is required for the
array
should be allocated before hand.
Once memory is allocated, it cannot be extended any more. So that array is
called the static data structure.
Wastage of memory is more in arrays.
Array has fixed size
But, Linked list is a dynamic data structure, it is able to grow in size as
needed.
What is Linked List?
 A linked list is a linear collection of homogeneous data elements,
called nodes, where linear order is maintained by means of links or
pointers.
 Each node has two parts:
 The first part contains the data (information of the element) and
 The second part contains the address of the next node (link /next
pointer field) in the list.
 Data part of the link can be an integer,
a character, a String or an object of any kind.
link
data
node
A
Null
B C D
Start
Linked Lists
 Linked list
– Linear collection of self-referential structures, called nodes, connected
by pointer links.
– Accessed via a pointer to the first node of the list.
– Subsequent nodes are accessed via the link-pointer member stored in
each node.
– Link pointer in the last node is set to null to mark the end of list.
– Data stored dynamically – each node is created as necessary.
– Length of a list can increase or decrease.
– Becomes full only when the system has insufficient memory to satisfy
dynamic storage allocation requests.
Types of linked lists
– Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
– Circular, singly linked list
• Pointer in the last node points back to the first node
– Doubly linked list
• Two “start pointers”- first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
– Circular, doubly linked list
• Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
Dynamic Memory Allocation
 Dynamic memory allocation
– Obtain and release memory during execution
• malloc
– Takes number of bytes to allocate
• Use sizeof to determine the size of an object
– Returns pointer of type void *
• A void * pointer may be assigned to any pointer
• If no memory available, returns NULL
– newPtr = malloc( sizeof( struct node ) );
• free
– Deallocates memory allocated by malloc
– Takes a pointer as an argument
– free (newPtr);
Self-Referential Structures
• Self-referential structures
– Structure that contains a pointer to a structure of the same type
– Can be linked together to form useful data structures such as lists,
queues, stacks and trees
– Terminated with a NULL pointer (0)
• Two self-referential structure objects linked together
10
15
NULL pointer (points
to nothing)
Data member
and pointer
Singly linked list operations
Insertion:
• Insertion of a node at the front
• Insertion of a node at any position in the list
• Insertion of a node at the end
Deletion:
• Deletion at front
• Deletion at any position
• Deletion at end
Display:
• Displaying/Traversing the elements of a list
Singly linked lists
Node Structure
struct node
{
int data;
struct node *link;
}*new, *ptr, *header, *ptr1;
Creating a node
new = malloc (sizeof(struct node));
new -> data = 10;
new -> link = NULL;
data link
2000
10
new
2000
NULL
Inserting a node at the beginning
Create a node that is to be inserted
2500
data link
10
new
2500
NULL
Algorithm:
1.Create a new node.
2.if (header = = NULL)
3. header = new;
4.else
5.{
6. new -> link = header;
7. header = new;
8.}
If the list is empty
NULL
header header
If the list is not empty
1200
header
2500
10
new
2500
NULL 1200 1300 1330 1400
NULL
5 5 4 8
1300 1330 1400
Inserting a node at the end
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200
1400
1500
header
50 NULL
2000
Algorithm:
1. new=malloc(sizeof(struct node));
2. ptr = header;
3. while(ptr -> link!= NULL)
4. ptr = ptr -> link;
5. ptr -> link = new;
2000
new
1500
ptr
1800
1200
1400
2000
Inserting a node at the given position
10 1800 20 1200 30 1400 40 NULL
1500
header
50 NULL
2000
2000
new Algorithm:
1. new=malloc(sizeof(struct node));
2. ptr = header;
3. for(i=1;i < pos-1;i++)
4. ptr = ptr -> link;
5. new -> link = ptr -> link;
6. ptr -> link = new;
1500
ptr
1500 1800 1200
1400
Insert position : 3
1800
1200
2000
Deleting a node at the beginning
Algorithm:
1. if (header = = NULL)
2. print “List is Empty”;
3. else
4. {
5. ptr = header;
6. header = header ->
link;
7. free(ptr);
8. }
10 1800 20 30 1400 40 NULL
1500 1800 1200
1400
1200
1500
header
1500
ptr
1800
Deleting a node at the end
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200
1400
1500
header
Algorithm:
1. ptr = header;
2. while(ptr -> link != NULL)
3. ptr1=ptr;
4. ptr = ptr -> link;
5. ptr1 -> link = NULL;
6. free(ptr);
1500
ptr
1800
1200
NULL
ptr1 ptr1 ptr1
1400
Deleting a node at the given position
10 1800 20 1200 30 1400 40 NULL
1500
header
Algorithm:
1. ptr = header ;
2. for(i=1;i<pos-1;i++)
3. ptr = ptr -> link;
4. ptr1 = ptr -> link;
5. ptr -> link = ptr1-> link;
6. free(ptr1);
1500
ptr
1500 1800 1200
1400
Insert position : 3
1800
ptr1
1200
1400
Traversing an elements of a list
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200 1400
1500
header
Algorithm:
1. if(header = = NULL)
2. print “List is empty”;
3. else
4. for (ptr = header ; ptr != NULL ; ptr = ptr ->
link)
5. print “ptr->data”;
ptr
1500
 Disadvantage of using an array to implement a stack or queue is the
wastage of space.
 Implementing stacks as linked lists provides a feasibility on the number of
nodes by dynamically growing stacks, as a linked list is a dynamic data
structure.
 The stack can grow or shrink as the program demands it to.
 A variable top always points to top element of the stack.
 top = NULL specifies stack is empty.
Representing Stack with Linked List
10 NULL
1500
1800
1200
1400
20 1400
30 1200
40 1800
50 1500 1100
top
Example:
The following list consists of five cells, each of which holds a data object
and a link to another cell.
A variable, top, holds the address of the first cell in the list.
 New items are added to the end of the list.
 Removing an item from the queue will be done from the front.
 A pictorial representation of a queue being implemented as a linked list
is given below.
 The variables front points to the front item in the queue and rear points
to the last item in the queue.
Representing Queue with Linked List
10 1800 20 1200 30 1400 40 NULL
1500 1800 1200 1400
front rear
Doubly linked list
 In a singly linked list one can move from the header node to any node in
one direction only (left-right).
 A doubly linked list is a two-way list because one can move in either
direction. That is, either from left to right or from right to left.
 It maintains two links or pointer. Hence it is called as doubly linked list.
 Where, DATA field - stores the element or data, PREV- contains the
address of its previous node, NEXT- contains the address of its next
node.
PREV DATA NEXT
Structure of the node
Doubly linked list operations
Insertion:
• Insertion of a node at the front
• Insertion of a node at any position in the list
• Insertion of a node at the end
Deletion:
• Deletion at front
• Deletion at any position
• Deletion at end
Display:
• Displaying/Traversing the elements of a list
Algorithm:
1. Create a new node
2. Read the item
3. new->data=item
4. ptr= header
5. new->next=ptr;
6. ptr->prev=new;
7. new->prev=NULL;
8. header=new;
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
2200
1010 2020 1000 2000
50 1010
NULL
2200
new
header
2200
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
header
1010
ptr
1010 Before inserting a node at the beginning
After inserting a node at the beginning
50 NULL
NULL
2200 new
1. Create a new node
2. Read the item
3. new->data=item
4. ptr= header
5. while(ptr->next!=NULL)
1. ptr=ptr->next;
6. new->next=NULL;
7. new->prev=ptr;
8. ptr->next=new;
50 NULL
NULL
2200 new
header
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010 ptr
1010 ptr
new
20 1000
1010 30 2000
2020 40 2200
1000
10 2020
NULL
1010 2020 1000 2000
50 NULL
2000
2200
new
ptr 2000
Before inserting a node at end of a list
After inserting a node
at end of a list
Algorithm:
Insertion of a node at any position in the list
1. create a node new
2. read item
3. new->data=item
4. ptr=header;
5. Read the position where the element is to be inserted
6. for(i=1;i<pos-1;i++)
6.1 ptr=ptr->next;
7. if(ptr->next = = NULL)
7.1 new->next = NULL;
7.2 new->prev=ptr;
7.3 ptr->next=new;
8. else
8.1 ptr1=ptr->next;
8.2 new->next=ptr1;
8.3 ptr1->prev=new;
8.4 new->prev=ptr;
8.5 ptr->next=new;
9. end
Algorithm:
header
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010 ptr
1010 ptr
50 NULL
NULL
2200 new
Before inserting a node at position 3
header
20 2200
1010 30 2000
2200 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010
ptr
2020 ptr
50 1000
2020
2200 new
ptr
1000 ptr1
After inserting a node at position 3
Algorithm:
1.ptr=header
2.ptr1=ptr->next;
3.header=ptr1;
4.if(ptr1!=NULL)
1.ptr1->prev=NULL;
5. free(ptr);
header
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010
ptr1
ptr
20 1000
NULL 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
2020
1010
header
Before deleting a node at beginning
After deleting a node at beginning
header
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010
Algorithm:
1. ptr=header
2. while(ptr->next!=NULL)
1. ptr=ptr->next;
3. end while
4. ptr1=ptr->prev;
5. ptr1->next=NULL;
Before deleting a node at end
ptr
pt1
header
20 1000
1010 30 NULL
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010
2000
1000
After deleting a node at end
Deletion at any position
Algorithm:
1. ptr=header;
2. while(ptr->next!=NULL)
1.for(i=0;i<pos-1;i++)
1. ptr=ptr->next;
2.if(i = = pos-1)
1. break;
3. end while
4. if(ptr = = header)
//if the deleted item is first node
4.1 ptr1=ptr->next;
4.2 ptr1->prev=NULL;
4.3 header=ptr1;
4.4 end if
5.else
5.1 ptr1=ptr->prev;
5.2 ptr2=ptr->next;
5.3 ptr1->next=ptr2;
5.4 ptr2->prev=ptr1;
6. end else
7. end if
header
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
1010 ptr
1010 ptr
Before deleting a node at position 3
After deleting a node at position 3
2000
header
20 2000
1010 30 2000
2200 40 NULL
2020
10 2020
NULL
1010 2020 1000 2000
1010
ptr
2020
ptr
1
ptr
1000 ptr2
Displaying elements of a list
Algorithm:
1. ptr=header;
2. if(header = = NULL)
1. printf("The list is emptyn");
3. else
1. print “The elements in farword order: “
2. while(ptr!=NULL)
1. print “ptr->data”;
2. if(ptr->next = = NULL)
1. break;
3. ptr=ptr->next;
3. print “The elements in reverse order: “
4. while(ptr!=header)
1. if(ptr->next = = NULL)
1. print “ptr->data”;
2. else
1. print “ptr->data”;
2. ptr=ptr->prev;
3. print “ptr->data”;
3.end else
4. end else
20 1000
1010 30 2000
2020 40 NULL
1000
10 2020
NULL
1010 2020 1000 2000
header
1010
ptr
1010
Forward Order : 10 20 30 40
Reverse Order : 40 30 20 10
Circular linked list
 The linked list where the last node points the header node is called
circular linked list.
Circular singly linked list
Circular doubly linked list
Circular Linked Lists
• A Circular Linked List is a special type of Linked List
• It supports traversing from the end of the list to the beginning by
making the last node point back to the head of the list
• A Rear pointer is often used instead of a Head pointer
Rear
10 20 40 70
55
Motivation
• Circular linked lists are useful for playing video
and sound files in “looping” mode.
• They are also a stepping stone to implementing
graphs.

U-3.1 Linked List singly_doubly_circular.ppt

  • 1.
  • 2.
     Singly LinkedList  Doubly Linked List  Circular Linked List  Representing Stack with Linked List.  Representing Queue with Linked List.
  • 3.
    In array, elementsare stored in consecutive memory locations. To occupy the adjacent space, block of memory that is required for the array should be allocated before hand. Once memory is allocated, it cannot be extended any more. So that array is called the static data structure. Wastage of memory is more in arrays. Array has fixed size But, Linked list is a dynamic data structure, it is able to grow in size as needed.
  • 4.
    What is LinkedList?  A linked list is a linear collection of homogeneous data elements, called nodes, where linear order is maintained by means of links or pointers.  Each node has two parts:  The first part contains the data (information of the element) and  The second part contains the address of the next node (link /next pointer field) in the list.  Data part of the link can be an integer, a character, a String or an object of any kind.
  • 5.
  • 6.
    Linked Lists  Linkedlist – Linear collection of self-referential structures, called nodes, connected by pointer links. – Accessed via a pointer to the first node of the list. – Subsequent nodes are accessed via the link-pointer member stored in each node. – Link pointer in the last node is set to null to mark the end of list. – Data stored dynamically – each node is created as necessary. – Length of a list can increase or decrease. – Becomes full only when the system has insufficient memory to satisfy dynamic storage allocation requests.
  • 7.
    Types of linkedlists – Singly linked list • Begins with a pointer to the first node • Terminates with a null pointer • Only traversed in one direction – Circular, singly linked list • Pointer in the last node points back to the first node – Doubly linked list • Two “start pointers”- first element and last element • Each node has a forward pointer and a backward pointer • Allows traversals both forwards and backwards – Circular, doubly linked list • Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node
  • 8.
    Dynamic Memory Allocation Dynamic memory allocation – Obtain and release memory during execution • malloc – Takes number of bytes to allocate • Use sizeof to determine the size of an object – Returns pointer of type void * • A void * pointer may be assigned to any pointer • If no memory available, returns NULL – newPtr = malloc( sizeof( struct node ) ); • free – Deallocates memory allocated by malloc – Takes a pointer as an argument – free (newPtr);
  • 9.
    Self-Referential Structures • Self-referentialstructures – Structure that contains a pointer to a structure of the same type – Can be linked together to form useful data structures such as lists, queues, stacks and trees – Terminated with a NULL pointer (0) • Two self-referential structure objects linked together 10 15 NULL pointer (points to nothing) Data member and pointer
  • 10.
    Singly linked listoperations Insertion: • Insertion of a node at the front • Insertion of a node at any position in the list • Insertion of a node at the end Deletion: • Deletion at front • Deletion at any position • Deletion at end Display: • Displaying/Traversing the elements of a list
  • 11.
    Singly linked lists NodeStructure struct node { int data; struct node *link; }*new, *ptr, *header, *ptr1; Creating a node new = malloc (sizeof(struct node)); new -> data = 10; new -> link = NULL; data link 2000 10 new 2000 NULL
  • 12.
    Inserting a nodeat the beginning Create a node that is to be inserted 2500 data link 10 new 2500 NULL Algorithm: 1.Create a new node. 2.if (header = = NULL) 3. header = new; 4.else 5.{ 6. new -> link = header; 7. header = new; 8.} If the list is empty NULL header header If the list is not empty 1200 header 2500 10 new 2500 NULL 1200 1300 1330 1400 NULL 5 5 4 8 1300 1330 1400
  • 13.
    Inserting a nodeat the end 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 1500 header 50 NULL 2000 Algorithm: 1. new=malloc(sizeof(struct node)); 2. ptr = header; 3. while(ptr -> link!= NULL) 4. ptr = ptr -> link; 5. ptr -> link = new; 2000 new 1500 ptr 1800 1200 1400 2000
  • 14.
    Inserting a nodeat the given position 10 1800 20 1200 30 1400 40 NULL 1500 header 50 NULL 2000 2000 new Algorithm: 1. new=malloc(sizeof(struct node)); 2. ptr = header; 3. for(i=1;i < pos-1;i++) 4. ptr = ptr -> link; 5. new -> link = ptr -> link; 6. ptr -> link = new; 1500 ptr 1500 1800 1200 1400 Insert position : 3 1800 1200 2000
  • 15.
    Deleting a nodeat the beginning Algorithm: 1. if (header = = NULL) 2. print “List is Empty”; 3. else 4. { 5. ptr = header; 6. header = header -> link; 7. free(ptr); 8. } 10 1800 20 30 1400 40 NULL 1500 1800 1200 1400 1200 1500 header 1500 ptr 1800
  • 16.
    Deleting a nodeat the end 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 1500 header Algorithm: 1. ptr = header; 2. while(ptr -> link != NULL) 3. ptr1=ptr; 4. ptr = ptr -> link; 5. ptr1 -> link = NULL; 6. free(ptr); 1500 ptr 1800 1200 NULL ptr1 ptr1 ptr1 1400
  • 17.
    Deleting a nodeat the given position 10 1800 20 1200 30 1400 40 NULL 1500 header Algorithm: 1. ptr = header ; 2. for(i=1;i<pos-1;i++) 3. ptr = ptr -> link; 4. ptr1 = ptr -> link; 5. ptr -> link = ptr1-> link; 6. free(ptr1); 1500 ptr 1500 1800 1200 1400 Insert position : 3 1800 ptr1 1200 1400
  • 18.
    Traversing an elementsof a list 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 1500 header Algorithm: 1. if(header = = NULL) 2. print “List is empty”; 3. else 4. for (ptr = header ; ptr != NULL ; ptr = ptr -> link) 5. print “ptr->data”; ptr 1500
  • 19.
     Disadvantage ofusing an array to implement a stack or queue is the wastage of space.  Implementing stacks as linked lists provides a feasibility on the number of nodes by dynamically growing stacks, as a linked list is a dynamic data structure.  The stack can grow or shrink as the program demands it to.  A variable top always points to top element of the stack.  top = NULL specifies stack is empty. Representing Stack with Linked List
  • 20.
    10 NULL 1500 1800 1200 1400 20 1400 301200 40 1800 50 1500 1100 top Example: The following list consists of five cells, each of which holds a data object and a link to another cell. A variable, top, holds the address of the first cell in the list.
  • 21.
     New itemsare added to the end of the list.  Removing an item from the queue will be done from the front.  A pictorial representation of a queue being implemented as a linked list is given below.  The variables front points to the front item in the queue and rear points to the last item in the queue. Representing Queue with Linked List 10 1800 20 1200 30 1400 40 NULL 1500 1800 1200 1400 front rear
  • 22.
    Doubly linked list In a singly linked list one can move from the header node to any node in one direction only (left-right).  A doubly linked list is a two-way list because one can move in either direction. That is, either from left to right or from right to left.  It maintains two links or pointer. Hence it is called as doubly linked list.  Where, DATA field - stores the element or data, PREV- contains the address of its previous node, NEXT- contains the address of its next node. PREV DATA NEXT Structure of the node
  • 23.
    Doubly linked listoperations Insertion: • Insertion of a node at the front • Insertion of a node at any position in the list • Insertion of a node at the end Deletion: • Deletion at front • Deletion at any position • Deletion at end Display: • Displaying/Traversing the elements of a list
  • 24.
    Algorithm: 1. Create anew node 2. Read the item 3. new->data=item 4. ptr= header 5. new->next=ptr; 6. ptr->prev=new; 7. new->prev=NULL; 8. header=new; 20 1000 1010 30 2000 2020 40 NULL 1000 10 2020 2200 1010 2020 1000 2000 50 1010 NULL 2200 new header 2200 20 1000 1010 30 2000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 header 1010 ptr 1010 Before inserting a node at the beginning After inserting a node at the beginning 50 NULL NULL 2200 new
  • 25.
    1. Create anew node 2. Read the item 3. new->data=item 4. ptr= header 5. while(ptr->next!=NULL) 1. ptr=ptr->next; 6. new->next=NULL; 7. new->prev=ptr; 8. ptr->next=new; 50 NULL NULL 2200 new header 20 1000 1010 30 2000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 ptr 1010 ptr new 20 1000 1010 30 2000 2020 40 2200 1000 10 2020 NULL 1010 2020 1000 2000 50 NULL 2000 2200 new ptr 2000 Before inserting a node at end of a list After inserting a node at end of a list Algorithm:
  • 26.
    Insertion of anode at any position in the list 1. create a node new 2. read item 3. new->data=item 4. ptr=header; 5. Read the position where the element is to be inserted 6. for(i=1;i<pos-1;i++) 6.1 ptr=ptr->next; 7. if(ptr->next = = NULL) 7.1 new->next = NULL; 7.2 new->prev=ptr; 7.3 ptr->next=new; 8. else 8.1 ptr1=ptr->next; 8.2 new->next=ptr1; 8.3 ptr1->prev=new; 8.4 new->prev=ptr; 8.5 ptr->next=new; 9. end Algorithm:
  • 27.
    header 20 1000 1010 302000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 ptr 1010 ptr 50 NULL NULL 2200 new Before inserting a node at position 3 header 20 2200 1010 30 2000 2200 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 ptr 2020 ptr 50 1000 2020 2200 new ptr 1000 ptr1 After inserting a node at position 3
  • 28.
    Algorithm: 1.ptr=header 2.ptr1=ptr->next; 3.header=ptr1; 4.if(ptr1!=NULL) 1.ptr1->prev=NULL; 5. free(ptr); header 20 1000 101030 2000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 ptr1 ptr 20 1000 NULL 30 2000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 2020 1010 header Before deleting a node at beginning After deleting a node at beginning
  • 29.
    header 20 1000 1010 302000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 Algorithm: 1. ptr=header 2. while(ptr->next!=NULL) 1. ptr=ptr->next; 3. end while 4. ptr1=ptr->prev; 5. ptr1->next=NULL; Before deleting a node at end ptr pt1 header 20 1000 1010 30 NULL 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 2000 1000 After deleting a node at end
  • 30.
    Deletion at anyposition Algorithm: 1. ptr=header; 2. while(ptr->next!=NULL) 1.for(i=0;i<pos-1;i++) 1. ptr=ptr->next; 2.if(i = = pos-1) 1. break; 3. end while 4. if(ptr = = header) //if the deleted item is first node 4.1 ptr1=ptr->next; 4.2 ptr1->prev=NULL; 4.3 header=ptr1; 4.4 end if 5.else 5.1 ptr1=ptr->prev; 5.2 ptr2=ptr->next; 5.3 ptr1->next=ptr2; 5.4 ptr2->prev=ptr1; 6. end else 7. end if
  • 31.
    header 20 1000 1010 302000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 1010 ptr 1010 ptr Before deleting a node at position 3 After deleting a node at position 3 2000 header 20 2000 1010 30 2000 2200 40 NULL 2020 10 2020 NULL 1010 2020 1000 2000 1010 ptr 2020 ptr 1 ptr 1000 ptr2
  • 32.
    Displaying elements ofa list Algorithm: 1. ptr=header; 2. if(header = = NULL) 1. printf("The list is emptyn"); 3. else 1. print “The elements in farword order: “ 2. while(ptr!=NULL) 1. print “ptr->data”; 2. if(ptr->next = = NULL) 1. break; 3. ptr=ptr->next; 3. print “The elements in reverse order: “ 4. while(ptr!=header) 1. if(ptr->next = = NULL) 1. print “ptr->data”; 2. else 1. print “ptr->data”; 2. ptr=ptr->prev; 3. print “ptr->data”; 3.end else 4. end else
  • 33.
    20 1000 1010 302000 2020 40 NULL 1000 10 2020 NULL 1010 2020 1000 2000 header 1010 ptr 1010 Forward Order : 10 20 30 40 Reverse Order : 40 30 20 10
  • 34.
    Circular linked list The linked list where the last node points the header node is called circular linked list. Circular singly linked list Circular doubly linked list
  • 35.
    Circular Linked Lists •A Circular Linked List is a special type of Linked List • It supports traversing from the end of the list to the beginning by making the last node point back to the head of the list • A Rear pointer is often used instead of a Head pointer Rear 10 20 40 70 55
  • 36.
    Motivation • Circular linkedlists are useful for playing video and sound files in “looping” mode. • They are also a stepping stone to implementing graphs.