File tree 4 files changed +187
-0
lines changed
4 files changed +187
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ """
3
+ Execution: python bag < input.txt
4
+
5
+ % more tobe.txt
6
+ to be or not to - be - - that - - - is
7
+
8
+ % python stack < tobe.txt
9
+ to be or not to - be - - that - - - is
10
+ """
11
+
12
+ from algs4 .utils .linklist import Node , LinkIterator
13
+
14
+
15
+ class Bag :
16
+
17
+ def __init__ (self ):
18
+ self .first = None
19
+ self .n = 0
20
+
21
+ def __str__ (self ):
22
+ return " " .join (i for i in self )
23
+
24
+ def __iter__ (self ):
25
+ return LinkIterator (self .first )
26
+
27
+ def size (self ):
28
+ return self .n
29
+
30
+ def is_empty (self ):
31
+ return self .first is None
32
+
33
+ def add (self , item ):
34
+ oldfirst = self .first
35
+ self .first = Node (item , oldfirst )
36
+ self .n += 1
37
+
38
+
39
+ if __name__ == '__main__' :
40
+ import sys
41
+ for line in sys .stdin :
42
+ bag = Bag ()
43
+ for item in line .split ():
44
+ bag .add (item )
45
+ print ("size of bag = " , bag .size ())
46
+ for i in bag :
47
+ print (i )
Original file line number Diff line number Diff line change
1
+ """
2
+ Execution: python queue < input.txt
3
+
4
+ % more tobe.txt
5
+ to be or not to - be - - that - - - is
6
+
7
+ % python queue < tobe.txt
8
+ to be or not to be (2 left on queue)
9
+ """
10
+
11
+ from algs4 .utils .linklist import Node , LinkIterator
12
+
13
+
14
+ class Queue :
15
+
16
+ def __init__ (self ):
17
+ self .first = None
18
+ self .last = None
19
+ self .n = 0
20
+
21
+ def __str__ (self ):
22
+ return " " .join (i for i in self )
23
+
24
+ def __iter__ (self ):
25
+ return LinkIterator (self .first )
26
+
27
+ def size (self ):
28
+ return self .n
29
+
30
+ def is_empty (self ):
31
+ return self .first is None
32
+
33
+ def enqueue (self , item ):
34
+ oldlast = self .last
35
+ self .last = Node (item , None )
36
+ if self .is_empty ():
37
+ self .first = self .last
38
+ else :
39
+ oldlast .next = self .last
40
+ self .n += 1
41
+
42
+ def dequeue (self ):
43
+ if self .is_empty ():
44
+ raise ValueError ("Queue empty" )
45
+ else :
46
+ item = self .first .item
47
+ self .first = self .first .next
48
+ if self .is_empty ():
49
+ self .last = None
50
+ self .n -= 1
51
+ return item
52
+
53
+
54
+ if __name__ == '__main__' :
55
+ import sys
56
+
57
+ for line in sys .stdin :
58
+ queue = Queue ()
59
+ for item in line .split ():
60
+ if item != "-" :
61
+ queue .enqueue (item )
62
+ elif not queue .is_empty ():
63
+ print (queue .dequeue () + " " , end = '' )
64
+ print ("(%d left on queue)" % queue .size ())
Original file line number Diff line number Diff line change
1
+ """
2
+ Execution: python stack < input.txt
3
+
4
+ % more tobe.txt
5
+ to be or not to - be - - that - - - is
6
+
7
+ % python stack < tobe.txt
8
+ to be not that or be (2 left on stack)
9
+ """
10
+
11
+ from algs4 .utils .linklist import Node , LinkIterator
12
+
13
+
14
+ class Stack :
15
+
16
+ def __init__ (self ):
17
+ self .first = None
18
+ self .n = 0
19
+
20
+ def __str__ (self ):
21
+ return " " .join (i for i in self )
22
+
23
+ def __iter__ (self ):
24
+ return LinkIterator (self .first )
25
+
26
+ def size (self ):
27
+ return self .n
28
+
29
+ def is_empty (self ):
30
+ return self .first is None
31
+
32
+ def push (self , item ):
33
+ oldfirst = self .first
34
+ self .first = Node (item , oldfirst )
35
+ self .n += 1
36
+
37
+ def pop (self ):
38
+ if self .is_empty ():
39
+ raise ValueError ("Stack underflow" )
40
+ else :
41
+ item = self .first .item
42
+ self .first = self .first .next
43
+ self .n -= 1
44
+ return item
45
+
46
+
47
+ if __name__ == '__main__' :
48
+ import sys
49
+
50
+ for line in sys .stdin :
51
+ stack = Stack ()
52
+ for item in line .split ():
53
+ if item != "-" :
54
+ stack .push (item )
55
+ elif not stack .is_empty ():
56
+ print (stack .pop () + " " , end = '' )
57
+ print ("(%d left on stack)" % stack .size ())
Original file line number Diff line number Diff line change
1
+ class Node :
2
+
3
+ def __init__ (self , item , nextItem ):
4
+ self .item = item
5
+ self .next = nextItem
6
+
7
+
8
+ class LinkIterator :
9
+
10
+ def __init__ (self , current ):
11
+ self .current = current
12
+
13
+ def __next__ (self ):
14
+ if self .current is None :
15
+ raise StopIteration ()
16
+ else :
17
+ item = self .current .item
18
+ self .current = self .current .next
19
+ return item
You can’t perform that action at this time.
0 commit comments