The document discusses stacks and queues, which are common data structures that follow the Last In First Out (LIFO) and First In First Out (FIO) principles respectively. Stacks allow insertion and deletion of elements from one end only, while queues allow insertion from one end and deletion from the other end. Circular queues are better than linear queues as they make more efficient use of memory space by allowing insertion at the start when the end is reached. Multiple stacks and queues also allow managing multiple such data structures.
Stacks
•The most commondata
objects in the computer
algorithms are Stacks and
Queues.
•Stacks are dynamic data
structures that follow
the Last In First Out
(LIFO) principle
•An Ordered List in which
all the insertions and
deletions take place at the
one end called Top.
Bottom
5.
Stacks
Operations: The operationthat can be performed on the stack are:
CREATE(S) – This Procedure creates S an empty stack.
ADD(i,S) – This procedure inserts an element i into the
stack S and returns the new stack.
DELETE(i,S) – This procedure removes the top most element
from the stack S and returns the new stack.
TOP(S) – This procedure returns the top most element
of the stack S.
ISEMT(S) – This procedure returns true, if S is an empty
6.
STACK Implementations
• CREATE( ) :: = declare STACK(1:n); top 0
• ISEMTS(STACK) :: = if top = 0 then true
• else false
• TOP(STACK) :: = if top = 0 then error
else STACK(top)
Procedures- Push
procedure ADD(item, STACK, n, top)
//insert item into the STACK of maximum size n; top is the number of
elements currently in STACK//
if top n then call STACK_FULL
Top top + 1
STACK (top) item
end ADD
9.
Procedures - Pop
procedureDELETE (item, STACK, top)
//removes the top element of STACK and stores
it in item
unless STACK is empty//
if top 0 then call STACK_EMPTY
item STACK (top)
Top top - 1
end DELETE
Queue Operations
• CREATEQ(Q)which creates Q as an empty queue;
• ADDQ(i,Q) which adds the element i to the rear
of a queue and returns the new queue;
• DELETEQ(Q) which removes the front element
from the queue Q and returns the resulting
queue;
• FRONT(Q) which returns the front element of Q;
• ISEMTQ(Q) which returns true if Q is empty else
false.
19.
Queue Implementations
CREATEQ(Q) ::= declare Q(l:n); front rear 0
ISEMTQ(Q) :: = if front = rear then true
else false
FRONT(Q) :: = if ISEMTQ(Q) then error
else Q(front + 1)
Circular Queue
• CircularQueue is a bounded queue which implements arrays.
• It is better than a normal queue because it will effectively
utilize the memory space.
• If we have a normal queue and have deleted some elements
an empty space is created.
• Even if the queue has empty space we cannot insert any new
element because the inserting has to be done from one side
only(Rear) and deletions has to be done from another
side(front).
• In case of Circular Queue the Front and Rear are adjacent to
each other. Hence we can insert a new element in the empty
space (i.e the space from where the element is deleted).
Circular Queue –ADD Procedure
Procedure ADD(i,Q,n,rear)
//insert the element i into the Queue
rear=(rear+1) mod n;
If front = rear then call Queue_Full
Q(rear)=item;
end ADD
Circular Queue –Delete Procedure
Procedure DELETE(i,Q,front,rear)
//insert the element i into the Queue
If front=rear then call Queue_Empty
front = (front +1) mod n;
i=Q(front);
end DELETE