Advertisement
Guest User

zad

a guest
Apr 2nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #!/usr/bin/env python3
  3. # -*- coding: utf-8 -*-
  4. """
  5. Created on Fri Mar 27 11:34:11 2020
  6.  
  7. @author: aleksanderwardyn
  8. """
  9.  
  10. class Node:
  11.     def __init__(self,x = None, next = None, prev = None):
  12.         self.key = x
  13.         self.next = next
  14.         self.prev = prev
  15.  
  16.  
  17. class LinkedList:
  18.     def __init__(self):
  19.         self.head = None
  20.  
  21.     def wstaw(self, x):
  22.         x.next = self.head  
  23.         if self.head != None:
  24.            self.head.prev = x
  25.         self.head = x
  26.         x.prev = None
  27.  
  28.     def wypisz(self):
  29.         x = self.head
  30.         while x:
  31.             print(x.key)
  32.             x = x.next
  33.  
  34.     def szukaj(self,k):
  35.         x = self.head
  36.         while x!=None and x.key!=k:
  37.             x = x.next
  38.         return x
  39.  
  40.     def usun(self,x):  
  41.         if (x == None):
  42.             return None
  43.         if x.prev != None:
  44.            x.prev.next = x.next
  45.         else:
  46.            self.head = x.next
  47.         if x.next != None:
  48.            x.next.prev = x.prev
  49.  
  50.     def bezPowtorzen(self):
  51.         copy = LinkedList()
  52.         buffer = self.head
  53.         while buffer.next != None:
  54.             copy.wstaw(Node(buffer.key))
  55.             buffer = buffer.next
  56.         copy.wstaw(Node(buffer.key))
  57.        
  58.         current = second = copy.head
  59.         while current is not None:
  60.             while second.next is not None:
  61.                 if second.next.key == current.key:
  62.                     second.next = second.next.next
  63.                 else:
  64.                     second = second.next
  65.             current = second = current.next
  66.         return copy
  67.  
  68. def merge(List_1, List_2):
  69.     head_ptr = temp_ptr = Node()
  70.     while List_1 or List_2:
  71.         if List_1 and (not List_2 or List_1.key <= List_2.key):
  72.             temp_ptr.next = Node(List_1.key)
  73.             List_1 = List_1.next
  74.         else:
  75.             temp_ptr.next = Node(List_2.key)
  76.             List_2 = List_2.next
  77.         temp_ptr = temp_ptr.next
  78.     return head_ptr.next
  79.  
  80. print("Podpunkt1")            
  81. l = LinkedList()
  82. l.wstaw(Node("ala"))
  83. l.wstaw(Node("ma"))
  84. l.wstaw(Node("kota"))
  85. l.wstaw(Node("ala"))
  86. l.wypisz()
  87. print("----------")
  88. l.usun(l.szukaj("a"))
  89. l.wypisz()
  90. print("-----")
  91. print("Podpunkt 2")
  92. s = l.bezPowtorzen()
  93. s.wypisz()
  94. print("----")
  95. print("Podpunkt 3")
  96. l.head = merge(l.head, s.head)
  97. l.wypisz()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement