Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. # ZAD 3
  2. class Node:
  3. def __init__(self, data):
  4. self.data = data
  5. self.next = None
  6.  
  7. class LinkedListS:
  8. def __init__(self):
  9. self.head = None
  10.  
  11. def Insert(self, new_node):
  12. if self.head is None:
  13. new_node.next = self.head
  14. self.head = new_node
  15.  
  16. elif self.head.data >= new_node.data:
  17. new_node.next = self.head
  18. self.head = new_node
  19.  
  20. else :
  21. current = self.head
  22. while(current.next is not None and
  23. current.next.data < new_node.data):
  24. current = current.next
  25.  
  26. new_node.next = current.next
  27. current.next = new_node
  28.  
  29. def print_list(self):
  30. temp = self.head
  31. while temp:
  32. print(temp.data)
  33. temp = temp.next
  34. def srednia(self):
  35. temp = self.head
  36. suma = 0
  37. licznik = 0
  38. while temp:
  39. licznik += 1
  40. suma += temp.data
  41. temp = temp.next
  42. return suma/licznik
  43.  
  44. def mediana(self):
  45. temp = self.head
  46. lista = []
  47. licznik = 0
  48. while temp:
  49. licznik += 1
  50. lista.append(temp)
  51. temp = temp.next
  52. if licznik % 2 == 1:
  53. l = licznik //2
  54. licznik2 = 0
  55. for i in lista:
  56. if licznik2 == l:
  57. print(i.data)
  58. licznik2 += 1
  59. else:
  60. l = licznik //2
  61. licznik2 = 0
  62. suma = 0
  63. for i in lista:
  64.  
  65. if licznik2 == l-1 or licznik2 == l:
  66. suma += i.data
  67. licznik2 += 1
  68.  
  69. return suma/2
  70.  
  71.  
  72. L = LinkedListS()
  73. L.Insert(Node(1))
  74. L.Insert(Node(3))
  75. L.Insert(Node(2))
  76. L.Insert(Node(2))
  77.  
  78. L.print_list()
  79. print(" ")
  80.  
  81. L.mediana()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement