2019-08-06 18:14:23 +08:00
|
|
|
# Python code to demonstrate working of
|
2018-10-19 20:48:28 +08:00
|
|
|
# extend(), extendleft(), rotate(), reverse()
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# importing "collections" for deque operations
|
|
|
|
import collections
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# initializing deque
|
2019-10-05 13:14:13 +08:00
|
|
|
de = collections.deque([1, 2, 3])
|
2019-08-06 18:14:23 +08:00
|
|
|
|
|
|
|
# using extend() to add numbers to right end
|
2018-10-19 20:48:28 +08:00
|
|
|
# adds 4,5,6 to right end
|
2019-10-05 13:14:13 +08:00
|
|
|
de.extend([4, 5, 6])
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# printing modified deque
|
2019-08-06 18:14:23 +08:00
|
|
|
print("The deque after extending deque at end is : ")
|
|
|
|
print(de)
|
|
|
|
|
|
|
|
# using extendleft() to add numbers to left end
|
2018-10-19 20:48:28 +08:00
|
|
|
# adds 7,8,9 to right end
|
2019-10-05 13:14:13 +08:00
|
|
|
de.extendleft([7, 8, 9])
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# printing modified deque
|
2019-08-06 18:14:23 +08:00
|
|
|
print("The deque after extending deque at beginning is : ")
|
|
|
|
print(de)
|
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# using rotate() to rotate the deque
|
|
|
|
# rotates by 3 to left
|
|
|
|
de.rotate(-3)
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# printing modified deque
|
2019-08-06 18:14:23 +08:00
|
|
|
print("The deque after rotating deque is : ")
|
|
|
|
print(de)
|
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# using reverse() to reverse the deque
|
|
|
|
de.reverse()
|
2019-08-06 18:14:23 +08:00
|
|
|
|
2018-10-19 20:48:28 +08:00
|
|
|
# printing modified deque
|
2019-08-06 18:14:23 +08:00
|
|
|
print("The deque after reversing deque is : ")
|
|
|
|
print(de)
|
2019-09-30 22:27:41 +08:00
|
|
|
|
|
|
|
# get right-end value and eliminate
|
|
|
|
startValue = de.pop()
|
|
|
|
|
|
|
|
print("The deque after popping value at end is : ")
|
|
|
|
print(de)
|
|
|
|
|
|
|
|
# get left-end value and eliminate
|
|
|
|
endValue = de.popleft()
|
|
|
|
|
|
|
|
print("The deque after popping value at start is : ")
|
|
|
|
print(de)
|
|
|
|
|
|
|
|
# eliminate element searched by value
|
|
|
|
de.remove(5)
|
|
|
|
|
|
|
|
print("The deque after eliminating element searched by value : ")
|
|
|
|
print(de)
|