Advertisement
Guest User

Untitled

a guest
Feb 17th, 2015
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ListNode:
  2.     def __init__(self,data,next):
  3.         self.data = data
  4.         self.next = next
  5.  
  6.     def __repr__(self):
  7.         return str(self.data)
  8.  
  9. class MyCircularLinkedList:
  10.     def __init__(self):
  11.         self.tail = None
  12.  
  13.     def __repr__(self):
  14.         s = ''
  15.         current = self.tail
  16.         if current != None:
  17.             s = s + str(current)
  18.             current = current.next
  19.         while current != self.tail:
  20.             s = s + " -> " + str(current)
  21.             current = current.next
  22.         if not s: # s == '':
  23.             s = 'empty list'
  24.         return s
  25.  
  26.     def append(self,e):
  27.         if not self.tail: # self.head == None:
  28.             self.tail = ListNode(e,None)
  29.             self.tail.next = self.tail
  30.         else:
  31.             n = ListNode(e,None)
  32.            
  33.             n.next = self.tail.next
  34.             self.tail.next = n
  35.             self.tail = n
  36.  
  37.     def delete(self,e):
  38.         if self.tail:
  39.             if self.tail.next == self.tail:
  40.                 self.tail = None
  41.             else :
  42.                 n = self.tail
  43.                 while n.next.data != e:
  44.                     n = n.next
  45.                     if n.next == self.tail:
  46.                         self.tail = n.next.next
  47.                 n.next = n.next.next
  48.                
  49.                
  50. mylist =  MyCircularLinkedList()
  51. print(mylist)
  52. mylist.append(1)
  53. print(mylist)
  54. mylist.append(2)
  55. print(mylist)
  56. mylist.append(3)
  57. print(mylist)
  58. mylist.delete(2)
  59. print(mylist)
  60. mylist.delete(1)
  61. print(mylist)
  62. mylist.delete(3)
  63. print(mylist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement