Unit2 Class 2
Unit2 Class 2
for the last element, reset the values of FRONT and REAR to
-1
the check for full queue has a new additional case:
Case 1: FRONT = 0 && REAR == SIZE – 1
X=a/b-c+d*e-a*c
If the user consider the expression and statement it
contains operators (== ,+,-,||,&&,!,/) and
operands(rear , front, MAX_QUEUE_SIZE)
items.
DATA LINK
HAT 15
CAT 4
EAT 9
GAT 1
WAT 0
BAT 3
FAT 6
VAT 7
• It is usual to draw linked list as an ordered sequence
of nodes with links being represented by arrows.
• The arrows indicates that the
The start pointer of the singly linked list plays the role of
FRONT while the pointer to the last node is set to play the
role of REAR.
When several stacks and queues coexisted, there was no
efficient way to represent them sequentially.
We can easily add or delete a node from the top of the stack.
We can easily add or delete a node to the rear of the queue
and add or delete a node at the front
Algorithm: Push item ITEM into a linked stack S with
top pointer TOP procedure PUSH_LINKSTACK (TOP,
ITEM)
end PUSH_LINKSTACK.
Algorithm: Pop from a linked stack S and output the
element through ITEM
procedure POP_LINKSTACK(TOP, ITEM)
/
* pop element from stack and set ITEM to the element
*/
if (TOP = 0) then call LINKSTACK_EMPTY
/* check if linked stack is empty */
else { TEMP = TOP ITEM = DATA(TOP) TOP =
LINK(TOP)
}
call RETURN(TEMP) ;
end POP_LINKSTACK.
Algorithm: Enqueue an ITEM into a linked list queue Q
procedure INSERT_LINKQUEUE(FRONT,REAR,ITEM)
Call GETNODE(X);
DATA(X)= ITEM;
LINK(X)= NIL;
/* Node with ITEM is ready to be inserted into Q */
if (Queue = 0) then
Queue = FRONT = REAR = X;
/* If Q is empty then ITEM is the first element in
the queue Q
else {LINK(REAR) = X; REAR = X
}
end INSERT_LINKQUEUE.
Algorithm: Dequeue an element from the
linked queue Q procedure
DELETE_LINKQUEUE (FRONT,ITEM)
if (FRONT = 0) then call LINKQUEUE_EMPTY;
/* Test condition to avoid deletion in an
empty queue */
else {TEMP = FRONT; ITEM
= DATA (TEMP); FRONT =
LINK (TEMP);
}
call RETURN (TEMP); /* return the
node TEMP to the free pool */
end DELETE_LINKQUEUE