1+ class Node :
2+ def __init__ (self ,data ):
3+ self .data = data
4+ self .next = None
5+
6+
7+ class CircularLinkedList :
8+ def __init__ (self ):
9+ self .head = None
10+
11+ def get_node (self ,index ):
12+ if self .head is None :
13+ return None
14+ current = self .head
15+ for i in range (index ):
16+ current = current .next
17+ if current == self .head :
18+ return None
19+ return current
20+
21+ def get_prev_node (self ,ref_node ):
22+ if self .head is None :
23+ return None
24+ current = self .head
25+ while current .next != ref_node :
26+ current = current .next
27+ return current
28+
29+ def insert_after (self ,ref_node ,new_node ):
30+ new_node .next = ref_node .next
31+ ref_node .next = new_node
32+
33+ def insert_before (self ,ref_node ,new_node ):
34+ prev_node = self .get_prev_node (ref_node )
35+ self .insert_after (prev_node ,new_node )
36+
37+ def insert_at_end (self ,new_node ):
38+ if self .head is None :
39+ self .head = new_node
40+ new_node .next = new_node
41+ else :
42+ self .insert_before (self .head ,new_node )
43+
44+ def insert_at_beg (self ,new_node ):
45+ self .insert_at_end (new_node )
46+ self .head = new_node
47+
48+ def remove (self ,node ):
49+ if self .head .next == self .head :
50+ self .head = None
51+ else :
52+ prev_node = self .get_prev_node (node )
53+ prev_node .next = node .next
54+ if self .head == node :
55+ self .head = node .next
56+
57+ def display (self ):
58+ if self .head is None :
59+ return
60+ current = self .head
61+ while True :
62+ print (current .data ,end = ' ' )
63+ current = current .next
64+ if current == self .head :
65+ break
66+
67+
68+ a_cllist = CircularLinkedList ()
69+
70+ print ('Menu' )
71+ print ('insert <data> after <index>' )
72+ print ('insert <data> before <index>' )
73+ print ('insert <data> at beg' )
74+ print ('insert <data> at end' )
75+ print ('remove <index>' )
76+ print ('quit' )
77+
78+ while True :
79+ print ('The list: ' ,end = '' )
80+ a_cllist .display ()
81+ print ()
82+ do = input ('What would you like to do? ' ).split ()
83+
84+ operation = do [0 ].strip ().lower ()
85+
86+ if operation == 'insert' :
87+ data = int (do [1 ])
88+ position = do [3 ].strip ().lower ()
89+ new_node = Node (data )
90+ suboperation = do [2 ].strip ().lower ()
91+ if suboperation == 'at' :
92+ if position == 'beg' :
93+ a_cllist .insert_at_beg (new_node )
94+ elif position == 'end' :
95+ a_cllist .insert_at_end (new_node )
96+ else :
97+ index = int (position )
98+ ref_node = a_cllist .get_node (index )
99+ if ref_node is None :
100+ print ('No such index.' )
101+ continue
102+ if suboperation == 'after' :
103+ a_cllist .insert_after (ref_node ,new_node )
104+ elif suboperation == 'before' :
105+ a_cllist .insert_before (ref_node ,new_node )
106+
107+ elif operation == 'remove' :
108+ index = int (do [1 ])
109+ node = a_cllist .get_node (index )
110+ if node is None :
111+ print ('No such index.' )
112+ continue
113+ a_cllist .remove (node )
114+
115+ elif operation == 'quit' :
116+ break