Stack and Queue
Outline
I. Stack
Stack
Array-based Stack
Stack Implementation
II. Queue
Queue
Priority Queue
Queue Implementation
Stack
Stack Data Structure (1/4)
Stack Data Structure:
Linear data structure
It can be accessed only at one of its ends
for storing and retrieving data.
A Last In, First Out (LIFO) data structure:
• Anything added to the stack goes on
the ‘Top’ of the stack.
• Anything removed from the stack is
taken from the ‘Top’ of the stack
• Things are removed in the reverse or-
der from that in which they were in-
serted
Stack Data Structure (2/4)
Opeartions on a Stack :
clear(): clear the stack
isEmpty(): check whether if the stack is empty
push(a): Put the element a on the top of the stack
pop(): Take the topmost element fron the stack
top(): return the topmost element in the stack without removing it
Stack Data Structure (3/4)
Stack Exception:
Operations pop and top can be performed if the stack is empty:
• StackEmptyException
Stack Data Structure (4/4)
Applications:
Sort of nesting (such as parenthese)
Evaluating arithmetic expressions (and other sorts of expression)
Keeping track of previous choices (as in backtracking)
Undo sequence in a texy editor
Component of the other data structure
Array-based Stack (1/2)
Array-based Stack:
Use an array to implement the Stack ADT (abstract data type):
• Add elements from left to right
• Use a top variable keeps track of the index of the top element
…
S
0 1 2 top
Array-based stack
Array-based Stack (2/2)
Array-based Stack:
Limitations:
• The stack may become full thrown a exception:
– FullStackException
…
S
0 1 2 top
Array-based stack may become full
Solution:
• Use Linked-list to implement the Stack
Stack Implementation (1/3)
Array-based Stack Implementation:
class ArrayStack public boolean isEmpty()
{protected Object [] a; int top, max; { return(top==-1);}
public ArrayStack() public boolean isFull()
{ this(50); { return(top==max-1);}
} public void clear()
public ArrayStack(int max1) { top=-1;}
{ max = max1; public void push(Object x)
a = new Object[max]; { if(isFull() && !grow()) return;
top = -1; a[++top] = x;
} }
protected boolean grow() Object top() throws EmptyStackException
{ int max1 = max + max/2; { if(isEmpty()) throw new EmptyStackException();
Object [] a1 = new Object[max1]; return(a[top]);
if(a1 == null) return(false); }
for(int i =0; i<=top; i++) a1[i] = a[i]; public Object pop() throws EmptyStackException
a = a1; { if(isEmpty()) throw new EmptyStackException();
return(true); Object x = a[top];
} top--;
public boolean isEmpty() return(x);
{ return(top==-1);} }
Stack Implementation (2/3)
Linked List-based Stack Implementation:
class Node
{ public Object info;
public Node next; Object top() throws EmptyStackException
public Node(Object x, Node
{ if(isEmpty()) throw new EmptyStackException();
p)
{ info=x; next=p; } return(head.info);
public Node(Object x) }
{ this(x,null); }
};
public Object pop() throws EmptyStackException
class LinkedStack { if(isEmpty()) throw new EmptyStackException();
{ protected Node head; Object x = head.info;
head=head.next;
public LinkedStack()
{ head = null; } return(x);
}
public boolean isEmpty()
{ return(head==null);}
public void push(Object x)
{ head = new Node(x,head);
}
Stack Implementation (3/3)
Stack Implementation in Java.util:
import java.util.*; import java.util.*;
class MyStack class MyStack
{ArrayList h; {LinkedList h;
MyStack() {h = new ArrayList();} MyStack() {h = new LinkedList();}
boolean isEmpty() boolean isEmpty()
{return(h.isEmpty());} {return(h.isEmpty());}
void push(Object x) void push(Object x)
{h.add(x); {h.add(x);
} }
Object pop() Object pop()
{if(isEmpty()) return(null); {if(isEmpty()) return(null);
return(h.remove(h.size()-1)); return(h.removeLast());
} }
} }
Arraylist-based LinkedList-based
Application Implementation
Convert decimal integer number to binary number
public class Main
{public static void decToBin(int k)
{MyStack s = new MyStack();
System.out.print(k + " in binary system is: ");
while(k>0)
{s.push(new Integer(k%2));
k = k/2;
}
while(!s.isEmpty())
System.out.print(s.pop());
System.out.println();
}
public static void main(String [] args)
{decToBin(11);
System.out.println();
}
}
Stack Class in Java
The Stack class implemented in Java.util package
Summary
Stack is a linear data structure that can be accessed at only one
of its ends for storing and retrieving data.
Stack is called an LIFO structure: last in/first out.
Reading at home
Text book: Data Structures and Algorithms in Java.
• 6 Stacks, Queues, and Deques 225
• 6.1 Stacks - 226
Queue
Queue Data Structure (1/4)
Queue Data Structure:
A waiting line that grows by adding el-
ements to its end and shrinks by tak-
ing elements from its front
A First In, First Out (FIFO) data struc-
ture:
• A structure in which ends are
used:
– One for adding new element
– One for removing them
Queue Data Structure (2/4)
Opeartions on a Queue :
clear(): clear the queue
isEmpty(): check whether if the stack is empty
enqueue(a): Put the element a at the end of the queue
pop(): Take the topmost element fron the stack
top(): return the topmost element in the stack without removing it
Queue Data Structure (3/4)
Opeartions on a Queue :
clear(): clear the queue
isEmpty(): check whether if the stack is empty
enqueue(a): Put the element a at the end of the queue
pop(): Take the topmost element fron the stack
top(): return the topmost element in the stack without removing it
Queue Data Structure (4/4)
Applications:
Direct applications:
• Waiting list
• Access to shared resource (printer)
• Multiprogramming
Indirect application:
• Auxiliary data structure for algorithms
• Component of other data structure
Array-based Queue (1/2)
Array-based Queue:
Use an array of size in a circular fashion
Two variables keep track of the first and last:
• : index of the front element
• : index of the last element
normal configuration
Q
012 f l
wrapped-around configuration
Q
012 l f
Array-based Queue
Array-based Queue (2/2)
Array-based Queue in detail
Queue Implementation (1/3)
Array-based Queue Implementation:
class ArrayQueue private boolean grow()
{ protected Object [] a; { int i,j;
protected int max; int max1 = max + max/2;
protected int first, last; Object [] a1 = new Object[max1];
if(a1 == null) return(false);
public ArrayQueue() if(last>=first)
{ this(10); for(i=first;i<=last;i++) a1[i-first]=a[i];
} else
public ArrayQueue(int max1) { for(i=first;i<max;i++) a1[i-first]=a[i];
{ max = max1; i = max-first;
a = new Object[max]; for(j=0;j<=last;j++) a1[i+j]=a[j];
first = last = -1; }
} a = a1;
public boolean isEmpty() first = 0;
{ return(first==-1);} last = max-1;
max = max1;
public boolean isFull() return(true);
{ return((first == 0 && }
last == max-1) || first == last+1);
}
Queue Implementation (2/3)
Array-based Queue Implementation-cont:
void enqueue(Object x)
{ if(isFull() && !grow()) return;
if(last == max-1 || last == -1)
{ a[0] = x; last=0;
if(first==-1) first = 0;
}
else a[++last] = x;
}
Object front() throws Exception
{ if(isEmpty()) throw new Exception();
return(a[first]);
}
public Object dequeue() throws Exception
{ if(isEmpty()) throw new Exception();
Object x = a[first];
if(first == last) // only one element
{first = last = -1;}
else if(first==max-1)
first = 0;
else
first++;
return(x);
}
Queue Implementation (3/3)
LinkedList-based Queue Implementation-cont:
class Node
{ public Object info; public Object dequeue() throws Exception
public Node next; { if(isEmpty()) throw new Exception();
public Node(Object x, Node p) Object x = head.info;
{ info=x; next=p; } head=head.next;
public Node(Object x) if(head==null) tail=null;
{ this(x,null); } return(x);
}; }
void enqueue(Object x)
class MyQueue { if(isEmpty())
{ protected Node head,tail; head = tail = new Node(x);
else
public MyQueue() { tail.next = new Node(x);
{ head = tail = null; } tail = tail.next;
}
public boolean isEmpty() }
{ return(head==null);}
Object front() throws Exception
{ if(isEmpty()) throw new Exception();
return(head.info);
}
Circular Queue
Circular Queue:
An extension of a regular Queue
The last element is connected to the first element.
enqueue
enqueue 11
54
54 11 24 44 78 dequeue
78
24
dequeue
44
Regular queue Circular queue
Double-Ended Queue (1/2)
Double-End Queue:
In short: deque
A linear data structure where the insertion and deletion operations are
performed from both ends.
Though the insertion and deletion in a deque can be performed on
both ends
enqueue enqueue enqueue
54 11 24 44 78 54 11 24 44 78
dequeue dequeue
dequeue
Regular queue Double-End queue
Double-Ended Queue (2/2)
Operations in Double-End Queue:
Main functions:
• addFirst(a): insert a new element a at the front of the deque
• addLast(a): insert a new element a at the end of the deque
• removeFirst(): remove and return the first element/null of the deque
• removeLast(): remove and return the last element/null of the deque
Optional:
• first() : return the first element of the deque
• last() : return the last element of the deque
• size() : return the number of elements in the deque
• isEmpty(): return a Boolean indicating whether the deque is empty
Priority Queue (1/3)
Operations in Double-End Queue:
Main functions:
• addFirst(a): insert a new element a at the front of the deque
• addLast(a): insert a new element a at the end of the deque
• removeFirst(): remove and return the first element/null of the deque
• removeLast(): remove and return the last element/null of the deque
Optional:
• first() : return the first element of the deque
• last() : return the last element of the deque
• size() : return the number of elements in the deque
• isEmpty(): return a Boolean indicating whether the deque is empty