Stack and Queue
Stack and Queue
1
Stack
A stack is an ordered list in which all insertions and deletions are made at one end, called the
top. Stacks are data structures which maintain the order of last-in, first-out (LIFO).
Stack Operations
CREATE (S) which creates S as an empty stack;
ADD (i,S) which inserts the element i onto the stack S and returns the new
stack;
DELETE (S) which removes the top element of stack S and returns the new
stack;
TOP (S) which returns the top element of stack S;
ISEMTS (S) which returns true if S is empty else false;
2
Stack data structure
structure STACK (item)
1 declare CREATE ( ) - stack
2 ADD (item, stack) - stack
3 DELETE (stack) - stack
4 TOP (stack) - item
5 ISEMTS (stack) - boolean;
6 for all S stack, i item let
7 ISEMTS (CREATE) :: = true
8 ISEMTS (ADD (i,S)) :: = false
9 DELETE (CREATE) :: = error
10 DELETE (ADD (i,S)) :: = S
11 TOP(CREATE) :: = error
12 TOP(ADD(i,S)) :: = i
3
13 end
Stack data structure
To represent a stack is by using a one-dimensional array, STACK(1:n), where n is the maximum
number of allowable entries. The first or bottom element in the stack will be stored at STACK(1),
the second at STACK(2) and the i-th at STACK(i).
procedure ADD (item, STACK, n, top) procedure DELETE (item, STACK, top)
//insert item into the STACK of maximum size n; top //removes the top element of STACK and stores it in
is the number of elements currently in STACK// item unless STACK is empty//
if top = n then call STACK_FULL if top = 0 then call STACK_EMPTY
top= top + 1 item= STACK (top)
STACK (top) item top = top - 1
end ADD end DELETE
4
Queue
Queues are data structures which maintain the order of first-in, first-out. A queue is an
ordered list in which all insertions take place at one end, the rear, while all deletions take
place at the other end, the front.
5
Queue operations
6
Queue data structure
structure QUEUE (item)
1 declare CREATEQ( ) -
2 ADDQ(item,queue) -
3 DELETEQ(queue) -
4 FRONT(queue) -
5 ISEMTQ(queue) -
6 for all Q queue, i item let
7 ISEMTQ(CREATEQ) :: =
8 ISEMTQ(ADDQ(i,Q)) :: =
9 DELETEQ(CREATEQ) :: =
10 DELETEQ(ADDQ(i,Q)):: =
11 if ISEMTQ(Q) then CREATEQ
12 else ADDQ(i,DELETEQ(Q))
13 FRONT(CREATEQ) :: =
14 FRONT(ADDQ(i,Q)) :: =
15 if ISEMTQ(Q) then i else FRONT(Q)
16 end
17 end QUEUE
9
Algorithms for ADDQ and DELETEQ
procedure ADDQ(item, Q, n, rear)
//insert item into the queue represented in Q(l:n)//
if rear = n then call QUEUE_FULL
rear = rear + 1
Q(rear) = item
end ADDQ
10
Algorithms for ADDQ and DELETEQ
procedure ADDQ(item, Q, n, rear)
//insert item into the queue represented in Q(l:n)//
if rear = n then call QUEUE_FULL
rear = rear + 1
Q(rear) = item
end ADDQ
11
Inefficiency in normal QUEUE representation
One obvious thing to do when QUEUE_FULL is signaled is to move the entire queue to the left so that
the first element is again atQ(1) and front = 0. This is time consuming, especially when there are many
elements in the queue at the time of the QUEUE_FULL signal.
12
Circular queue
A more efficient queue representation is obtained by regarding the array Q(1:n) as circular. It
now becomes more convenient to declare the array as Q(0:n - 1). When rear = n - 1, the next
element is entered at Q(0) in case that spot is free. Using the same conventions as before, front
will always point one position counterclockwise from the first element in the queue. Again,
front = rear if and only if the queue is empty. Initially have front = rear = 1.
13
Circular queue
14
Algorithms ADDQ and DELETEQ
One surprising point in the two algorithms is that the test for queue full in ADDQ and the test for queue empty
in DELETEQ are the same. To avoid this, we signal queue-full thus permitting a maximum, of n - 1 rather
than n elements to be in the queue at any time.
15