Sei sulla pagina 1di 22

Data Structures

IS3C03
Abstract Data Type
Abstract Data Type
 Abstract Data Type (ADT) is a useful tool for specifying
the logical properties of a data type.
 A data type is a collection of values and a set of operations
on those values.
 An abstract data type (ADT) is an object with a generic
description independent of implementation details.
 This description includes a specification of the components
from which the object is made and also the behavioral
details of the object.
Cont . . .
 ADT consists of two parts:
• A Value definition and
• An Operator definition.
 Value definition defines the collection of values for the
ADT and consists of two parts:
• Definition clause
• Condition clause.
Example
/* Value definition */
abstract typedef <integer, integer> RATIONAL;
condition RATIONAL[1] <> 0;
/* Operator definition */
abstract RATIONAL makerational (a, b)
int a, b;
precondition b <> 0;
postcondition makerational [0] == a;
makerational [1] == b;

abstract RATIONAL add (a, b)


RATIONAL a, b;
postcondition add [1] == a[1] * b[1];
add [0] == a[0] * b[1] + b[0] * a[1];
Cont . . .
 Value definition for the ADT RATIONAL states that a
RATIONAL value consists of two integers, the second of
which does not equal 0.
 Keywords abstract typedef introduce a value definition, and
the keyword condition is used to specify any conditions on
the newly defined type.
 Definition clause is required, but the condition clause may
not be necessary for every ADT.
 In operator definition each operator is defined as an
abstract function with three parts:
• A header
• Optional postconditions and preconditions
Stack
 A stack is an ordered collection of items into which new
items may be inserted and from which items may be deleted
at one end, called the top of the stack.
 The definition of the stack provides for the insertion and
deletion of items, so that stack is dynamic, constantly
changing object.
 A single end of the stack is designated as top of the stack.
 New items may be put on top of the stack or items which
are at the top of the stack may be removed.
 Last item inserted will be on top of stack.
 Since deletion is done from the same end, last item inserted
is the first item to be deleted out, so last in first out (LIFO)
Cont . . .
 Various operations that can be performed on stack are:
• Insert an item
• Delete an item
• Display the contents.
Insert / push operation
 If an item is to be inserted into stack(push), top of the
stack is to be incremented and then item is to be inserted.
 But during insertion overflow condition has to be checked,
whether stack is full or not.
 Example:
if (top == STACK_SIZE – 1)
{ printf (“stack overflow”); return;}
s [++top] = item;
Deletion / pop operation
 Deleting an item from the stack is called pop operation
 This can be achieved by first accessing the top item and
then decrementing top by one.
 Each time, the item is deleted, top is decremented and
finally the top will be -1, i.e. empty stack.
 pop operation can be performed only when the stack is not
empty.
 Example:
if (top == -1)
{ return -1; }
item = s[top --]; return item;
Display operation
 Contents of the stack are displayed from the bottom of the
stack, till the top of the stack is reached.
 Contents of the stack can be displayed only, when it is not
empty.
 Example:
if (top == -1)
{ printf(“stack is empty”); }
for ( i=0; i<=top; i++)
{ printf ( “%d”, s[ i ]); }
An Example: INFIX, POSTFIX, PREFIX
 Section examines a major application that illustrates the
different types of stacks and various operations and
function defined upon them.
 Example: sum of A and B
• Above statement is written as A + B, i.e. applying operator +, to
the operands A and B.
• This representation is called infix.
• There are two alternate notations for expressing the sum of A
and B using the symbols A, B and +.
+ A B -- prefix
A B + -- postfix
Cont . . .
 The prefixes pre, post, in refer to the relative position of
the operator with respect to the two operands.
 Example: Evaluation of the expression
A+B*C
A + BC *
A BC * + -- Post fix
 Another Example:
(A + B) * C
(AB +) * C
(AB +) C *
AB + C * -- Post fix
Cont . . .
 Example: Evaluation of the expression to prefix
A+B*C
A + * BC
+ A * BC -- prefix
 When unparenthesized operators of the same precedence
are scanned, the order is assumed to be left to right.
 In the case of exponentiation the order is assumed to be
from right to left.
 Example: A + B + C means (A + B) + C
A $ B $ C means A $ (B $ C)
Evaluation of postfix expression
 While evaluating an infix expression, it is required to scan
from left to right repeatedly.
 This repetition can be avoided if the infix expression is
converted to its corresponding postfix expression.
 Evaluation of a postfix is very simple.
 Steps to be followed are:
• Scan from left to right till an operator is encountered.
• Scan backward to obtain immediate two left operand.
• Perform the indicated operation.
• Replace the operands and the operator by the result.
Example
6 3 2 - 5 * +
6 (3 – 2) 5 * +
6 ((3 – 2) * 5) +
(6 + ((3 – 2) * 5))
 Using stack this can be implemented as:
• If the scanned symbol is an operand, push it on to the stack
• If the scanned symbol is an operator, pop two operands from the
stack.
• First operand popped is an operand2 and the second operand is an
operand1.
• Statements are op2 = pop ( &top, s); op1 = pop ( &top, s);
• Perform the desired operation. res = op1 op op2
Cont . . .
• Push the result on to the stack
• Repeat the above procedure till the end of input is encountered.
 Following algorithm evaluates the valid postfix expression
while end of input is not reached do
{ symbol = nextchar()
if (symbol is an operand)
push (symbol, top, s);
else
op2 = pop(top, s); op1 = pop(top, s);
res = op1 op op2; push (res, top, s);
endif }
Conversion from infix to prefix
 An expression where an operator precede the two operands
is a prefix expression.
 Example: Evaluation of the expression to prefix
A+B*C
A + * BC
+ A * BC -- prefix
Evaluation of Prefix expression
 Start scanning the string from the right one character at a
time.
 If it is an operand, push it in stack.
 If it is an operator, pop opnd1, opnd2 and perform the
operation, specified by the operator. Push the result in the
stack.
 Repeat these steps until array of input prefix strings ends.
Example
-*+4325
- * ( 4 + 3 )2 5
- (( 4 + 3 ) * 2) 5
(( 4 + 3 ) * 2) – 5  9
Recursion
 Recursion is a powerful programming tool.
 An important facility that is available in most of the
programming languages.
 Recursion is the name given for expressing anything in
terms of itself.
 A function, which contains a call to itself or a call to
another function, which eventually causes the first function
to be called, is known as a recursive function.
 An expression, a language construct or a solution to a
problem can be expressed using recursion.
Cont . . .
 Example:
• An arithmetic expression can be defined recursively as follows:
• a is an arithmetic expression where a is a variable or a numeric constant
• If E is an arithmetic expression then E op E, +E, -E, (E) are arithmetic
expressions where op is an arithmetic operator such as +, -, *, /, %
 Example below are arithmetic expressions constructed
from the above definition
A
(A+B) * (A-B)
A + 2.5/5

Potrebbero piacerti anche