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

Stacks and Queues - 3

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)
16 views

Stacks and Queues - 3

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/ 8

Stacks and Queues-2

PUSH OPERATION
The process of putting a new data element onto stack is known as a Push Operation.
Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.

Algorithm for PUSH Operation

begin procedure push: stack, data


if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure

Implementation of this algorithm in C


void push(int data) {
if(!isFull()) {
top = top + 1;
stack[top] = data;
}else {
printf("Could not insert data, Stack is full.\n");
}
}

POP OPERATION
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit
Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success

Algorithm for Pop Operation


A simple algorithm for Pop operation can be derived as follows –

begin procedure pop: stack


if stack is empty
return null
endif

data ← stack[top]

top ← top - 1

return data

end procedure

Implementation of this algorithm in C –

int pop(int data) {

if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}
else {
printf("Could not retrieve data, Stack is empty.\n");
}
}

LIST ADT

List is basically the collection of elements arranged in a sequential manner. In memory we can
store the list in two ways: one way is we can store the elements in sequential memory locations.
That means we can store the list in arrays. The other way is we can use pointers or links to associate
elements sequentially. This is known as linked list.

LINKED LISTS:
The linked list is very different type of collection from an array. Using such lists, we can store
collections of information limited only by the total amount of memory that the OS will allow us to
use. Further more, there is no need to specify our needs in advance. The linked list is very flexible
dynamic data structure: items may be added to it or deleted from it at will.
A programmer need not worry about how many items a program will have to accommodate in
advance. This allows us to write robust programs which require much less maintenance.
The linked allocation has the following draw backs:
1.No direct access to a particular element.
2.Additional memory required for pointers.
Linked list are of 3 types:
1.Singly Linked List
2.Doubly Linked List
3.Circularly Linked List

Stack using Linked list


We can represent a stack using a one way or singly linked list. In a stack push and pop operations
are performed at one end called top. We can perform similar operations at one end of list using top
pointer. The linked stack looks as shown in figure.

Applications of Stack
• Evaluating Arithmetic Expression: Stack is used to convert an infix expression into
postfix/prefix form. Stack is used to evaluate a postfix expression.
• Balancing the symbols (Parenthesis): Stack is used to evaluate a postfix expression.
• In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
• Function Calls: During a function call the return address and arguments are pushed onto a
stack and on return they are popped off

Converting and evaluating Algebraic expressions: An algebraic expression is a legal


combination of operators and operands. Operand is the quantity on which a mathematical
operation is performed. Operand may be a variable like x, y, z or a constant like 5, 4, 6 etc.
Operator is a symbol which signifies a mathematical or logical operation between the operands.
Examples of familiar operators include +, -, *, /, ^ etc.
An algebraic expression can be represented using three different notations. They are infix,
postfix and prefix notations:
Infix: It is the form of an arithmetic expression in which we fix (place) the arithmetic operator
in between the two operands. Example: A + B
Prefix: It is the form of an arithmetic notation in which we fix (place) the arithmetic operator
before (pre) its two operands. The prefix notation is called as polish notation. Example: + A B
Postfix: It is the form of an arithmetic expression in which we fix (place) the arithmetic
operator after (post) its two operands. The postfix notation is called as suffix notation and is
also referred to reverse polish notation. Example: A B +

Converting Infix to Postfix Expression (an example)


The method of converting infix expression A + B * C to postfix form is:
A + B * C Infix Form
A + (B * C) Parenthesized expression
A + (B C *) Convert the multiplication
A (B C *) + Convert the addition
A B C * + Postfix form
The rules to be remembered during infix to postfix conversion are:
1. Parenthesize the expression starting from left to light.
2. During parenthesizing the expression, the operands associated with operator having higher
precedence are first parenthesized. For example in the above expression B * C is
parenthesized first before A + B.
3. The sub-expression (part of expression), which has been converted into postfix, is to be
treated as single operand.
4. Once the expression is converted to postfix form, remove the parenthesis.

Problem 1. Give postfix form for A + [ (B + C) + (D + E) * F] / G


Solution. Evaluation order is
A + { [ (BC +) + (DE +) * F ] / G}
A + { [ (BC +) + (DE + F *] / G}
A + { [ BC + DE + F * +] / G}
A + {BC + DE + F *+ G /}
ABC + DE + F * + G / + Postfix Form

The three important features of postfix expression are:


1. The operands maintain the same order as in the equivalent infix expression.
2. The parentheses are not needed to designate the expression unambiguously.
3. While evaluating the postfix expression the priority of the operators is no longer relevant.
We consider five binary operations: +, -, *, / and $ or ↑ (exponentiation). For these binary
operations, the following in the order of precedence (highest to lowest):

Problem 2. Give postfix form for (A + B) * C / D + E ^ A / B


Solution. Evaluation order is
[(AB + ) * C / D ] + [ (EA ^) / B ]
[(AB + ) * C / D ] + [ EA ^ B / ]
[(AB + ) C * D / ] + [ (EA ^) B / ]
(AB + ) C * D / (EA ^) B / +
AB + C * D / EA ^ B / + Postfix Form

Algorithm

Suppose P is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression Q. Besides operands and operators, P (infix notation) may also
contain left and right parentheses. We assume that the operators in P consists of only
exponential ( ^ ), multiplication ( * ), division ( / ), addition ( + ) and subtraction ( - ). The
algorithm uses a stack to temporarily hold the operators and left parentheses. The postfix
expression Q will be constructed from left to right using the operands from P and operators,
which are removed from stack. We begin by pushing a left parenthesis onto stack and adding
a right parenthesis at the end of P. the algorithm is completed when the stack is empty.
1. Push “(” onto stack, and add “)” to the end of P.
2. Scan P from left to right and repeat Steps 3 to 6 for each element of P until the stack is
empty.
3. If an operand is encountered, add it to Q.
4. If a left parenthesis is encountered, push it onto stack.
5. If an operator ⊗ is encountered, then:
(a) Repeatedly pop from stack and add P each operator (on the top of stack), which has the
same precedence as, or higher precedence than ⊗.
(b) Add ⊗ to stack.
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from stack and add to P (on the top of stack until a left parenthesis is
encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to P.]
7. Exit.
Note. Special character ⊗ is used to symbolize any operator in P.

Problem 3. Consider the following arithmetic infix expression P


P=A+(B/C-(D*E^F)+G)*H
Table below shows the character (operator, operand or parenthesis) scanned, status of the stack
and postfix expression Q of the infix expression P.
Procedure to convert from infix expression to postfix expression is as follows:
1. Scan the infix expression from left to right.
2. a) If the scanned symbol is left parenthesis, push it onto the stack.
b) If the scanned symbol is an operand, then place directly in the postfix expression (output).
c) If the symbol scanned is a right parenthesis, then go on popping all the items from the stack
and place them in the postfix expression till we get the matching left parenthesis.
d) If the scanned symbol is an operator, then go on removing all the operators from the stack
and place them in the postfix expression, if and only if the precedence of the operator which is
on the top of the stack is greater than (or greater than or equal) to the precedence of the scanned
operator and push the scanned operator onto the stack otherwise, push the scanned operator
onto the stack.

You might also like