Guest User

Untitled

a guest
Dec 8th, 2019
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node:
  2. def __init__ (self,value):
  3. self.value = value
  4. self.next = None
  5. self.prev = None
  6.  
  7. class LinkedList:
  8. def __init__ (self):
  9. self.head = None
  10. self.tail = None
  11. self.size = 0
  12.  
  13. def push(self, value):
  14. value = Node(value)
  15. if self.size >= 1:
  16. value.prev = self.tail
  17. self.tail.next = value
  18. else:
  19. self.head = value
  20. self.tail = value
  21. self.size += 1
  22.  
  23. def pop(self):
  24. deleted = self.tail
  25. if self.size > 1:
  26. self.tail = self.tail.prev
  27. self.tail.next = None
  28. self.size -= 1
  29. if self.size == 1:
  30. self.tail = None
  31. self.size -= 1
  32. return deleted
  33.  
  34. def unshift(self, value):
  35. value = Node(value)
  36. if self.size == 0:
  37. self.tail = value
  38. else:
  39. value.next = self.head
  40. self.head.prev = value
  41.  
  42. self.head = value
  43. self.size += 1
  44.  
  45. def shift(self):
  46. deleted = self.head
  47. self.head = self.head.next
  48. if self.size > 1:
  49. self.head.prev = None
  50. self.size -= 1
  51. return deleted
  52.  
  53. def find(self, v):
  54. current = self.head
  55. for i in range(self.size):
  56. if current.value == v:
  57. return current
  58.  
  59. current = current.next
  60.  
  61. return None
  62. def get(self, index):
  63. current = self.head
  64. for i in range(self.size):
  65. if i == index:
  66. return current
  67. current = current.next
  68.  
  69. def size(self):
  70. return self.size()
  71.  
  72. def print(self):
  73. current = self.head
  74. output = ''
  75. if self.size > 0:
  76. for i in range(self.size):
  77. output += str(current.value) + ' '
  78. current = current.next
  79.  
  80. print(output)
  81.  
  82. def insert(self, index, value):
  83. value = Node(value)
  84.  
  85. if index >= self.size - 1:
  86. self.push(value.value)
  87. elif index == 0:
  88. self.unshift(value.value)
  89. else:
  90. current = self.head
  91.  
  92. for i in range(self.size):
  93. if i == index:
  94. current.prev.next = value
  95. value.prev = current.prev
  96. value.next = current
  97. current.prev = value
  98. else:
  99. current = current.next
  100.  
  101. self.size += 1
  102. def printReverse(self):
  103. current = self.tail
  104. output = ''
  105.  
  106. if self.size > 0:
  107. for i in range(self.size):
  108. output += str(current.value) + ' '
  109. current = current.prev
  110.  
  111. print(output)
  112.  
  113.  
  114.  
  115. list = LinkedList()
  116. n = int(input())
  117. if n > 0: elements = input().split()
  118.  
  119. for i in range(n):
  120. list.push(float(elements[i]))
  121.  
  122. m = int(input())
  123.  
  124. for i in range(m):
  125. commands = input().split()
  126. commandType = commands[0]
  127. commandValue = None
  128. if len(commands) > 1:
  129. commandValue = commands[1]
  130.  
  131. if commandType == 'push':
  132. list.push(float(commandValue))
  133. elif commandType == 'pop':
  134. list.pop()
  135. elif commandType == 'unshift':
  136. list.unshift(float(commandValue))
  137. elif commandType == 'shift':
  138. list.shift()
  139. elif commandType == 'insert':
  140. list.insert(int(commandValue), float(commands[2]))
  141.  
  142. list.printReverse()
  143. list.print()
RAW Paste Data