Professional Documents
Culture Documents
Queue: (FIFO) Lists
Queue: (FIFO) Lists
Queue: (FIFO) Lists
front rear
Features:
A list structure with two access points called the
front and rear.
All insertions (enqueue) occur at the rear and
deletions (dequeue) occur at the front.
Varying length (dynamic).
Homogeneous components
Has a First-In, First-Out characteristic (FIFO)
Queue
Queue
Algorithm to add an item into queue
1. Algorithm AddQ(item,Q,n,rear)
2. { // insert item into the queue represented in Q(1 : n)
3. if (rear = n) then
4. {
5. write (Queue full)
6. Return
7. }
8. rear = rear +1
9. Q(rear) = item
10.}
Queue
Algorithm to delete an item from queue
1.Algorithm DeleteQ (item,Q,front,rear)
2.{ // delete an element from a queue
3.if ( front = rear ) then
4.{
5.write (Queue empty)
6.}
7.front = front +1
8.Item = Q(front)
9.}
Circular Queue
Another way to represent queue is obtained
by regarding the array Q(0:n) as circular
When rear = n-1, the next element is entered
at Q(0) if it is free.
If front=rear then the queue is empty
Initially we have front=rear=1
Circular Queue
Front=0, rear =3
Figure shows the circular queue of n
elements
and 3 jobs
n-2
3 J3
J2 n-1
J1
2
0
1
Circular Queue
Algorithm to add an item into circular queue
1. Algorithm AddQ(item,Q,n,front,rear)
2. { // insert item in the circular queue started in Q(0 : n-1)
3. // rear points to be the last item and front is one position
4. // counter clockwise from the first item in Q
5. rear = (rear+1) mod n // advance rear clockwise
6. if (front=rear) then
7. {
8. write (queue is full)
9. return
10.}
11.Q(rear)=item //insert new item
12.}
Circular Queue
Algorithm to delete one item from the circular queue
1.circular
Algorithmqueue
DeleteQ(item, Q, n, front, rear)
2. { // removes the front element of the queue Q (0:n-
1)
3. if (front=rear) then
4. {
5. write (queue is empty)
6. }
7. front = (front+1) mod n //advance front clockwise
8. item = Q(front) //set item to front of queue
9. }
Priority Queue
A priority queue is a data structure for
maintaining a set of elements such that each
element has been assigned a priority and such
that the order in which elements are deleted
and processed comes from the following rules
1. An element for higher priority is processed
before any element of low priority
2. Two elements with the same priority are
processed according to the order in which
they were added to the queue
Applications of Queue
Typical uses of queues are in simulations and
operating systems.
Operating systems often maintain a queue of
processes that are ready to execute or that are
waiting for a particular event to occur.
Computer systems must often provide a
holding area for messages between two
processes, two programs, or even two
systems. This holding area is usually called a
buffer and is often implemented as a queue.
Linked List
Linked list is a structure that leads to
algorithm that minimize data movement as
insertions and deletions occurs in an ordered
list
Each element called node
Each node has two fields called DATA and LINK
DATA LINK
Singly Linked List
A single linked list is a linked list in which each node
contains only one link field pointing to the next node
in the list
Algorithm to create linked list
1. Algorithm Createlist(T)
2. { // T is a pointer to the first node in the list
3. Get new node I // get new node
4. T = I
5. Data(I) = A // store information into node
6. Get new node I
7. LINK(T) = I // attach first node to second node
8. LINK(I) = 0
9. DATA(I)=B
10.}
Singly Linked List
The result is
T A B 0
1. Algorithm Insertnode(T,X)
2. { // T is a list, T=0 if T has no nodes
3. // This procedure insert an item C after node X
4. Get new node I
5. Data (I) = C
6. If (T=0) then {T=I ; LINK(I) = 0}
7. Else {LINK (I) = LINK(X); LINK(X) = I}
8. }
Singly Linked List
The result is
T C 0
T X 0
C
Singly Linked List
Algorithm to delete a node from the list
1. Algorithm Deletenode(X,Y,T)
2. { // This procedure delete node X from T, Y be the node
preceding X, Y=0 if X is the first of T
3. If (Y=0) then T= LINK(X)
4. Else LINK(Y)=LINK(X)
5. Remove X from storage
6. }
T Y X 0
Doubly Linked List
A doubly linked list has two pointers, a forward link
and a backward link.
The forward link is points to the next node in the list
Backward link points to the preceding node
T A X C 0
Doubly Linked List
Algorithm to insert a node into the list
1. Algorithm Dinsert(P,X)
2. { // insert node P into right of node X
3. LLINK(P)=X
4. RLINK(P)=RLINK(X)
5. LLINK(RLINK(X))=P
6. RLINK(X)=P
7. }
T X A 0
P
Application of Linked List
Polynomial Addition
Consider a problem, manipulating the polynomials using linked list.
In general, we represent the polynomial as
A
3 14 2 8 1 0 0
C 11 14
Polynomial Addition
Step 2
EXP(P) < EXP(Q), the term B is attached to C.
The pointer and Q is advanced to next term
P
3 14 2 8 1 0 0
A
Q
B 8 14 -3 10 10 6 0
C 11 14 -3 10 0
Polynomial Addition
Step 3
EXP(P) > EXP(Q), the term A is attached to C.
The pointer and P is advanced to next term
P
3 14 2 8 1 0 0
A
Q
B 8 14 -3 10 10 6 0
C 11 14 -3 10 2 8 0
Polynomial Addition
Step 4
EXP(P) < EXP(Q), similar to step 2.
P
3 14 2 8 1 0 0
A
Q
B 8 14 -3 10 10 6 0
C 11 14 -3 10 2 8 0 10 6 0
Polynomial Addition
Step 4
No more terms in B. so the remaining terms in
A is attached to C. The resultant Polynomial C is,
11 14 -3 10 2 8 10 6 1 0 0
Algorithm for polynomial addition
Each time a new node is generated its COEF
and EXP fields are set and it is appended to
the end of the list
We keep a pointer d which points to the
current last nodes in C
The algorithm polyadd uses a subroutine
Attach which create a new node and append it
to the end of C
Initially C having a single node with no values
which is deleted at the end of the algorithm
Algorithm for polynomial addition
1. Algorithm Attach (C,E,d)
2. {
3. // This algorithm create a new term with COEF=C, and
EXP=E and attach it to the node pointed by d
4. GETNODE(I) // create a new node
5. EXP(I)=E
6. COEF(I)=C
7. Link (d)=I//attach the node I to the list
8. d=I //move the pointer to the new last node
9. }
Algorithm for polynomial addition
1. Algorithm polyadd(A,B,C) 21. while P 0 //copy remaining
2. { terms of A
3. P=A, Q=B 22. {
4. GETNODE(C) //Initial node for C 23. Attach(COEF(P),EXP(P),d)
5. d=C 24. P=LINK(P)
6. While P 0 and Q 0 25. }
7. { // if there are terms in A and B 26. while Q 0 // copy remaining
8. Case terms of B
9. EXP(P) = EXP(Q) //equal exponents 27. {
10. x = COEF(P) + COEF(Q) 28. Attach(COEF(Q),EXP(Q),d)
11. if (x 0) then 29. Q=LINK(Q)
12. Attach(x,EXP(P),d) 30. }
13. P=LINK(P) 31. LINK(d)=NULL
14. EXP(P) < EXP(Q) 32. }
15. Attach (COEF(Q),EXP(Q),d)
16. Q=LINK(Q)
17. else
18. Attach (COEF(P),EXP(P),d)
19. P=LINK(P)
20. }