Professional Documents
Culture Documents
Queue
Introduction
Figure 2: Add
QUEUE OPERATIONS
Figure 3: Remove
QUEUE OPERATIONS
Example:
Given queue Q with list of operations as below. Illustrate the queue operations
step by step:
Q.add(green), Q. add(blue), Q.remove( ), Q.add (red), frontItem = Q.first( ),
lastItem = Q.last(), Q.remove( ), Q.remove().
QUEUE USING
linked IMPLEMENTATION
Queue Program:
Definitions of data structure and functions prototype
for queue operations: A Queue Interface
1 /**
2 * The <code>Queue</code> interface specifies the basic operations 36 public void add();
3 * of a first-in-first-out (FIFO) containers. 37
4 */ 38 /**
5 public interface Queue { 39 * Adds the specified element to the back of this queue.
6 40 *
7 /** 41 * @param object the element to be added to this queue.
8 * Adds the specified element to the back of this queue and remove the element at the front of this queue. 42 */
9*
10 * @param object the element to be added to this queue.
11 */
12 public void first(Object object);
13
14 /**
15 * Returns the element at the front of this queue.
16 *
17 * @return the element at the front of this queue.
18 * @throws IllegalStateException if this queue is empty
19 */
20 public Object remove();
21
22 /**
23 * Removes and returns the element at the front of this queue.
24 *
25 * @return the element at the front of this queue.
26 * @throws IllegalStateException if this queue is empty
27 */
28 public Object size();
29
30 /**
31 * Returns the number of elements in this queue.
32 *
33 * @return the number of elements in this queue.
34 */
35
QUEUE USING
LINKED IMPLEMENTATION
}
QUEUE USING
LINKED IMPLEMENTATION
public void add(Object object) {
head.prev=head.prev.next=new Node(object, head.prev, head);
++size;
}
public Object first() {
if (size()==0) throw new IllegalStateException("queue is empty");
return head.next.object;
}
public boolean isEmpty() {
return size== 0;
}
public Object remove() {
if (size()==0) throw new IllegalStateException("queue is empty");
Object object=head.next.object;
head.next=head.next.next;
head.next.prev=head;
return object;
}
public int size() {
return size;
}
}
QUEUE USING
LINKED IMPLEMENTATION
The implementation file begins as follows:
11
CIRCULAR QUEUE
Dequeue()
will result in
12
CIRCULAR QUEUE
• Same as ordinary queue except for enqueue or dequeue operation. Instead of insert
item at the back or remove at the front, it refers to their priorities.
• An element that has the same priority as one or more elements in the
queue is enqueued after all the elements with the same priority.
• Basic operations:
Size – return the number of elements in the stack.
Push – add a given element to the top of the stack.
<<interface>>
Stack
+peek() Object
+pop() Object
+push(Object)
+size() int
STACK USING ARRAY-BASED
IMPLEMENTATION
• A Stack Interface:
1 /** 29 public void size(Object object);
2 * The <code>Stack</code> interface specifies the basic 30
operations 31 /**
3 * of a last-in-first-out (LIFO) containers. 32 * Returns the number of elements in this stack.
4 */ 33 *
5 public interface Stack { 34 * @return the number of elements in this queue.
6 35 */
7 /** 36 public Object peek();
8 * Returns a reference to the top element on this stack, 37
leaving 38 /**
9 * the stack unchanged. 39 * Returns the element at the top of this stack.
10 * 40 *
11 * @return the element at the top of this stack. 41 * @return the element at the top of this stack.
12 * @throws IllegalStateException if this stack is empty. 42 * @throws IllegalStateException if this stack is empty.
13 */ 43 */
14 public Object pop();
15
16 /**
17 * Removes and returns the element at the top of this stack.
18 *
19 * @remove and return the element at the top of this stack.
20 * @throws IllegalStateException if this stack is empty.
21 */
22 public Object push();
23
24 /**
25 * Adds the specified element to the top of this stack.
26 *
27 * @param object the element to be pushed onto this stack.
28 */
STACK USING ARRAY-BASED
IMPLEMENTATION
The implementation file begins as follows:
ArrayStack
- a: Object[]
- size: int
(methods)
STACK USING ARRAY-BASED
IMPLEMENTATION
An ArrayStack Class
Resize an array
// rebuild the array, doubling its size
private void resize() {
Object[] aa = a;
a = new Object[2*aa.length];
System.arraycopy(aa, 0, a, 0, size);
}
STACK USING ARRAY-BASED
IMPLEMENTATION
• Pop
// retrieves and removes the top of a stack
public Object pop() {
if (size == 0) throw new IllegalStateException("stack is
empty");
Object object = a[--size];
a[size] = null;
return object;
}
STACK USING ARRAY-BASED
IMPLEMENTATION
• Peek
Function definition:
public Object peek() {
if (size == 0) throw new IllegalStateException("stack is
empty");
return a[size-1];
}
STACK USING ARRAY-BASED
IMPLEMENTATION
•Output:
crates.size(): 5 crates.peek(): BANANAS
crates.pop(): BANANAS
crates.pop(): PICKLES
crates.pop(): RAISINS
crates.size(): 2 crates.peek(): ORANGES
crates.size(): 4 crates.peek(): OYSTERS
crates.pop(): OYSTERS
crates.pop(): WALNUTS
crates.pop(): ORANGES
crates.pop(): CARROTS
java.lang.IllegalStateException: stack is empty
Exception in thread main
STACK APPLICATIONS
TRY THIS: 2 5 3 * + 1 – ?
3
5 5 15 1
2 2 2 2 17 17 16
STACK APPLICATION:
POSTPONING DATA USAGE
public static void main(String[] args) {
char ch;
Stack s; // create stack
double operan1, operan2, value;
System.out.println( “Type postfix expression: “);
while ( ch = System.in.read() && ch != ‘\n’) {
if ( Character.isDigit(ch) ) s.push(ch – 48);
else {
s.pop(operan2);
s.pop(operan1);
switch (ch) {
case ‘+’ : s.push(operan1+operan2); break;
case ‘-’ : s.push(operan1-operan2); break;
case ‘*’ : s.push(operan1*operan2); break;
case ‘/’ : s.push(operan1+operan2); break;
} // end switch
} // end else
} //end while
s.pop(value);
System.out.println(“ = “ + value);
}
STACK APPLICATION:
POSTPONING DATA USAGE
345*+ = 23.00
345+* = 27.00
34*5+ = 17.00
4321*++ = 9.00
9324+5-*/7+ = 10.00
STACK APPLICATION:
POSTPONING DATA USAGE