Circular Linked List EENG212  Algorithms and  Data Structures
Circular Linked Lists In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list   must be preserved in order to be able to reference the list again. Circular linked lists can be used to help the traverse the same list again and again if needed. A   circular list is very similar to the linear list where in the circular list the pointer of the last node   points not NULL but the first node.
Circular Linked Lists A Linear Linked List
Circular Linked Lists
Circular Linked Lists
Circular Linked Lists In a circular linked list there are two methods to know if a node is the first node or not. Either a external pointer,  list , points the first node or A  header node  is placed as the first node of the circular list. The header node can be separated from the others by either heaving a  sentinel value  as the   info part or having a dedicated  flag  variable to specify if the node is a header node or not .
PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The structure definition of the circular linked lists and the linear linked list is the same: struct node{   int info;   struct node *next; }; typedef struct node  *NODEPTR ;
PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The delete after and insert after functions of the linear lists and the circular lists are almost the   same. The delete   after function : delafter( ) void delafter( NODEPTR  p, int *px) { NODEPTR q; if((p == NULL) || (p == p->next)){ /*the empty list contains a single node and may be pointing itself*/ printf(“void deletion\n”); exit(1); } q = p->next; *px = q->info; /*the data of the deleted node*/ p->next = q->next; freenode(q); }
PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The insertafter function : insafter( ) void insafter( NODEPTR  p, int x) { NODEPTR q ; if(p == NULL){ printf(“void insertion\n”); exit(1); } q = getnode(); q->info = x; /*the data of the inserted node*/ q->next = p->next; p->next = q; }
CIRCULAR LIST with header node The header node in a circular list can be specified by a   sentinel value  or a dedicated  flag : Header Node with Sentinel:  Assume that info part contains positive integers. Therefore the info   part of a header node can be -1. The following circular list is an example for a sentinel used to   represent the header node: struct node{ int info; struct node *next; }; typedef struct node  *NODEPTR ;
CIRCULAR LIST with header node
CIRCULAR LIST with header node Header Node with Flag:  In this case a extra variable called flag can be used to represent the   header node. For example flag in the header node can be 1, where the flag is 0 for the other   nodes. struct node{ int flag; int info; struct node *next; }; typedef struct node  *NODEPTR ;
CIRCULAR LIST with header node
Example Consider a circular linked list with a header node, where each node contains the name,   account number and the balance of a bank customer. The header node contains a sentinel   account number to be -99. (a)  Write an appropriate node structure definition for the circular linked list. (b)  Write a function to display the full records of the customers with negative balance.
a) struct node{ char Name[15];   int AccNo; float Balance; struct node *next; }; typedef struct node  *NODEPTR ; b)  Assume that the list pointer points the header with the sentinel   account number -99 . void DispNegBalanca( NODEPTR  *plist) { NODEPTR  p; p= * plist; if(p == NULL){ printf(“There is no list!\n”); exit(1); } p=p->next; while(p->AccNo!=-99){ if(p->Balance < 0.0) printf(“The Customer Name:%s\nThe Account No:%d\nThe Balance:%.2f\n”, p->Name, p->AccNo, p->Balance); p=p->next; } }
Example Write a function that returns the average of the numbers in a circular list. Assume that the   following node structure is used, where the flag variable is 1 for the header node and 0 for all the   other nodes. struct node{ int flag; float info; struct node *next; }; typedef struct node  *NODEPTR ;
float avList( NODEPTR  *plist) /*assume that plist points the header node*/ { int count=0; float sum =0.0; NODEPTR  p; p= * plist; if((p == NULL)){ printf(“Empty list\n”); exit(1); } do{ sum=sum + p->info; p =p->next; count++; }while(p->flag !=1); return sum/count; }

More Related Content

PPTX
Stack & Queue using Linked List in Data Structure
PPTX
Stacks IN DATA STRUCTURES
PPTX
linked list in Data Structure, Simple and Easy Tutorial
PPTX
Circular link list.ppt
PPT
Linked list
PPT
PPTX
Stack and its operations
PDF
2nd puc computer science chapter 3 data structures 1
Stack & Queue using Linked List in Data Structure
Stacks IN DATA STRUCTURES
linked list in Data Structure, Simple and Easy Tutorial
Circular link list.ppt
Linked list
Stack and its operations
2nd puc computer science chapter 3 data structures 1

What's hot (20)

PPT
Infix prefix postfix
PDF
Data structures
PPTX
Doubly Linked List
PPTX
Queue
PDF
PPT
C++ Arrays
PPTX
Ppt on this and super keyword
PPTX
Stack and Queue
PPTX
Graph traversals in Data Structures
PPTX
Polymorphism in java
PPTX
Threaded Binary Tree
PDF
Queue as data_structure
PPTX
Super keyword in java
PPTX
PPTX
Trees in data structures
PDF
Applications of stack
PDF
itft-Inheritance in java
PPT
Data Structures- Part5 recursion
PPTX
Super Keyword in Java.pptx
Infix prefix postfix
Data structures
Doubly Linked List
Queue
C++ Arrays
Ppt on this and super keyword
Stack and Queue
Graph traversals in Data Structures
Polymorphism in java
Threaded Binary Tree
Queue as data_structure
Super keyword in java
Trees in data structures
Applications of stack
itft-Inheritance in java
Data Structures- Part5 recursion
Super Keyword in Java.pptx
Ad

Similar to Circular linked list (20)

PPT
Circular_Linked_List.ppt
PPT
Linked list1.ppt
PPT
Link List
PPTX
Linked List.pptx
PPTX
Linked Lists, Single Linked list and its operations
PPT
linkedlist (1).ppt
PPTX
Data structure
PPT
17 linkedlist (1)
PPT
Linkedlist
PPT
Wk11-linkedlist.ppt
PPT
Wk11-linkedlist.ppt
PPT
linkedlistqwerwwdderdsddsseddddddsdrr.ppt
PPT
Algo>ADT list & linked list
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
PPTX
LINKED LIST.pptx
PPT
linked-list - Abstract data type (ADT) Linked Lists
PDF
Fjdkkdnncmckkgkhkhkkhkhkhkhkhkhkhkhkhkhhl
PPT
Abstract data types
PPT
linkedlist.ppt
Circular_Linked_List.ppt
Linked list1.ppt
Link List
Linked List.pptx
Linked Lists, Single Linked list and its operations
linkedlist (1).ppt
Data structure
17 linkedlist (1)
Linkedlist
Wk11-linkedlist.ppt
Wk11-linkedlist.ppt
linkedlistqwerwwdderdsddsseddddddsdrr.ppt
Algo>ADT list & linked list
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
LINKED LIST.pptx
linked-list - Abstract data type (ADT) Linked Lists
Fjdkkdnncmckkgkhkhkkhkhkhkhkhkhkhkhkhkhhl
Abstract data types
linkedlist.ppt
Ad

Recently uploaded (20)

DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
20th Century Theater, Methods, History.pptx
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
HVAC Specification 2024 according to central public works department
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Unit 4 Computer Architecture Multicore Processor.pptx
Environmental Education MCQ BD2EE - Share Source.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Hazard Identification & Risk Assessment .pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
20th Century Theater, Methods, History.pptx
Virtual and Augmented Reality in Current Scenario
History, Philosophy and sociology of education (1).pptx
Introduction to pro and eukaryotes and differences.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Chinmaya Tiranga quiz Grand Finale.pdf
HVAC Specification 2024 according to central public works department
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Cambridge-Practice-Tests-for-IELTS-12.docx

Circular linked list

  • 1. Circular Linked List EENG212 Algorithms and Data Structures
  • 2. Circular Linked Lists In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again. Circular linked lists can be used to help the traverse the same list again and again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node.
  • 3. Circular Linked Lists A Linear Linked List
  • 6. Circular Linked Lists In a circular linked list there are two methods to know if a node is the first node or not. Either a external pointer, list , points the first node or A header node is placed as the first node of the circular list. The header node can be separated from the others by either heaving a sentinel value as the info part or having a dedicated flag variable to specify if the node is a header node or not .
  • 7. PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The structure definition of the circular linked lists and the linear linked list is the same: struct node{ int info; struct node *next; }; typedef struct node *NODEPTR ;
  • 8. PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The delete after and insert after functions of the linear lists and the circular lists are almost the same. The delete after function : delafter( ) void delafter( NODEPTR p, int *px) { NODEPTR q; if((p == NULL) || (p == p->next)){ /*the empty list contains a single node and may be pointing itself*/ printf(“void deletion\n”); exit(1); } q = p->next; *px = q->info; /*the data of the deleted node*/ p->next = q->next; freenode(q); }
  • 9. PRIMITIVE FUNCTIONS IN CIRCULAR LISTS The insertafter function : insafter( ) void insafter( NODEPTR p, int x) { NODEPTR q ; if(p == NULL){ printf(“void insertion\n”); exit(1); } q = getnode(); q->info = x; /*the data of the inserted node*/ q->next = p->next; p->next = q; }
  • 10. CIRCULAR LIST with header node The header node in a circular list can be specified by a sentinel value or a dedicated flag : Header Node with Sentinel: Assume that info part contains positive integers. Therefore the info part of a header node can be -1. The following circular list is an example for a sentinel used to represent the header node: struct node{ int info; struct node *next; }; typedef struct node *NODEPTR ;
  • 11. CIRCULAR LIST with header node
  • 12. CIRCULAR LIST with header node Header Node with Flag: In this case a extra variable called flag can be used to represent the header node. For example flag in the header node can be 1, where the flag is 0 for the other nodes. struct node{ int flag; int info; struct node *next; }; typedef struct node *NODEPTR ;
  • 13. CIRCULAR LIST with header node
  • 14. Example Consider a circular linked list with a header node, where each node contains the name, account number and the balance of a bank customer. The header node contains a sentinel account number to be -99. (a) Write an appropriate node structure definition for the circular linked list. (b) Write a function to display the full records of the customers with negative balance.
  • 15. a) struct node{ char Name[15]; int AccNo; float Balance; struct node *next; }; typedef struct node *NODEPTR ; b) Assume that the list pointer points the header with the sentinel account number -99 . void DispNegBalanca( NODEPTR *plist) { NODEPTR p; p= * plist; if(p == NULL){ printf(“There is no list!\n”); exit(1); } p=p->next; while(p->AccNo!=-99){ if(p->Balance < 0.0) printf(“The Customer Name:%s\nThe Account No:%d\nThe Balance:%.2f\n”, p->Name, p->AccNo, p->Balance); p=p->next; } }
  • 16. Example Write a function that returns the average of the numbers in a circular list. Assume that the following node structure is used, where the flag variable is 1 for the header node and 0 for all the other nodes. struct node{ int flag; float info; struct node *next; }; typedef struct node *NODEPTR ;
  • 17. float avList( NODEPTR *plist) /*assume that plist points the header node*/ { int count=0; float sum =0.0; NODEPTR p; p= * plist; if((p == NULL)){ printf(“Empty list\n”); exit(1); } do{ sum=sum + p->info; p =p->next; count++; }while(p->flag !=1); return sum/count; }