File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ # Author: OMKAR PATHAK
2+
3+ class Queue (object ):
4+ def __init__ (self , limit = 10 ):
5+ self .queue = []
6+ self .front = None
7+ self .rear = None
8+ self .limit = limit
9+ self .size = 0
10+
11+ def __str__ (self ):
12+ return ' ' .join ([str (i ) for i in self .queue ])
13+
14+ # to check if queue is empty
15+ def isEmpty (self ):
16+ return self .size <= 0
17+
18+ # to add an element from the rear end of the queue
19+ def enqueue (self , data ):
20+ if self .size >= self .limit :
21+ return - 1 # queue overflow
22+ else :
23+ self .queue .append (data )
24+
25+ # assign the rear as size of the queue and front as 0
26+ if self .front is None :
27+ self .front = self .rear = 0
28+ else :
29+ self .rear = self .size
30+
31+ self .size += 1
32+
33+ # to pop an element from the front end of the queue
34+ def dequeue (self ):
35+ if self .isEmpty ():
36+ return - 1 # queue underflow
37+ else :
38+ self .queue .pop ()
39+ self .size -= 1
40+ if self .size == 0 :
41+ self .front = self .rear = 0
42+ else :
43+ self .rear = self .size - 1
44+
45+ def getSize (self ):
46+ return self .size
47+
48+ if __name__ == '__main__' :
49+ myQueue = Queue ()
50+ for i in range (10 ):
51+ myQueue .enqueue (i )
52+ print (myQueue )
53+ print ('Queue Size:' ,myQueue .getSize ())
54+ myQueue .dequeue ()
55+ print (myQueue )
56+ print ('Queue Size:' ,myQueue .getSize ())
You can’t perform that action at this time.
0 commit comments