SlideShare a Scribd company logo
1 of 17
Circular Linked List
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 deletionnโ€);
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 insertionnโ€);
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:%snThe Account No:%dnThe
Balance:%.2fnโ€, 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 listnโ€);
exit(1);
}
do{
sum=sum + p->info;
p =p->next;
count++;
}while(p->flag !=1);
return sum/count;
}

More Related Content

Similar to Circular_Linked_List.ppt

LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptxManojUniversity
ย 
Data structures
Data structuresData structures
Data structuresJauhar Amir
ย 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).pptSwarChaudhary
ย 
Data structure
Data structureData structure
Data structureNida Ahmed
ย 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
ย 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
ย 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptxPoonamPatil120
ย 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptxSherinRappai
ย 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
ย 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
ย 
CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.pptArifKamal36
ย 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptxSaralaT3
ย 
Linked List in Data Structure
Linked List in Data StructureLinked List in Data Structure
Linked List in Data StructureMeghaj Mallick
ย 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxJoannahClaireAlforqu
ย 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
ย 
Adt of lists
Adt of listsAdt of lists
Adt of listsNivegeetha
ย 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure conceptsAkila Krishnamoorthy
ย 

Similar to Circular_Linked_List.ppt (20)

17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
ย 
LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptx
ย 
Data structures
Data structuresData structures
Data structures
ย 
linkedlist (1).ppt
linkedlist (1).pptlinkedlist (1).ppt
linkedlist (1).ppt
ย 
Data structure
Data structureData structure
Data structure
ย 
Linked lists
Linked listsLinked lists
Linked lists
ย 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
ย 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
ย 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
ย 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
ย 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
ย 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
ย 
CH5_Linked List.ppt
CH5_Linked List.pptCH5_Linked List.ppt
CH5_Linked List.ppt
ย 
CH5_Linked List.pptx
CH5_Linked List.pptxCH5_Linked List.pptx
CH5_Linked List.pptx
ย 
Linked List in Data Structure
Linked List in Data StructureLinked List in Data Structure
Linked List in Data Structure
ย 
Data structures2
Data structures2Data structures2
Data structures2
ย 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
ย 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
ย 
Adt of lists
Adt of listsAdt of lists
Adt of lists
ย 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
ย 

Recently uploaded

Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
ย 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
ย 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
ย 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7Call Girls in Nagpur High Profile Call Girls
ย 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
ย 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
ย 
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoordharasingh5698
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
ย 
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 

Recently uploaded (20)

Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
ย 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
ย 
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Ramesh Nagar Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
ย 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
ย 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
ย 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
ย 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
ย 
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor ๐Ÿ“ฑ {7001035870} VIP Escorts chittoor
ย 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort ServiceCall Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
Call Girls in Netaji Nagar, Delhi ๐Ÿ’ฏ Call Us ๐Ÿ”9953056974 ๐Ÿ” Escort Service
ย 

Circular_Linked_List.ppt

  • 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 deletionnโ€); 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 insertionnโ€); 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:%snThe Account No:%dnThe Balance:%.2fnโ€, 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 listnโ€); exit(1); } do{ sum=sum + p->info; p =p->next; count++; }while(p->flag !=1); return sum/count; }