Пример #1
0
class Stack:
   def __init__(self):
      self.list = Linked_list()
      self.size = 0

   def push(self,data):
      self.list.add_node_head(data)
      self.size += 1

   def pop(self):
      if self.isEmpty():
         return None
      else :
         data = self.top()
         self.list.delete_node(data)
         self.size -= 1
         return data

   def top(self):
      if self.isEmpty():
         return None
      else:
         return self.list.get_head().data

   def isEmpty(self):
      if self.size == 0:
         return True
      else:
         return False
Пример #2
0
    def test_delete_first_item(sef):
        ll = Linked_list(array)
        item = 1

        if item == ll.head.data:
            ll.head = ll.head.next
        assert (ll.head.data == 2)
Пример #3
0
 def test_replace_implementation(self):
     ll = Linked_list(array)
     assert ll.replace(8, 1) == 8  #replace first element
     assert ll.replace(9, 3) == 9  #replace item in the middle
     assert ll.replace(10, 6) == 10  #replace last item
     assert ll.replace(0, 45) == None  #replace item that doesn't exist
     assert ll.size == 6  #size doesn't change
Пример #4
0
 def test_prepend_implementation(self):
     ll = Linked_list()
     assert ll.prepend(1) == 1
     assert ll.prepend(2) == 2
     assert ll.head.data == 2
     assert ll.tail.data == 1
     assert ll.size == 2
Пример #5
0
    def test_find(self):

        ll = Linked_list(array)
        #pdb.set_trace()
        assert ll.find(lambda item: item == 1) == 1
        assert ll.find(lambda item: item < 2) == 1
        assert ll.find(lambda item: item > 3) == 4
        assert ll.find(lambda item: item == 7) is None
Пример #6
0
    def test_delete_implementation(self):

        ll = Linked_list(array)

        assert ll.delete(1) == 1
        assert (ll.delete(4)) == 4
        assert (ll.delete(6)) == 6
        assert (ll.delete(9)) == None
        assert (ll.size == 3)
Пример #7
0
    def test_delete_last_node(self):
        ll = Linked_list(array)
        item = 6
        current_node = ll.head
        previous_node = current_node

        while current_node is not None:

            if current_node is not None and current_node.next is None:
                ll.tail = previous_node
            previous_node = current_node
            current_node = current_node.next
        assert (ll.tail.data == 5)
Пример #8
0
    def test_delete_node_in_middle(self):
        ll = Linked_list(array)
        item = 4
        current_node = ll.head
        previous_node = current_node
        while current_node is not None:

            if current_node.data == item:
                current_node = current_node.next
                previous_node.next = current_node

            previous_node = current_node
            current_node = current_node.next

        assert ll.find(lambda item: item == 4) is None
Пример #9
0
    def split(linked_list):
        if  linked_list == None or linked_list.head == None:
            left_half = linked_list
            right_half = None
            return left_half,right_half
        else:
            size = linked_list.size()
            mid = size // 2
            mid_node = linked_list.node_at_index(mid-1)
            left_half = linked_list
            right_half = Linked_list()
            right_half.head = mid_node.next_Node
            
            mid_node.next_Node = None    

        return left_half,right_half
Пример #10
0
class Queue:
   def __init__(self):
      self.list = Linked_list()
      self.size = 0
   
   def poll(self):
      if self.isEmpty():
         return None
      else:
         data = self.peek()
         # linked list should store last element and delete node in O(1)
         self.list.delete_node(data)
         self.size -= 1
         return data

   def peek(self):
      if self.isEmpty():
         return None
      else:
         return self.list.get_head().data

   def add(self,data):
      self.list.add_node(data)
      self.size += 1

   def isEmpty(self):
      if self.size == 0:
         return True
      else:
         return False
   
   def print_queue(self):
      self.list.list_print()
Пример #11
0
    def merge(left,right):

        merge = Linked_list()
        merge.add(0)
        
        
        current = merge.head
        
    

        left_head = left.head
        
        right_head = right.head
      

        while left_head or right_head:
            

            if left_head is None:
                current.next_Node = right_head
                
                right_head = right_head.next_Node
                

            elif right_head is None:
                current.next_Node = left_head
                left_head = left_head.next_Node
            else:
                

                left_data = left_head.data    
                right_data = right_head.data 

                if left_data < right_data :
                    current.next_Node = left_head
                    left_head = left_head.next_Node
                else:
                    current.next_Node = right_head
                    right_head = right_head.next_Node
                   
            current = current.next_Node
            
        head = merge.head.next_Node    
        merge.head = head
        return merge
Пример #12
0
    def test_replace(self):

        ll = Linked_list(array)
        newItem = Node(8)
        replaceItem = 6

        current = ll.head
        previous = current

        while current:
            if current.data == replaceItem:
                current = current.next
                previous.next = newItem
                newItem.next = current
                break
            previous = current
            current = current.next
        assert (ll.find(lambda item: item == 8)) == 8
        assert (ll.find(lambda item: item == 6)) == None
Пример #13
0
    def test_delete_none_existing_item(sef):
        ll = Linked_list(array)
        current_node = ll.head
        previous_node = current_node
        item = 9
        find = False
        while current_node is not None:

            if current_node.data == item:
                current_node = current_node.next
                previous_node.next = current_node
                find = True

            previous_node = current_node
            current_node = current_node.next

        assert (find == False)
Пример #14
0
 def test_init_array(self):
     ll = Linked_list(array)
     assert (ll.head.data == 1)
     assert (ll.head.next.data == 2)
     assert (ll.tail.data == 6)
Пример #15
0
 def push(self, value):
     Linked_list.add_to_tail(self, value)
Пример #16
0
    def pop(self):
        if self.size == 0:
            return None

        return_value = Linked_list.remove_from_head(self)
        return return_value
Пример #17
0
	def pop_min (self):
		return Linked_list.remove_from_head(self)
Пример #18
0
	def pop_max (self):
		return Linked_list.remove_from_tail(self)
Пример #19
0
 def __init__(self):
    self.list = Linked_list()
    self.size = 0
            a_ref = a_curr.next
        if b_curr:
            b_ref = b_curr.next
        if a_curr:
            a_curr.next = b_curr
        if b_curr:
            b_curr.next = a_ref

        a_curr = a_ref
        b_curr = b_ref

    return a_curr


if __name__ == "__main__":
    lst1 = Linked_list()
    lst2 = Linked_list()
    lst3 = Linked_list()
    lst1.insert(1)
    lst1.append(2)
    lst1.append(3)
    lst1.append(2)
    lst1.append(1)
    lst2.insert(2)
    lst2.append(4)
    lst2.append(6)
    lst2.append(8)
    lst2.append(10)
    lst3.insert(420)
    lst3.append(69)
    print(lst1.to_string())
Пример #21
0
 def __init__(self):
     Linked_list.__init__(self)
from linked_list import Node, Linked_list


def is_cycle(node):
    if not node or not node.next:
        return False

    slow, fast = node

    while slow:
        if fast.next or fast.next.next:
            return False

        if slow == fast:
            return True


if __name__ == "__main__":
    linked_list = Linked_list()
    print(linked_list)
Пример #23
0
from linked_list import Linked_list

linked_list_1 = Linked_list()
linked_list_2 = Linked_list()
linked_list_3 = Linked_list()
linked_list_4 = Linked_list()

elements_1 = [3, 2, 4, 35, 6, 65, 6, 4, 3, 21]
elements_2 = [6, 32, 4, 9, 6, 1, 11, 21, 1]

for i in elements_1:
    linked_list_1.append(i)

for i in elements_2:
    linked_list_2.append(i)

print(linked_list_1.union(linked_list_2))
print(linked_list_1.intersection(linked_list_2))

elements_3 = [3, 2, 4, 35, 6, 65, 6, 4, 3, 23]
elements_4 = [1, 7, 8, 9, 11, 21, 1]

for i in elements_3:
    linked_list_3.append(i)

for i in elements_4:
    linked_list_4.append(i)

print(linked_list_3.union(linked_list_4))
print(linked_list_3.intersection(linked_list_4))
Пример #24
0
def linked_list():
    return Linked_list()
Пример #25
0
from linked_list import Linked_list

link_list1 = Linked_list()
link_list1.append(0)
link_list1.append(1)
link_list1.append(2)
link_list1.append(3)
link_list1.append(4)
link_list1.append(5)

item = link_list1[3]

print(item)
print(link_list1)

link_list1.delete(3)
print(link_list1)

link_list1.delete(0)
print(link_list1)

link_list1.append(6)
print(link_list1)

link_list1.prepend(0)
print(link_list1)
 def __init__(self, init_size=8):
     """Initialize this hash table with the given initial size."""
     # Create a new list (used as fixed-size array) of empty linked lists
     self.buckets = [Linked_list() for _ in range(init_size)]
Пример #27
0
Search.verify(resulte)
print(f"time of monitor: {(lastTime-StartTime)}")

#--------------------// chack if the List is sorted or not  \\--------------------
print("\n--------// chack if the List is sorted or not \\\\-------")
# print(alist)
# print(Sort.verify_sort(alist))
StartTime = time.perf_counter_ns()
sorted_list = Sort.merge_sort(alist)
EndTime = time.perf_counter_ns()
print(f"sorted_list {sorted_list} \ntime of monitor: {(EndTime-StartTime)}ns ")
# print(Sort.verify_sort(sorted_list))

#------------------// sort lkinked list \\-------------------------------------
print("\n--------// sort lkinked list \\\\-------")
l = Linked_list()
l.add(20)
l.add(30)
l.add(10)
l.add(5)
l.add(25)
l.add(40)
print(l)
sorted_link_list = Linked_list_sortition.sort_linked_list(l)
print(sorted_link_list)

#------------------// bogo sort \\-------------------------------------
print("\n-----------//bogo sort \\\\ ----------------")
alist1 = load_numbers("Data_Structures/Files/numers.txt")
StartTime = time.perf_counter_ns()
sorted_bogo = Sort.bogo_sort(alist1)