Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ListNode:
- def __init__(self,data,next):
- self.data = data
- self.next = next
- def __repr__(self):
- return str(self.data)
- class MyCircularLinkedList:
- def __init__(self):
- self.tail = None
- def __repr__(self):
- s = ''
- current = self.tail
- if current != None:
- s = s + str(current)
- current = current.next
- while current != self.tail:
- s = s + " -> " + str(current)
- current = current.next
- if not s: # s == '':
- s = 'empty list'
- return s
- def append(self,e):
- if not self.tail: # self.head == None:
- self.tail = ListNode(e,None)
- self.tail.next = self.tail
- else:
- n = ListNode(e,None)
- n.next = self.tail.next
- self.tail.next = n
- self.tail = n
- def delete(self,e):
- if self.tail:
- if self.tail.next == self.tail:
- self.tail = None
- else :
- n = self.tail
- while n.next.data != e:
- n = n.next
- if n.next == self.tail:
- self.tail = n.next.next
- n.next = n.next.next
- mylist = MyCircularLinkedList()
- print(mylist)
- mylist.append(1)
- print(mylist)
- mylist.append(2)
- print(mylist)
- mylist.append(3)
- print(mylist)
- mylist.delete(2)
- print(mylist)
- mylist.delete(1)
- print(mylist)
- mylist.delete(3)
- print(mylist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement