QUEUE
QUEUE
================================================================================
- A Queue is defined as a linear data structure that is open at both ends.
- The operations are performed in First In First Out (FIFO) order.
- A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first serve).
- Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue(sometimes, head of the queue)
- Similarly, the position of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue
- Queue Operations:
- enqueue: Add a new element to the rear of queue. It requires element to be added and returns nothing.
- dequeue: Removes the elements from the front of queue. It does not require any parameters and returns the deleted item.
- isEmpty: Check the whether the queue is empty or not. It does not require any parameter and returns a Boolean value.
- isFull: Check the whether the queue is full or not. It does not require any parameter and returns a Boolean value.
- Types Of Queues:
- Linear Queue
- Circular Queue
- Priority Queue
- Double Ended Queue (deque)
- enqueue: Add a new element to the rear of queue. It requires element to be added and returns nothing.
- dequeue: Removes the elements from the front of queue. It does not require any parameters and returns the deleted item.
- isEmpty: Check the whether the queue is empty or not. It does not require any parameter and returns a Boolean value.
- isFull: Check the whether the queue is full or not. It does not require any parameter and returns a Boolean value.
- Linear Queue
- Circular Queue
- Priority Queue
- Double Ended Queue (deque)
- Linear Queue:
- It is generally referred as Queue
- No proper utilization of memory
- Operations of Linear Queue:
- Algorithm for Insertion:
- begin
- if (rear == sizeof(queue)-1)
- return "Queue is full"
- end if
- else
- increment rear
- queue[rear] = assign value
- increment front
- end else
- end
- Flow Chart for Insertion:
- It is generally referred as Queue
- No proper utilization of memory
- Operations of Linear Queue:
- Algorithm for Insertion:
- begin
- if (rear == sizeof(queue)-1)
- return "Queue is full"
- end if
- else
- increment rear
- queue[rear] = assign value
- increment front
- end else
- end
- Algorithm for Insertion:
- begin
- if (rear == sizeof(queue)-1)
- return "Queue is full"
- end if
- else
- increment rear
- queue[rear] = assign value
- increment front
- end else
- end
- Flow Chart for Insertion:
- Flow Chart for Insertion:
- Algorithm for Deletion:
- begin
- if (front == -1)
- return "Queue is empty"
- end if
- else
- store value of queue[front]
- increment front
- if (front == rear)
- set front = -1 and rear = -1
- end if
- else
- increment front
- end else
- end else
- end
- Flow Chart for Deletion:
- Algorithm for isFull:
- begin
- if (rear == sizeof(queue)-1)
- return true
- end if
- else
- return false
- end else
- end
- Flow Chart for isFull:
- Algorithm for isEmpty:
- begin
- if (front == -1)
- return true
- end if
- else
- return false
- end else
- end
- Flow Chart for isEmpty:
- Circular Queue:
- Proper utilization of memory
- Increment front and rear in circular fashion i.e. 0, 1, 2, ...... , Size-1, 0, 1, ....
- Operations of Circular Queue:
- Algorithm for Insertion:
- begin
- if ((front == -1 && rear == sizeof(queue)-1) || (rear== front&& rear!= -1) )
- return "Queue is full"
- end if
- else
- if (rear == sizeof(queue) -1)
- rear = 0
- end if
- else
- increment rear
- end else
- queue[rear] = assign value
- end else
- end
- Flow Chart for Insertion:
- Algorithm for Deletion:
- begin
- if (rear == -1 && front == -1)
- return "Queue is empty"
- end if
- else
- if(front == sizeof(queue)-1)
- store value of queue[0]
- end if
- else
- store value of queue[front++]
- end else
- if (front == sizeof(queue)-1)
- front = 0
- end if
- else
- increment front
- end if
- if (front == rear)
- set front = -1 and rear = -1
- end if
- end else
- end
- Flow Chart for Deletion:
- Proper utilization of memory
- Increment front and rear in circular fashion i.e. 0, 1, 2, ...... , Size-1, 0, 1, ....
- Operations of Circular Queue:
- Algorithm for Insertion:
- begin
- if ((front == -1 && rear == sizeof(queue)-1) || (rear== front&& rear!= -1) )
- return "Queue is full"
- end if
- else
- if (rear == sizeof(queue) -1)
- rear = 0
- end if
- else
- increment rear
- end else
- queue[rear] = assign value
- end else
- end
- Flow Chart for Insertion:
- Algorithm for Deletion:
- begin
- if (rear == -1 && front == -1)
- return "Queue is empty"
- end if
- else
- if(front == sizeof(queue)-1)
- store value of queue[0]
- end if
- else
- store value of queue[front++]
- end else
- if (front == sizeof(queue)-1)
- front = 0
- end if
- else
- increment front
- end if
- if (front == rear)
- set front = -1 and rear = -1
- end if
- end else
- end
- Flow Chart for Deletion:
- Algorithm for Insertion:
- begin
- if ((front == -1 && rear == sizeof(queue)-1) || (rear== front&& rear!= -1) )
- return "Queue is full"
- end if
- else
- if (rear == sizeof(queue) -1)
- rear = 0
- end if
- else
- increment rear
- end else
- queue[rear] = assign value
- end else
- end
- Flow Chart for Insertion:
- Algorithm for Deletion:
- begin
- if (rear == -1 && front == -1)
- return "Queue is empty"
- end if
- else
- if(front == sizeof(queue)-1)
- store value of queue[0]
- end if
- else
- store value of queue[front++]
- end else
- if (front == sizeof(queue)-1)
- front = 0
- end if
- else
- increment front
- end if
- if (front == rear)
- set front = -1 and rear = -1
- end if
- end else
- end
- begin
- if (rear == -1 && front == -1)
- return "Queue is empty"
- end if
- else
- if(front == sizeof(queue)-1)
- store value of queue[0]
- end if
- else
- store value of queue[front++]
- end else
- if (front == sizeof(queue)-1)
- front = 0
- end if
- else
- increment front
- end if
- if (front == rear)
- set front = -1 and rear = -1
- end if
- end else
- end
- Flow Chart for Deletion:
- Algorithm for isFull:
- begin
- if ((front == -1 && rear == sizeof(queue)-1) || (rear== front&& rear!= -1) )
- return true
- end if
- else
- return false
- end else
- end
- Flow Chart for isFull:
- Algorithm for isEmpty():
- begin
- if (rear == -1 && front == -1)
- return true
- end if
- else
- return false
- end else
- end
- Flow Chart for isEmpty():
- Priority Queue:
- Each element is associated with some priority
- Element with highest priority is popped out first
- No FIFO behavior (First In First Out)
- Typically it is implemented as sorted linked list
- While insertion element is added at appropriate position as per its priority
- So insertion time complexity will be 0(1)
- While deleting first element is deleted, as it is element with highest priority.
- Each element is associated with some priority
- Element with highest priority is popped out first
- No FIFO behavior (First In First Out)
- Typically it is implemented as sorted linked list
- While insertion element is added at appropriate position as per its priority
- So insertion time complexity will be 0(1)
- While deleting first element is deleted, as it is element with highest priority.
- Double Ended Queue (deque):
- Can push/pop elements from both sides i.e. front as well as rear
- Usually implemented as doubly linked list with head and tail
- Algorithm for Insertion using rear:
- begin
- if (rear == sizeof(queue)-1)
- return "Queue is full"
- end if
- else
- increment rear
- queue[rear] = assign value
- end else
- if (front == -1)
- set front = 0
- end if
- end
- Flow Chart for Insertion using rear :
- Can push/pop elements from both sides i.e. front as well as rear
- Usually implemented as doubly linked list with head and tail
- Algorithm for Insertion using rear:
- begin
- if (rear == sizeof(queue)-1)
- return "Queue is full"
- end if
- else
- increment rear
- queue[rear] = assign value
- end else
- if (front == -1)
- set front = 0
- end if
- end
- begin
- if (rear == sizeof(queue)-1)
- return "Queue is full"
- end if
- else
- increment rear
- queue[rear] = assign value
- end else
- if (front == -1)
- set front = 0
- end if
- end
- Flow Chart for Insertion using rear :
- Algorithm for Insertion using front:
- begin
- if (front == -1)
- return "Queue is full"
- end if
- else
- decrement front
- queue[front] = assign value
- end else
- if (rear == sizeof(queue)
- set rear = sizeof(queue)-1
- end if
- end
- Flow Chart for Insertion using front:
- Algorithm for Deletion using front:
- begin
- if (front == -1)
- return "Queue is empty"
- end if
- else
- store value of queue[front]
- if (front == rear)
- set front = -1 and rear = -1
- end if
- else
- increment front
- end else
- end else
- end
- Flow Chart Deletion using front:
- Algorithm for Deletion using rear:
- begin
- if (rear == sizeof(queue))
- return "Queue is empty"
- end if
- else
- store value of queue[rear]
- if (front == rear)
- set front = sizeof(queue) and rear = sizeof(queue)
- end if
- else
- decrement rear
- end else
- end else
- end
- Flow chart for Deletion using rear:
- Algorithm for isFull using rear:
- begin
- if (rear == sizeof(queue)-1)
- return true
- end if
- else
- return false
- end else
- end
- Flow Chart for isFull using rear:
- Algorithm for isFull using front:
- begin
- if (front== -1)
- return true
- end if
- else
- return false
- end else
- end
- Flow Chart for isFull using front:
Comments
Post a Comment