Algorithms and Data Structures: Queue
Algorithms and Data Structures: Queue
Structures
Queue
Definition
a b c d e f
queue
front end
add(obj)
Queue() {
ListNode front;
ListNode end;
Queue
Queue(); // create empty queue
void add(Object obj); // add object to queue
Object remove(); // get & throw top element
isEmpty()
ListNode getFront(); // get top element
getFront() boolean isEmpty(); // check if stack empty
}
Queue
Sample Code
Queue q = new Queue();
q.add(‘a’);
a b c d e f
q.add(‘b’);
q.add(‘c’); queue
q.add(‘d’);
String str = q.getFront(); front end
q.remove();
q.add(‘e’); a
q.add(‘f’);
q.remove();
str
Queue
Checking Palindrome
Palindrome is sequence of character that is the
same if it is read from left to right or
right to left
Procedure:
“c1 c2 c3 c4 c5 c6”
Given a string
Reverse character order
using stack c6 c5 c4 c3 c2 c1
Enter characters into
queue
Check if the two sequences c1 c2 c3 c4 c5 c6
are the same
Queue
Implementation
Using Linked-List Iteration
Using circular array
queue.add(‘g’);
queue queue.add(‘h’);
queue.add(‘i’);
ja b c d e f g h i queue.remove();
queue.remove();
queue.add(‘j’);
front end
Queue
Assignment (19 Oktober 2017)
Make your own code for:
Implementing queue using Linked-List
Implementing queue using DoublyLinked-
List
Checking palindrome
Implementing queue using circular array
Implementing priority queue
Queue