the art of
Algorithm
Notes on Analysis and Design



Circular Linked List

Function for implementing Circular linked list

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
class Node:
	def __init__(self,data):
		self.data=data
		self.next=None
class CLL:
	def __init__(self):
		self.head=None
	#Function for inserting node at the begining
	def push(self,data):
		ptr1=Node(data)
		temp=self.head
		ptr1.next=self.head
		#if LL is not None 
		#then set the next of last node
		if self.head is not None:

			while(temp.next!=self.head):
				temp=temp.next
			temp.next=ptr1
		else:
			ptr1.next=ptr1 #For first Node
		self.head=ptr1
	#Function to print nodes in a given CLL
	def printCLL(self):
		temp=self.head
		if self.head is not None:
			while(True):
				print temp.data
				temp=temp.next
				if temp==self.head:

					break
#Main program
cll=CLL()
# Created linked list will be 11->2->56->12
cll.push(21)
cll.push(65)
cll.push(20)
cll.push(14)
 
print "Contents of circular Linked List"
cllist.printList()