3. LOGO
1. Define Queues
Queue is way of organizing the objects stored in
the form of a linear list of objects which the addition
is made in the list and get the object that is made at
the end of the list.
Queue also called type list FIFO (First In First
Out - in front before)
6. LOGO
Operations of Queues
Queue Interface Structure
Method Behavior
E peek() Returns the object at the top of the queue without
removing it. If the queue is empty, returns null.
E poll() Returns the object at the top of the queue and removes it.
If the queue is empty, returns null.
boolean offer(E obj) Appends item obj at the end of queue.
Returns true if successful false otherwise.
8. LOGO
Implementation of a Queue
Implementation of a Queue using Linkedlist
LinkedList implements the Queue interface, so you can declare:
• Queue<String> name = new LinkedList<String>();
Class ListQueue contains a collection of Node<E> Objects
Insertions are at the rear of a queue and removals are from the front
9. LOGO
Implementation of a Queue
Implementing a Queue Using a Circular Array
• Need to know the index of the front, the index of the read, the size, and
the capacity.
• Mod can be used to calculate the front and Read positions in a circular
array, therefore avoiding comparisons to the array size
The read of the queue is:
(front + count - 1) % items.length;
where count is the number of items currently in the queue.
After removing an item the front of the queue is:
(front + 1) % items.length;
10. LOGO
Implementation of a Queue
//Java Code
Queue<Integer> q = new
ArrayBlockingQueue(6);
q.offer(6);
0 1 2 3 4 5
6
front = 0
count = 01
insert item at (front + count) % items.length
11. LOGO
Implementation of a Queue
0 1 2 3 4 5
6
front = 0
4 7 3 8
count = 12345
//Java Code
Queue<Integer> q = new
ArrayBlockingQueue(6);
q.offer(6);
q.offer(4);
q.offer(7);
q.offer(3);
q.ofer(8);
12. LOGO
Implementation of a Queue
0 1 2 3 4 5
6
front = 0
4
make front = (0 + 1) % 6 = 1
1
7 3 8 9
count = 5434
make front = (1 + 1) % 6 = 2
2
//Java Code
Queue<Integer> q = new
ArrayBlockingQueue(6);
q.offer(6);
q.offer(4);
q.offer(7);
q.offer(3);
q.offer(8);
q.poll();//front = 1
q.poll();//front = 2
q.offer(9);
13. LOGO
Implementation of a Queue
//Java Code
Queue<Integer> q = new
ArrayBlockingQueue(6);
q.offer(6);
q.offer(4);
q.offer(7);
q.offer(3);
q.offer(8);
q.poll();//front = 1
q.poll();//front = 2
q.offer(9);
q.offer(5);
0 1 2 3 4 5
front = 2
7 3 8 95
count = 45
insert at (front + count) % 6
= (2 + 4) % 6 = 0
14. LOGO
APPLICATIONS
The direct application
- Queue list
- Access to shared resources (Printers on the local
network)
- Most programming
The application does not directly
- Data structure algorithms support
- How components of other data structures
15. LOGO8-Queen Problems
The eight queens puzzle is the problem of placing eight chess queens on
an 8 8 chessboard so that no two queens attack each other. Thus, a
solution requires that no two queens share the same row, column, or
diagonal.
16. LOGO
8-Queen Problems Using Back Tracking
Backtracking is a general algorithm for
finding all (or some) solutions to
some computational problem, that
incrementally builds candidates to the
solutions, and abandons each partial
candidate c ("backtracks") as soon as it
determines that c cannot possibly be
completed to a valid solution
17. LOGO
Step Revisited- Backtracking
1.Place the first queen in the left upper corner of the table.
2. Save the attacked positions
3. Move to the next queen(which can only be placed to the next line).
4. Search for a valid position. If there is one go to step 8.
5. There is not a valid position for the queen. Delete it ( the x
coordinate is 0).
6. Move to the previous queen.
7. Go to step 4.
8. Place it to the first valid position.
9. Save the attacked posotions.
10. If the queen processed is the last stop otherwise go to step 3.
18. LOGO
Algorithm
public static void Try(int j){
for (int i = 1; i<=8; i++)
{
if (a[i]&& b[i+j]&&c[i-j+7])
{
x[j] = i;
a[i] = false;
b[i+j] = false;
c[i-j+7] = false;
if(j<8) Try(j+1);
else Print(x);
a[i] = true;
b[i+j] = true;
c[i-j+7] = true;
}
19. LOGO
Solution
There are 92 solutions to the 8 x 8 problem.
Many of these are reflections and rotations of some of the
others, and if we de-duplicate against this, purists state that
there are only 12 distinct solutions (92 does not divide equally
by 12 because many of the reflections and rotations of a pure
solutions are not unique).