Amazing-Python-Scripts
91 строка · 2.0 Кб
1# -*- coding: utf-8 -*-
2"""
3Author: Himanshu Agarwal
4Github: himanshu-03 (http://github.com/himanshu-03)
5LinkedIn: agarwal-himanshu (https://linkedin.com/in/agarwal-himanshu)
6
7# Menu Driven Code for Linear Queue
8"""
9
10
11class Q:12queue = []13MaxSize = 014front = 015rear = 016
17def createQueue(self, size):18Q.front = 019Q.rear = -120Q.MaxSize = size21for i in range(0, Q.MaxSize):22Q.queue.append(0)23print('\nQueue created of size: ', len(Q.queue))24print(Q.queue)25
26def enqueue(self, e):27Q.rear += 128Q.queue[Q.rear] = e29print(e, 'enqueued in Queue at position:', Q.rear+1)30print('')31
32def dequeue(self):33temp = Q.queue[Q.front]34Q.front += 135print(temp, 'dequeued from Queue at position:', Q.front)36print('')37
38def isFull(self):39if Q.rear == Q.MaxSize - 1:40return True41else:42return False43
44def isEmpty(self):45if Q.front > Q.rear:46return True47else:48return False49
50def printQueue(self):51print('Position', '\tData')52for i in range(Q.front, Q.rear+1):53print(i+1, '\t\t', Q.queue[i])54
55
56# Main Code:
57
58o = Q()59o.createQueue(int(input('Enter size of the queue: ')))60
61while True:62print('------------')63print('1.Enqueue\n2.Dequeue\n3.Print\n0.Exit')64print('------------')65
66ch = int(input('\nEnter your choice: '))67
68if ch == 1:69if o.isFull() != True:70data = int(input('\nEnter data to be enqueued: '))71o.enqueue(data)72else:73print('\nQueue is full..\n')74
75elif ch == 2:76if o.isEmpty() != True:77o.dequeue()78else:79print('\nQueue is empty..\n')80
81elif ch == 3:82if o.isEmpty() != True:83o.printQueue()84else:85print('\nQueue is empty..\n')86
87elif ch == 0:88break89
90else:91print('\nWrong Input..\nEnter the correct choice..!!\n')92