0% found this document useful (0 votes)
10 views15 pages

Stack and Queue

Stack and queue

Uploaded by

shreeshaaks01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

Stack and Queue

Stack and queue

Uploaded by

shreeshaaks01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Unit - I

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.

Queues are more difficult to implement than stacks, because action


happens at both ends. The restrictions on a queue require that the
first element which is inserted into the queue will be the first one to
be removed. Thus A is the first letter to be removed, and queues are
known as First In First Out (FIFO) lists.

5
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.

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

KG College of Arts and Science II B.Sc. IT A Data Structures 7


Queue data structure
structure QUEUE (item)
1 declare CREATEQ( ) - queue
2 ADDQ(item,queue) - queue
3 DELETEQ(queue) - queue
4 FRONT(queue) - item
5 ISEMTQ(queue) - boolean;
6 for all Q queue, i item let
7 ISEMTQ(CREATEQ) :: = true
8 ISEMTQ(ADDQ(i,Q)) :: = false
9 DELETEQ(CREATEQ) :: = error
10 DELETEQ(ADDQ(i,Q)):: =
11 if ISEMTQ(Q) then CREATEQ
12 else ADDQ(i,DELETEQ(Q))
13 FRONT(CREATEQ) :: = error
14 FRONT(ADDQ(i,Q)) :: =
15 if ISEMTQ(Q) then i else FRONT(Q)
16 end
17 end QUEUE

KG College of Arts and Science II B.Sc. IT A Data Structures 8


Queue example

Q(1) (2) (3) (4) (5) (6) (7) ... Remarks


front rear
0 0 queue empty Initial
0 1 Jl Job 1 joins Q
0 2 J1 J2 Job 2 joins Q
0 3 J1 J2 J3 Job 3 joins Q
1 3 J2 J3 Job 1 leaves Q
1 4 J2 J3 J4 Job 4 joins Q
2 4 J3 J4 Job 2 leaves Q

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

procedure DELETEQ(item, Q, front, rear)


//delete an element from a queue//
if front = rear then call QUEUE_EMPTY
front = front + 1
item = Q(front)
end DELETEQ

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.

Each time a new element is added,


the entire queue of n - 1 elements is
moved left.

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.

if rear = n - 1 then rear 0


else rear = rear + 1

13
Circular queue

Circular queue of n elements and four jobs


J1, J2, J3, J4

14
Algorithms ADDQ and DELETEQ

procedure ADDQ(item, Q, n, front, rear) procedure DELETEQ(item, Q, n, front, rear)


//insert item in the circular queue stored in Q(0:n - 1); //removes the front element of the queue Q(0:n - 1)//
rear points to the last item and front is one position if front = rear then call QUEUE-EMPTY
counterclockwise from the first item in Q// front= (front + 1)mod n
rear= (rear + l)mod n //advance rear clockwise// //advance front clockwise//
if front = rear then call QUEUE-FULL item = Q(front)
Q(rear)= item //insert new item// //set item to front of queue//
end ADDQ end 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy