Python Queue
- Categories Data Structures, Python
In this post we will cover Queue in Python. You can watch our videos on Queue in Python – Click Here
Q.1 What are queues? What all operations can be performed on queues?
Ans.
- Queues are the data structures where data is entered into the queue at one end — the rear end and deleted from the other end – the front end, i.e., these follow First In-First Out (FIFO) principle.
- Following basic operations can be performed on queues :
- Enqueue i.e., Insertion of an element in the queue
- Dequeue i.e., Deletion of an element from the queue
- Peek i.e., viewing frontmost element without removing it
- Display or view all the elements in the queue.
Q. 2 Define Queue
Ans. Queue is a first-in, first-out(FIFO) data structure. i.e the element added first to the queue will be the one to be removed first. Elements are always added to the rear of the queue and removed from the front of the queue
Q. 3 What are the two major queue operations?
Ans. Addition of element is known as Insert operation, also known as enqueuing, It is done using rear terminal position i.e. tail end. Removal of element is known as Delete operation, also known as dequeuing
Q. 4 What are the different type of queue?
Ans. Queue can be of four types:
- Simple Queue
- Circular Queue
- Priority Queue
- De-queue ( Double Ended Queue)
Q. 5 Enlist some applications of queues.
Ans. Applications of queues include the situations where FIFO property is exploited. Some common applications of queues include :
- Sharing of one resource among multiple users or seekers such as shared printer among multiple computers; Call center executive’s response among waiting callers etc.
- Airport authorities make use of queues in situation of sharing a single runway of airport for both landing and take-off of flights
- CPU uses queues to implement round-robin scheduling among waiting processes
- Queues are used in many computer algorithms also
Q. 6 What is deque?
Ans. Deques or double ended queues are refined queues in which elements can be removed or added at either end but not in the middle.
Q.7 What are Circular Queues?
Ans.
- Circular Queues are the queues implemented in circular form rather than a straight line.
- These are used in programming languages that allow the use of fixed-size linear structure(such as arrays of c/c++ etc )as
- Unutilized spaces problem is overcome by circular Queues
Q. 8 Difference between Input and Output restricted Deque
Ans.
Input restricted deque | Output restricted deque |
Input restricted deque is one which allows insertions at only one end but deletions are allowed at both ends | Output restricted deque is one which allows deletions at only one end of the list but insertions are allows at both ends |
Q. 9 Difference between Dequeue and Enqueue
Ans.
Dequeue | Enqueue |
Removes the item from the front of the queue(at front-end) and returns it | Adds item at the back of the queue(at rear end) |
Q.10 What is the single queue, deque and circular queues?
Ans.
- Queues can be used in several forms and ways, depending upon the requirements of the program. Two popular variations of queues are circular queues and dequeues (double-ended queues).
- Circular Queues are the queues implemented in circle form rather than a straight line. Circular queues overcome the problem of unutilised space in linear queues implemented as arrays (if shifting is not done to avoid the problem of unutilised space, but shifting as such makes it complex to implement).
- Deque (double-ended queues) are the refined queues in which elements can be added or removed at either end but not in the middle.
- There are two variations of a deque – an input restricted deque and an output restricted deque. An input restricted deque is a deque which allows insertions at only one end but allows deletions at both ends of the list. An output restricted deque is a deque which allows deletions at only one end of the list but allows insertions at both ends of the list.
Q.11 Write a program to perform insert and delete on a Queue containing Members details as given in the following definition of itemnode:
MemberNo integer
MemberName String
Age integer
queue: implemented as a list
front: integer having position of first (frontmost) element in queue
rear: integer having position of last element in queue
Ans.
def isEmpty(Qu): if Qu==[]: return True else: return False
def Enqueue(Qu, item) : Qu.append(item) if len(Qu)==1 : front=rear=0 else: rear=len(Qu)-1 print (Qu) def Dequeue(Qu): if isEmpty(Qu): return "Underflow" else: item =Qu.pop(0) if len(Qu)==0: front=rear=None return item print (Qu) def Display(Qu): if isEmpty(Qu): print("Queue Empty!") elif len(Qu)==1: print(Qu[0],"<==front,rear") else: front= 0 print(len(Qu)) rear=len(Qu)-1 print(Qu[front],"<-front") for a in range(1,rear): print (Qu[a]) print(Qu[rear],"<-rear") queue=[] front =None while True: print("Queue Operations") print("1.Enqueue") print("2.Dequeue") print("3.Display queue") print("4.Exit") ch=int(input("Enter your choice(1-4)")) if ch==1: print("for the new member,enter details below:") memberNo=int(input("Enter member no:")) memberName= input("Enter member name:") age=int(input("Enter member's age")) item =[memberNo,memberName,age] Enqueue(queue,item) elif ch==2: item=Dequeue(queue) if item=="underflow": print("Queue is empty!") else:
print("Dequeue-ed item is",item) elif ch==3: Display(queue) elif ch ==4: break else:
print("Invaild choice!")
Ans.
Output
Queue Operations
1.Enqueue
2.Dequeue
3.Display queue
4.Exit
Enter your choice(1-4)1
for the new member,enter details below:
Enter member no:3
Enter member name:Rita
Enter member’s age10
[[3, ‘Rita’, 10]]
Queue Operations
1.Enqueue
2.Dequeue
3.Display queue
4.Exit
Enter your choice(1-4)1
for the new member,enter details below:
Enter member no:6
Enter member name:sita
Enter member’s age12
[[3, ‘Rita’, 10], [6, ‘sita’, 12]]
Queue Operations
1.Enqueue
2.Dequeue
3.Display queue
4.Exit
Enter your choice(1-4)3
2
[3, ‘Rita’, 10] <-front
[6, ‘sita’, 12] <-rear
Queue Operations
1.Enqueue
2.Dequeue
3.Display queue
4.Exit
Enter your choice(1-4)2
Dequeue-ed item is [3, ‘Rita’, 10]
Queue Operations
1.Enqueue
2.Dequeue
3.Display queue
4.Exit
Enter your choice(1-4)4