Amazing-Python-Scripts

Форк
0
/
Menu_Driven_Code_for_Linear_Queue.py 
91 строка · 2.0 Кб
1
# -*- coding: utf-8 -*-
2
"""
3
Author: Himanshu Agarwal
4
Github: himanshu-03 (http://github.com/himanshu-03)
5
LinkedIn: agarwal-himanshu (https://linkedin.com/in/agarwal-himanshu)
6

7
# Menu Driven Code for Linear Queue
8
"""
9

10

11
class Q:
12
    queue = []
13
    MaxSize = 0
14
    front = 0
15
    rear = 0
16

17
    def createQueue(self, size):
18
        Q.front = 0
19
        Q.rear = -1
20
        Q.MaxSize = size
21
        for i in range(0, Q.MaxSize):
22
            Q.queue.append(0)
23
        print('\nQueue created of size: ', len(Q.queue))
24
        print(Q.queue)
25

26
    def enqueue(self, e):
27
        Q.rear += 1
28
        Q.queue[Q.rear] = e
29
        print(e, 'enqueued in Queue at position:', Q.rear+1)
30
        print('')
31

32
    def dequeue(self):
33
        temp = Q.queue[Q.front]
34
        Q.front += 1
35
        print(temp, 'dequeued from Queue at position:', Q.front)
36
        print('')
37

38
    def isFull(self):
39
        if Q.rear == Q.MaxSize - 1:
40
            return True
41
        else:
42
            return False
43

44
    def isEmpty(self):
45
        if Q.front > Q.rear:
46
            return True
47
        else:
48
            return False
49

50
    def printQueue(self):
51
        print('Position', '\tData')
52
        for i in range(Q.front, Q.rear+1):
53
            print(i+1, '\t\t', Q.queue[i])
54

55

56
# Main Code:
57

58
o = Q()
59
o.createQueue(int(input('Enter size of the queue: ')))
60

61
while True:
62
    print('------------')
63
    print('1.Enqueue\n2.Dequeue\n3.Print\n0.Exit')
64
    print('------------')
65

66
    ch = int(input('\nEnter your choice: '))
67

68
    if ch == 1:
69
        if o.isFull() != True:
70
            data = int(input('\nEnter data to be enqueued: '))
71
            o.enqueue(data)
72
        else:
73
            print('\nQueue is full..\n')
74

75
    elif ch == 2:
76
        if o.isEmpty() != True:
77
            o.dequeue()
78
        else:
79
            print('\nQueue is empty..\n')
80

81
    elif ch == 3:
82
        if o.isEmpty() != True:
83
            o.printQueue()
84
        else:
85
            print('\nQueue is empty..\n')
86

87
    elif ch == 0:
88
        break
89

90
    else:
91
        print('\nWrong Input..\nEnter the correct choice..!!\n')
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.