Python/data_structures/queue/double_ended_queue.py

40 lines
882 B
Python
Raw Normal View History

# Python code to demonstrate working of
2018-10-19 12:48:28 +00:00
# extend(), extendleft(), rotate(), reverse()
2018-10-19 12:48:28 +00:00
# importing "collections" for deque operations
import collections
2018-10-19 12:48:28 +00:00
# initializing deque
de = collections.deque([1, 2, 3,])
# using extend() to add numbers to right end
2018-10-19 12:48:28 +00:00
# adds 4,5,6 to right end
de.extend([4,5,6])
2018-10-19 12:48:28 +00:00
# printing modified deque
print("The deque after extending deque at end is : ")
print(de)
# using extendleft() to add numbers to left end
2018-10-19 12:48:28 +00:00
# adds 7,8,9 to right end
de.extendleft([7,8,9])
2018-10-19 12:48:28 +00:00
# printing modified deque
print("The deque after extending deque at beginning is : ")
print(de)
2018-10-19 12:48:28 +00:00
# using rotate() to rotate the deque
# rotates by 3 to left
de.rotate(-3)
2018-10-19 12:48:28 +00:00
# printing modified deque
print("The deque after rotating deque is : ")
print(de)
2018-10-19 12:48:28 +00:00
# using reverse() to reverse the deque
de.reverse()
2018-10-19 12:48:28 +00:00
# printing modified deque
print("The deque after reversing deque is : ")
print(de)