Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ZAD 3
- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class LinkedListS:
- def __init__(self):
- self.head = None
- def Insert(self, new_node):
- if self.head is None:
- new_node.next = self.head
- self.head = new_node
- elif self.head.data >= new_node.data:
- new_node.next = self.head
- self.head = new_node
- else :
- current = self.head
- while(current.next is not None and
- current.next.data < new_node.data):
- current = current.next
- new_node.next = current.next
- current.next = new_node
- def print_list(self):
- temp = self.head
- while temp:
- print(temp.data)
- temp = temp.next
- def srednia(self):
- temp = self.head
- suma = 0
- licznik = 0
- while temp:
- licznik += 1
- suma += temp.data
- temp = temp.next
- return suma/licznik
- def mediana(self):
- temp = self.head
- lista = []
- licznik = 0
- while temp:
- licznik += 1
- lista.append(temp)
- temp = temp.next
- if licznik % 2 == 1:
- l = licznik //2
- licznik2 = 0
- for i in lista:
- if licznik2 == l:
- print(i.data)
- licznik2 += 1
- else:
- l = licznik //2
- licznik2 = 0
- suma = 0
- for i in lista:
- if licznik2 == l-1 or licznik2 == l:
- suma += i.data
- licznik2 += 1
- return suma/2
- L = LinkedListS()
- L.Insert(Node(1))
- L.Insert(Node(3))
- L.Insert(Node(2))
- L.Insert(Node(2))
- L.print_list()
- print(" ")
- L.mediana()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement