fork download
  1. # your code goes here
  2. class Node(object):
  3. def __init__(self):
  4. self.data = 0
  5. self.next = None
  6. self.prev = None
  7. def __init__(self,data):
  8. self.data = data
  9. self.next = None
  10. self.prev = None
  11. def setData(self,data):
  12. self.data = data
  13. def getData(self):
  14. return self.data
  15. def setNext(self,next):
  16. self.next = next
  17. def getNext(self):
  18. return self.next
  19. def setPrev(self,prev):
  20. self.prev = prev
  21. def getPrev(self):
  22. return self.prev
  23.  
  24. class Stack(object):
  25. def __init__(self):
  26. self.top = None
  27. def setTop(self,top):
  28. self.top = top
  29. def getTop(self):
  30. return self.top
  31. def isEmpty(self):
  32. return self.top == None
  33. def push(self,data):
  34. newnode = Node(data)
  35. if not self.isEmpty():
  36. self.getTop().setPrev(newnode)
  37. newnode.setNext(self.getTop())
  38. self.setTop(newnode)
  39. def pop(self):
  40. if not self.isEmpty():
  41. temp = self.getTop()
  42. if not self.getTop().getNext() == None:
  43. self.getTop().getNext().setPrev(None)
  44. self.setTop(self.getTop().getNext())
  45. del temp
  46.  
  47. class Queue(object):
  48. def __init__(self):
  49. self.front = None
  50. self.rear = None
  51. def setFront(self,front):
  52. self.front = front
  53. def getFront(self):
  54. return self.front
  55. def setRear(self,rear):
  56. self.rear = rear
  57. def getRear(self):
  58. return self.rear
  59. def isEmpty(self):
  60. return self.getFront() == None
  61. def enqueue(self,data):
  62. newnode = Node(data)
  63. if self.isEmpty():
  64. self.setFront(newnode)
  65. else:
  66. self.getRear().setNext(newnode)
  67. newnode.setPrev(self.getRear())
  68. self.setRear(newnode)
  69. def dequeue(self):
  70. if not self.isEmpty():
  71. temp = self.getFront()
  72. if self.getFront().getNext() == None:
  73. self.setRear(None)
  74. else:
  75. self.getFront().getNext().setPrev(None)
  76. self.setFront(self.getFront().getNext())
  77. del temp
  78. def bubbleSort(A):
  79. n = len(A)
  80. i = 1
  81. while i < n:
  82. j = n - 1
  83. while j >= 1:
  84. if A[j] < A[j-1]:
  85. temp = A[j - 1]
  86. A[j - 1] = A[j]
  87. A[j] = temp
  88. j-=1
  89. i+=1
  90.  
  91. from random import randint
  92.  
  93. n = int(input())
  94. m = int(input());
  95.  
  96. S = Stack()
  97. counter = 1
  98. while(counter <= n):
  99. S.push(randint(0,m))
  100. counter += 1
  101.  
  102. A = []
  103.  
  104. while not S.isEmpty():
  105. if S.getTop().getData()%2==0:
  106. A.append(S.getTop().getData())
  107. S.pop()
  108.  
  109.  
  110. bubbleSort(A)
  111.  
  112. Q = Queue()
  113. counter = 1
  114. while(counter <= len(A)):
  115. Q.enqueue(A[counter-1])
  116. counter+=1
  117.  
  118.  
  119. while not Q.isEmpty():
  120. print(Q.getFront().getData())
  121. Q.dequeue()
  122.  
  123.  
  124.  
  125.  
  126.  
Success #stdin #stdout 0.02s 11772KB
stdin
50
1000
stdout
12
20
28
410
422
434
486
532
560
574
642
692
700
716
734
744
852
888
898
906
928
934
988