Computer >> Máy Tính >  >> Lập trình >> Python

Chương trình Python để triển khai hàng đợi bằng cách sử dụng ngăn xếp

Khi bắt buộc phải triển khai hàng đợi bằng cách sử dụng ngăn xếp, một lớp hàng đợi có thể được định nghĩa, nơi có thể xác định hai cá thể ngăn xếp. Các hoạt động khác nhau có thể được thực hiện trên hàng đợi được định nghĩa là các phương thức trong lớp này.

Dưới đây là một minh chứng về điều tương tự -

Ví dụ

class Queue_structure:
   def __init__(self):
      self.in_val = Stack_structure()
      self.out_val = Stack_structure()

   def check_empty(self):
      return (self.in_val.check_empty() and self.out_val.check_empty())

   def enqueue_operation(self, data):
      self.in_val.push_operation(data)

   def dequeue_operation(self):
      if self.out_val.check_empty():
         while not self.in_val.check_empty():
            deleted_val = self.in_val.pop_operation()
            self.out_val.push_operation(deleted_val)
      return self.out_val.pop_operation()

class Stack_structure:
   def __init__(self):
      self.items = []

   def check_empty(self):
      return self.items == []

   def push_operation(self, data):
      self.items.append(data)

   def pop_operation(self):
      return self.items.pop()

my_instance = Queue_structure()

while True:
   print('enqueue <value>')
   print('dequeue')
   print('quit')
   my_input = input('What operation would you like to perform ?').split()

   operation = my_input[0].strip().lower()
   if operation == 'enqueue':
      my_instance.enqueue_operation(int(my_input[1]))
   elif operation == 'dequeue':
      if my_instance.check_empty():
         print('The queue is empty')
      else:
         deleted_elem = my_instance.dequeue_operation()
         print('The deleted element is : ', int(deleted_elem))
   elif operation == 'quit':
      break

Đầu ra

enqueue <value>
dequeue
quit
What operation would you like to perform ?enqueue 45
enqueue <value>
dequeue
quit
What operation would you like to perform ?enqueue 23
enqueue <value>
dequeue
quit
What operation would you like to perform ?enqueue 78
enqueue <value>
dequeue
quit
What operation would you like to perform ?dequeue
The deleted element is : 45
enqueue <value>
dequeue
quit
What operation would you like to perform ?quit

Giải thích

  • Một ‘Queue_ architecture’ được định nghĩa, xác định hai trường hợp của Stack.

  • Nó có một phương thức tên là ‘check_empty’ để kiểm tra xem hàng đợi có trống không.

  • Một phương thức khác có tên là ‘enqueue_operation’ được định nghĩa, giúp thêm các phần tử vào hàng đợi.

  • Một phương thức khác có tên là ‘dequeue_operation’ được xác định, phương thức này sẽ xóa một phần tử khỏi hàng đợi.

  • Một lớp ‘Stack_ Structure’ khác được tạo.

  • Nó khởi tạo một danh sách trống.

  • Nó có một phương thức có tên là ‘check_empty’ để kiểm tra xem ngăn xếp có trống không.

  • Một phương thức khác có tên là ‘push_operation’ được xác định, giúp thêm các phần tử vào hàng đợi.

  • Một phương thức khác có tên là ‘pop_operation’ được xác định, phương thức này sẽ xóa một phần tử khỏi hàng đợi.

  • Phiên bản ‘Queue_ architects’ được tạo.

  • Ba tùy chọn là cho trước, hủy bỏ và bỏ.

  • Dựa trên tùy chọn do người dùng đưa ra, các hoạt động được thực hiện và kết quả phù hợp được hiển thị trên bảng điều khiển.