0% found this document useful (0 votes)
95 views

Data Structure-Module 2

The document discusses stacks and queues. Stacks follow LIFO (last in, first out) order where elements can only be inserted or removed from one end, called the top. Common stack operations are push to insert and pop to remove. Queues follow FIFO (first in, first out) order where elements can only be inserted at the rear and removed from the front. The document provides algorithms for array implementation of stacks and linked lists implementation of queues.

Uploaded by

alice
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Data Structure-Module 2

The document discusses stacks and queues. Stacks follow LIFO (last in, first out) order where elements can only be inserted or removed from one end, called the top. Common stack operations are push to insert and pop to remove. Queues follow FIFO (first in, first out) order where elements can only be inserted at the rear and removed from the front. The document provides algorithms for array implementation of stacks and linked lists implementation of queues.

Uploaded by

alice
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

STACKS AND QUEUES

NOTES
by
aminotes
STACKS
A Stack is a list of elements in which an element may be inserted or delet-
ed only at one end, called the top of the stack. This means, in particular,
that elements are removed from a stack in the reverse order of that in
whicch they were inserted into the stack.
The two basic operations associated with stacks are:
(i) “PUSH” - It is the term used to insert an element into a stack.
(ii) “POP” - It is the term used to delete an element from a stack.

NOTE- These terms are used only with stacks and not with other data
structures.
aminotes
ARRAY REPRESENTATION OF A STACK
Stacks can be represented in a linear way. Each of our stacks will be main-
tained by a linear array STACK; a pointer variable TOP, which contains the
location of the top element of the stackl and a variable MAXSTK which
gives the number of elements that can be held by the stack. The condition
TOP=0 or TOP=NULL will indicate that the stack is empty.

The figure represent an array representation of a stack. Since TOP=3, the


stack has three elements, AMI, NOT and ES; and since MAXSTK =6, there
is room for 3 more items in the stack.

STACK

AMI NOT ES
1 2 3 4 5 6

TOP 3 MAXSTK 6

To add an element (PUSH), one must first test whether there is a room in
the stack for the new item, if NOT then we have the condition known as
Overflow. Similarly, in executing the procedure delete (POP), one must first
test whethere there is an element in the stack to be deleted, if NOT then
we have the condition known as underflow.
aminotes
ALGORITHM - STACK PUSH
PUSH(STACK, TOP, MAXSTK, ITEM)
This procedure pushes an ITEM onto a stack.

1. IF TOP = MAXSTK, then print OVERFLOW and return (it means the
stack is full and we cannot add element into it.
2. SET TOP = TOP +1 (Increases the size of the stack, TOP by 1)
3. SET STACK[TOP] = ITEM (Inserts ITEM in new TOP position)
4. RETURN

ALGORITHM - STACK POP


POP(STACK, TOP, ITEM)
This procedure deletes the top element of the STACK and assigns it to the
variable ITEM.

1. IF TOP = 0, then print UNDERERFLOW and return (If there is no ele-


ment in the stack then we cannot delete)
2. SET ITEM= STACK[TOP] (Assigns the TOP element to ITEM)
3. SET TOP = TOP-1 (Decreases TOP by 1)
4. RETURN
aminotes
CONVERSION INFIX TO POSTFIX
ALGORITHM -
1. PUSH “(” onto STACK, and add “)” to the end of Q.
2. SCAN Q from left to right and repeat Steps 3 to 6 for each element of Q
until the stack is empty.
3. If an operand is encountered, add it to P.
4. If a left paranthesis is encountered, push it onto STACK.
5. If an operator is encountered then:
(a) Repeatedly POP from STACK and add to each operator (on the top
of STACK) which has the same precedenceas or higher precendence
than
(b) Add to STACK.
6. If a right paranthesis is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on the
top of STACK) until a left paranthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis on P.}
7. EXIT

CONVERSION INFIX TO PREFIX


ALGORITHM -
1. Reverse the Expression and parse the inputs in the expression one by
one.
2. If the input is an operand, then place it in the output buffer.
3. If the input is an operator, push it into the stack.
4. If the operator in stack has equal or higher precedence than input oper-
ator, then pop the operator present in stack and add it to output buffer.
5. If the input is an close brace, push it into the stack.
6. If the input is a open brace, pop elements in stack one by one until we
encounter open brace.
7. Discard braces while writing to output buffer.
8. EXIT
aminotes
EXAMPLES ON INFIX TO POSTFIX
aminotes
EXAMPLES ON INFIX TO POSTFIX
aminotes
EXAMPLES ON INFIX TO PREFIX
aminotes
TOWER OF HANOI
ALGORITHM -
TOWER(N, BEG, AUX, END) for general N
1. If N=1, then:
(a) Write: BEG -> END.
(b) Return.
2. Call TOWER(N-1, BEG, END, AUX) [ Move N-1 disks from peg BEG to
peg AUX].
3. Write: BEG -> END.
4. Call TOWER (N-1, AUX, BEG, END) [Move N-1 disks from peg AUX to
peg END].
5. Return.
aminotes
TOWER OF HANOI
aminotes
QUEUES
A queue is a linear list of elements in which deletions can take place only
at one end, called the front, and insertions can take place only at the other
end, called the rear. The terms “front” and “rear” are used in describing a
linear list only when it tis implemented as a queue.
Queues are also called first-in-first-out (FIFO) lists.The order in which ele-
ments enter a queue is the order in which they leave. This contrasts with
stack, which are last-in-first-out (LIFO) lists.

QUEUE

AMI NOT ES
1 2 3 4 5 6

1 3

FRONT REAR

QUEUE

NOT ES
1 2 3 4 5 6

2 3

FRONT REAR
aminotes
QUEUES
aminotes
QUEUES
INSERTION
LINKQ_INSERT(INFO, LINK, FRONT, REAR, AVAIL, ITEM)
This procedure inserts an ITEM into a linked queue.
1. If AVAIL = NULL then write OVERFLOW and EXIT (Checking space)
2. Set NEW = AVAIL and AVAIL = LINK[AVAIL]
3. Set INFO[NEW] = ITEM and LINK[NEW] = NULL
4. If (FRONT=NULL) then FRONT= REAR = NEW
else set LINK[REAR] = NEW and REAR = NEW
5. Exit

INSERTION
LINKQ_DELETE(INFO, LINK, FRONT, REAR, AVAIL, ITEM)
This procedure deletes the front element of the linked queue and stored it in
ITEM
1. If FRONT = NULL then write UNDERERFLOW and EXIT (Checking space)
2. Set TEMP = FRONT
3. ITEM = INFO[TEMP]
4. FRONT = LINK[TEMP]
5. LINK[TEMP] = AVAIL and AVAIL = TEMP
6. Exit

You might also like