Circular Linked List in C

Here you will get program for circular linked list in C.

What is Circular Linked List?

A circular linked list is a linked list in which the last node points to the head or front node making the data structure to look like a circle. A circularly linked list node can be implemented using singly linked or doubly linked list.

The below representation shows how a circular linked list looks like. Unlike the linear linked list, the last node is pointed to the head using a rear pointer.

Program for Circular Linked List in C

We basically have 3 nodes head, rear and temp to implement a circular linked list. The rear points to the last node of the list, head points to first node. This helps to maintain a track on where is the front and rear nodes of the circle. Let us see the basic operations such as creation, deletion and displaying of the elements of the circular linked list.

Creation of Node

This is much similar to that of creating a node in singly linked list. It involves creating a new node and assigning data and pointing the current node to the head of the circular linked list. The code is as shown below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

voidcreate()

{

node*newnode;

newnode=(node*)malloc(sizeof(node));

printf("\nEnter the node value : ");

scanf("%d",&newnode->info);

newnode->next=NULL;

if(rear==NULL)

front=rear=newnode;

else

{

rear->next=newnode;

rear=newnode;

}

rear->next=front;

}

Deletion of Node

We conventionally delete the front node from the list in this program. To delete a node, we need to check if the list is empty. If it is not empty then point the rear node to the front->next and rear->next to front. This removes the first node.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

voiddel()

{

temp=front;

if(front==NULL)

printf("\nUnderflow :");

else

{

if(front==rear)

{

printf("\n%d",front->info);

front=rear=NULL;

}

else

{

printf("\n%d",front->info);

front=front->next;

rear->next=front;

}

temp->next=NULL;

free(temp);

}

}

Traversing Circular Linked List

Traversing the circular list starts from front node and iteratively continues until the rear node. The following function is used for this purpose.

1

2

3

4

5

6

7

8

9

10

11

12

13

voiddisplay()

{

temp=front;

if(front==NULL)

printf("\nEmpty");

else

{

printf("\n");

for(;temp!=rear;temp=temp->next)

printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);

printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);

}

}

Complete Program

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

#include<stdio.h>

#include<stdlib.h>

typedefstructNode

{

intinfo;

structNode*next;

}node;

node*front=NULL,*rear=NULL,*temp;

voidcreate();

voiddel();

voiddisplay();

intmain()

{

intchc;

do

{

printf("\nMenu\n\t 1 to create the element : ");

printf("\n\t 2 to delete the element : ");

printf("\n\t 3 to display the queue : ");

printf("\n\t 4 to exit from main : ");

printf("\nEnter your choice : ");

scanf("%d",&chc);

switch(chc)

{

case1:

create();

break;

case2:

del();

break;

case3:

display();

break;

case4:

return1;

default:

printf("\nInvalid choice :");

}

}while(1);

return0;

}

voidcreate()

{

node*newnode;

newnode=(node*)malloc(sizeof(node));

printf("\nEnter the node value : ");

scanf("%d",&newnode->info);

newnode->next=NULL;

if(rear==NULL)

front=rear=newnode;

else

{

rear->next=newnode;

rear=newnode;

}

rear->next=front;

}

voiddel()

{

temp=front;

if(front==NULL)

printf("\nUnderflow :");

else

{

if(front==rear)

{

printf("\n%d",front->info);

front=rear=NULL;

}

else

{

printf("\n%d",front->info);

front=front->next;

rear->next=front;

}

temp->next=NULL;

free(temp);

}

}

voiddisplay()

{

temp=front;

if(front==NULL)

printf("\nEmpty");

else

{

printf("\n");

for(;temp!=rear;temp=temp->next)

printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);

printf("\n%d address=%u next=%u\t",temp->info,temp,temp->next);

}

}

Output

The above program results in the following output. You can see that the last node points to the first node address that is the main theme of circular linked list in C.