Docsity

Data Structures: Queues and Deques Implementations and Applications, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
1
CS261–DataStructures
Queues
Queues
• Linear data abstraction:
– Elements added only at ends and retain order inwhich they were
inserted
• Queue (FIFO):
– addLast
–removeFirst
–getFirst
• Queue applications (waitinginline):
– Process scheduling (operatingsystem)
– Print queue
– Breadth-first searchingand filling
Queue Implementations
• We’ve already seen one data structure that implement the
stack interface:
–LinkedList:Queue queue = new LinkedList();
•TheLinkedList class implements the operations in
constant time
• We will study others called:
–VectorQueue
–RingBufferQueue
–IndexedDeque
VectorQueue
•TheVector data type can’t provide constant removal
from the front as can the LinkedList
• However, Vector can insert and remove at an arbitrary
position:
– So, we can implement a Queue witha Vector:
public class VectorQueue extends Vector implements Queue {
public Object getFirst() { return elementAt(0); }
public void removeFirst() { removeElementAt(0); }
}
Circular, single-linked list (i.e., no previous links):
• Unlike LinkedList,links are not removed from the list when
elements are removed
• Instead,they remain in the list and are reused
• Used for buffer queues in operating systems
Ring Buffer Queue
head (firstFilled)
free (firstFree)
Ring Buffer Queue
public class RingBufferQueue implements Queue {
private int num; // Numberof nodes in queue.
private RingBufferNode head, // Head of list (first filled node).
free; // Next freenode.
RingBufferQueue() {
head = free = new RingBufferNode();
num = 0;
}
// Implementation of the Collection interface.
public boolean isEmpty() { return num == 0; }
public int size() { return num; }
// Implementation of the Queue interface.
public synchronized void addLast(Object obj) {
if (free.next == head) // Full, so create a node.
free.next = new RingBufferNode(free.next);
free.obj = obj; free = free.next;
num++;
}
...
pf3
pf4

Partial preview of the text

Download Data Structures: Queues and Deques Implementations and Applications and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 261 – Data Structures

Queues

Queues

• Linear data abstraction:

  • Elements added only at ends and retain order in which they were

inserted

• Queue (FIFO) :

  • addLast
  • removeFirst
  • getFirst

• Queue applications (waiting in line) :

  • Process scheduling (operating system)
  • Print queue
  • Breadth-first searching and filling

Queue Implementations

• We’ve already seen one data structure that implement the

stack interface:

  • LinkedList : Queue queue = new LinkedList();

• The LinkedList class implements the operations in

constant time

• We will study others called:

– VectorQueue

– RingBufferQueue

– IndexedDeque

VectorQueue

• The Vector data type can’t provide constant removal

from the front as can the LinkedList

• However, Vector can insert and remove at an arbitrary

position:

  • So, we can implement a Queue with a Vector :

public class VectorQueue extends Vector implements Queue { public Object getFirst() { return elementAt(0); } public void removeFirst() { removeElementAt(0); } }

Circular, single-linked list (i.e., no previous links) :

  • Unlike LinkedList , links are not removed from the list when

elements are removed

  • Instead, they remain in the list and are reused
  • Used for buffer queues in operating systems

Ring Buffer Queue

head ( firstFilled )

free ( firstFree )

Ring Buffer Queue

public class RingBufferQueue implements Queue { private int num; // Number of nodes in queue. private RingBufferNode head, // Head of list (first filled node). free; // Next free node. RingBufferQueue() { head = free = new RingBufferNode(); num = 0; } // Implementation of the Collection interface. public boolean isEmpty() { return num == 0; } public int size() { return num; }

// Implementation of the Queue interface. public synchronized void addLast(Object obj) { if (free.next == head) // Full, so create a node. free.next = new RingBufferNode(free.next); free.obj = obj; free = free.next; num++; } ...

Ring Buffer Queue

// Implementation of the Queue interface (cont.). public Object getFirst() { if (head == free) throw new NoSuchElementException; return head.obj; }

public void removeFirst() { if (head == free) throw new NoSuchElementException;

head.obj = null; head = head.next; num--; } } // End of RingBufferQueue class.

public class RingBufferNode { public Object obj; public RingBufferNode next;

public RingBufferNode() { next = this; } public RingBufferNode(RingBufferNode n) { next = n; } }

Ring Buffer Queue

• Time complexity:

  • Always O(1) for addLast , getFirst , removeFirst
  • Same as LinkedList but RingBufferQueue has less

memory overhead

  • Same also as IndexedDeque except that addLast not

always O(1) for IndexedDeque

• Applications:

  • Caterpillar game:
    • Queues for the caterpillar bodies
    • Queue for the pressed keys
  • Pastry factory simulation:
    • Queue for the conveyer belt (as a buffer)
    • Queue for the box contents

CS 261 – Data Structures

Deques

Deques

• A Deque can be thought of (and used) as a double-ended

queue and/or a double-ended stack

• Extends both Stack and Queue (multiple inheritance) :

interface Deque extends Stack, Queue { public void addFirst() }

  • Classes that implement Deque can be used whenever you need

either (or both) interfaces.

  • Often used for problems where you need to search a problem

space for a solution.

Deques: Backtracking

• Backtracking is a problem solving technique

• Useful when looking for a set of solutions or an optimal

solution

• Good problems to use backtracking on:

  • Allows a simple description of the current state (i.e., partial

solution)

  • Solvable by moving from state to state
  • Have multiple options for the next state

Deque: Maze Path Finding

• Find a path from start to finish in a maze:

  • Stack Æ Depth first
  • Queue Æ Breadth first
  • Deque interface allows us to try both

S

F

Search order (order in which

adjacent positions are visited)

IndexedDeque: Indexed Interface (cont.)

public synchronize void addElementAt(int idx, Object obj) { if (idx < 0 || idx > count) throw new ArrayIndexOutOfBoundsException(idx); setSize(count + 1); for (int i = count – 1; i > idx; i--) setElementAt(elementAt(i - 1), i); // Slide data up one spot. setElementAt(obj, idx); }

public synchronized void removeElementAt(int idx) { if (idx < 0 || idx >= count) throw new ArrayIndexoutOfBoundsException(idx); while (++idx < count) setElementAt(elementAt(idx), idx – 1); // Slide data down one spot. setElementAt(null, idx - 1); count--; }

IndexedDeque: Indexed Interface (cont.)

*public void setSize(int size) { int capacity = data.length; while (size > capacity) capacity = 2; ensureCapacity(capacity); count = size; }

public synchronize void ensureCapacity(int capacity) { if (capacity <= data.length) return; int i = first, idx = 0; Object [] old = data; data = new Object[capacity]; while (idx < count && i < data.length) data[idx++] = old[i++]; for (i = 0; idx < count; ) data[idx++] = old[i++]; first = 0; }

Why two loops?

IndexedDeque: Deque Interface

// Implementation of the Deque interface. public Object getLast() { return elementAt(count – 1); } public void addLast(Object obj) { addElementAt(obj,count); } public void removeLast() { removeElementAt(count – 1); } public Object getFirst() { return elementAt(0); }

public synchronized void addFirst(Object obj) { if (++count >= data.length) ensureCapacity(2 * data.length); if (--first < 0 ) first = data.length – 1; setElementAt(obj, 0); } public synchronized void removeFirst() { if (count == 0) throw new NoSuchElementException(); data[firstFilled] = null; if(++first == data.length) first = 0; count--; } } // End of IndexedQueue class.

IndexedDeque

  • Lower overhead when compared to LinkedList :
    • No (or little) memory overhead
    • No links to maintain
  • Used as a queue that stays roughly the same size:
    • IndexedQueue is slightly faster that LinkedList
  • Much fast than VectorQueue when adding or removing

the first element

  • When used as a stack: Slightly slower than Vector