Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Fri Mar 27 11:34:11 2020
- @author: aleksanderwardyn
- """
- class Node:
- def __init__(self,x = None, next = None, prev = None):
- self.key = x
- self.next = next
- self.prev = prev
- class LinkedList:
- def __init__(self):
- self.head = None
- def wstaw(self, x):
- x.next = self.head
- if self.head != None:
- self.head.prev = x
- self.head = x
- x.prev = None
- def wypisz(self):
- x = self.head
- while x:
- print(x.key)
- x = x.next
- def szukaj(self,k):
- x = self.head
- while x!=None and x.key!=k:
- x = x.next
- return x
- def usun(self,x):
- if (x == None):
- return None
- if x.prev != None:
- x.prev.next = x.next
- else:
- self.head = x.next
- if x.next != None:
- x.next.prev = x.prev
- def bezPowtorzen(self):
- copy = LinkedList()
- buffer = self.head
- while buffer.next != None:
- copy.wstaw(Node(buffer.key))
- buffer = buffer.next
- copy.wstaw(Node(buffer.key))
- current = second = copy.head
- while current is not None:
- while second.next is not None:
- if second.next.key == current.key:
- second.next = second.next.next
- else:
- second = second.next
- current = second = current.next
- return copy
- def merge(List_1, List_2):
- head_ptr = temp_ptr = Node()
- while List_1 or List_2:
- if List_1 and (not List_2 or List_1.key <= List_2.key):
- temp_ptr.next = Node(List_1.key)
- List_1 = List_1.next
- else:
- temp_ptr.next = Node(List_2.key)
- List_2 = List_2.next
- temp_ptr = temp_ptr.next
- return head_ptr.next
- print("Podpunkt1")
- l = LinkedList()
- l.wstaw(Node("ala"))
- l.wstaw(Node("ma"))
- l.wstaw(Node("kota"))
- l.wstaw(Node("ala"))
- l.wypisz()
- print("----------")
- l.usun(l.szukaj("a"))
- l.wypisz()
- print("-----")
- print("Podpunkt 2")
- s = l.bezPowtorzen()
- s.wypisz()
- print("----")
- print("Podpunkt 3")
- l.head = merge(l.head, s.head)
- l.wypisz()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement