File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Copyright (c) Oct 22, 2018 CareerMonk Publications and others.
2
+
3
+ # Creation Date : 2014-01-10 06:15:46
4
+ # Last modification : 2008-10-31
5
+ # by : Narasimha Karumanchi
6
+ # Book Title : Data Structures And Algorithmic Thinking With Python
7
+ # Warranty : This software is provided "as is" without any
8
+ # warranty; without even the implied warranty of
9
+ # merchantability or fitness for a particular purpose.
10
+
11
+ import random
12
+
13
+ class Stack :
14
+ def __init__ (self , C = 5 ):
15
+ self .C = C
16
+ self .array = []
17
+ def size (self ):
18
+ return len (self .array )
19
+ def isEmpty (self ):
20
+ return len (self .array ) == 0
21
+ def isFull (self ):
22
+ return len (self .array ) == self .C
23
+ def peek (self ):
24
+ if self .isEmpty ():
25
+ return None
26
+ return self .array [self .size ()- 1 ]
27
+ def pop (self ):
28
+ if self .isEmpty ():
29
+ print "Underflow"
30
+ return None
31
+ data = self .array .pop ()
32
+ return data
33
+ def push (self , data ):
34
+ if self .isFull ():
35
+ print "Overflow"
36
+ return
37
+ self .array .append (data )
38
+
39
+ class Queue :
40
+ def __init__ (self ):
41
+ self .s1 = Stack ()
42
+ self .s2 = Stack ()
43
+ def enqueue (self , data ):
44
+ self .s1 .push (data )
45
+
46
+ def dequeue (self ):
47
+ if (not self .s2 .isEmpty ()):
48
+ return self .s2 .pop ()
49
+ while (not self .s1 .isEmpty ()):
50
+ self .s2 .push (self .s1 .pop ())
51
+ return self .s2 .pop ()
52
+
53
+ q = Queue ()
54
+ q .enqueue (1 )
55
+ q .enqueue (6 )
56
+ q .enqueue (8 )
57
+ q .enqueue (10 )
58
+ q .enqueue (16 )
59
+ print q .dequeue ()
60
+ print q .dequeue ()
61
+ print q .dequeue ()
62
+ print q .dequeue ()
63
+ print q .dequeue ()
You can’t perform that action at this time.
0 commit comments