Queues

From

(Difference between revisions)
Jump to: navigation, search
(Priority Queue)
(Deque)
Line 7: Line 7:
==Deque==
==Deque==
A deque is a double-ended queue. This means that we can add or remove items from either end. The operations we need are addToFront(), addToBack(), removeFront(), removeBack(), peekFront(), peekBack(), isFull(), sEmpty().
A deque is a double-ended queue. This means that we can add or remove items from either end. The operations we need are addToFront(), addToBack(), removeFront(), removeBack(), peekFront(), peekBack(), isFull(), sEmpty().
 +
<br/><br/>
==Priority Queue==
==Priority Queue==

Revision as of 20:20, 26 March 2009

A queue is another basic data structure that is normally homogeneous data. It is similar to a stack except it operates using a FIFO (First In, First Out) ordering. A queue is another way of expressing a line. The standard operations on a queue are enqueue(), dequeue(),
peek(), and isEmpty().

Similar to a stack, a queue can be implemented either by an array or a linked list. When implemented using an array, it is normally implemented as a circular list (circular array). Items are added at the rear and removed from the front—in the same manner as a list. And, just like a list, we need an isFull() operation when using an array.

When using a linked list, items are added to the rear and removed from the front, so we need both front and rear pointers. Also note that similar to a stack, the operations on a queue are constant.

Deque

A deque is a double-ended queue. This means that we can add or remove items from either end. The operations we need are addToFront(), addToBack(), removeFront(), removeBack(), peekFront(), peekBack(), isFull(), sEmpty().

Priority Queue

A priority queue is a standard queue, but the elements have a priority. When an item is entered into the queue, it moves up in the queue until it encounters an item of equal or greater priority. This is its position in the queue.

When implementing a priority queue, it is often implemented as a linked list. If I am adding items to the back of the list, and removing items from the head of the list, a priority queue is best implemented as a doubly linked list. In this case, we would do the following.

Adding an item to a priority queue that is a doubly linked list:

Let Temp be a node pointer that points to a new list node. Assume a list node has a field called priority.

If Rear is null, the list is empty. So, set Front and Rear of the head node to the value of Temp.

If Rear is not null, there is at least one node in the list.

If Temp.priority <= Rear.priority, then add Temp to the end of the list
    Set Rear.next to Temp.
    Set Temp.previous to Rear
    Set Rear to Temp
Else 
    let P be a node pointer
    let P = Rear
EndIf

While P is not null and Temp.priority > P.priority
    Let P = P.previous

If P is not null, then P has stopped at a node whose priority is greater or equal to that of
Temp. Temp is to be inserted after the node pointed to by P.
    Let Temp.previous = P
    Let Temp.next = P.next
    Let P.next.previous = Temp
    Let P.next = Temp


If P is null, then the node pointed to by Temp goes at the head of the list.
    Let Front.previous = Temp
    Let Temp.next = Front
    Let Front = Temp


Personal tools
MediaWiki Appliance - Powered by TurnKey Linux