Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
2.8 kB
1
Indexable
Never
class MyCircularQueue(object):

    def __init__(self, k):
      self.length = k
      self.queue = [0] * k
      self.q_start = 0 
      self.q_tail = -1
      self.not_full = True
      self.empty = True
      self.size = 0

    def enQueue(self, value):
      if self.isFull():
        return False

      self.q_tail = (self.q_tail + 1) % self.length
      self.queue[self.q_tail] = value
      self.size += 1
      return True
        

    def deQueue(self):
      if self.isEmpty():
        return False
     
      self.q_start = (self.q_start + 1) % self.length
      self.size -= 1
      return True
        
        
        

    def Front(self):
      if self.isEmpty():
        return -1
      return self.queue[self.q_start]
        

    def Rear(self):
      if self.isEmpty():
        return -1
      return self.queue[self.q_tail]
      

    def isEmpty(self):
      return self.size == 0
        

    def isFull(self):
      return self.size == self.length
        


# Your MyCircularQueue object will be instantiated and called as such:
["Front","deQueue","deQueue","deQueue"]

obj = MyCircularQueue(1)
print(obj.enQueue(1))
print(obj.enQueue(2))
print(obj.enQueue(3))
print(obj.queue)
print(obj.deQueue())
print(obj.enQueue(3))
print(obj.queue)


print()
# print(obj.enQueue(4))
# print(obj.isFull())
# print(obj.enQueue(4))

# Input
# ["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
# [[3], [1], [2], [3], [4], [], [], [], [4], []]
# Output
# [null, true, true, true, false, 3, true, true, true, 4]

# Explanation
# MyCircularQueue myCircularQueue = new MyCircularQueue(3);
# myCircularQueue.enQueue(1); // return True
# myCircularQueue.enQueue(2); // return True
# myCircularQueue.enQueue(3); // return True
# myCircularQueue.enQueue(4); // return False
# myCircularQueue.Rear();     // return 3
# myCircularQueue.isFull();   // return True
# myCircularQueue.deQueue();  // return True
# myCircularQueue.enQueue(4); // return True
# myCircularQueue.Rear();     // return 4


# //////////////////////////
from collections import deque

class Node:
    def __init__(self, value):
        pass

def BFS(root, target):
    pass

# أمثلة للاختبار:

# مثال 1: شجرة بسيطة

# إنشاء العقد
root = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)

# ربط العقد كأنها شجرة
# أكمل هنا

# اختبار البحث عن أقصر مسار من الجذر إلى node5
result = BFS(root, node5)  
print("Shortest path:", result)

# مثال 2: لا يوجد مسار

# إنشاء العقد
root = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)

# ربط العقد
# أكمل هنا

result = BFS(root, node5)  
print("Shortest path:", result)
Leave a Comment