stack

44
STACK

Upload: maleeha-khawar-hashmie

Post on 18-May-2015

544 views

Category:

Documents


2 download

DESCRIPTION

data structure

TRANSCRIPT

Page 1: Stack

STACK

Page 2: Stack

What is a stack?• In array insertion and deletion can take place at both

end, i.e. start and end,

• But if insertion and deletion are restricted from one end, must used STACKS and QUEUES.

• Stack is a special type of data structure.

• Stores a set of elements in a particular order

• Compared to a container

• Stack principle: LAST IN FIRST OUT

• = LIFO

• It means: the last element inserted is the first one to be removed

Page 3: Stack

Examples:

Page 4: Stack

Characteristics of a Stack Structure• A stack is a collection of elements, which can be

stored and retrieved one at a time.

• Elements are retrieved in reverse order of their time of storage, i.e. the latest element stored is the next element to be retrieved.

• A stack is sometimes referred to as a Last-In-First-Out (LIFO) or First-In-Last-Out (FILO) structure. Elements previously stored cannot be retrieved until the latest element (usually referred to as the 'top' element) has been retrieved.

Page 5: Stack

▓ Stack Terminologies▒ Push (inserting An element In Stack)

▒ Pop (Deleting an element in stack)

▒ Top ( The last entered value)

▒ Max Stack ( maximum value, a stack can hold)

▒ Isempty ( either stack has any value or not)

• Isempty()=True-> empty

• Isempty()=False-> contain elements

▒ Overflow (stack is full, elements can’t be pushed due to absence of space)

Page 6: Stack

Operations perform on stack

Primary operations: Push and Pop Push◦ Add an element to the top of the stack. Pop◦ Remove the element at the top of the stack.

Page 7: Stack
Page 8: Stack

Stack-Related Terms▓ Top

░ A pointer that points the top element in the stack.

▓ Stack Underflow

░ When there is no element in the stack or stack holds elements less than its capacity, the status of stack is

known as stack underflow.

TOP=NULL

▓ Stack Overflow

░ When the stack contains equal number of elements as per its capacity and no more elements can be

added, the status of stack is known as stack overflow.

TOP=MAXSTK

Page 9: Stack

How the stack routines work

empty stack; push(a), push(b); pop

empty stacktop = 0

a

push(a)top = 1

b

a

push(b)top = 2

a

pop()top = 1

Page 10: Stack

2

45

54

64

Top

0

1

2

3

Page 11: Stack

2

54

64

Top

0

1

2

3

45

Poped an item

Page 12: Stack

2

54

64

Top

0

1

2

3

42

42

Page 13: Stack

2

54

64

Top

0

1

2

3

42

34

42

Page 14: Stack

2

54

64

Top

0

1

2

3

42

52

42

STACK OVERFLOW

Page 15: Stack

Operation performed On stack

• Tarversing

• Insertion

• Deletion

Page 16: Stack
Page 17: Stack

Algorithm for Insertion• Start

• If TOP()=maxstack();

Return TRUE

Exit

• Top=Top+1;

• Top=Top[new value];

• Exit

Page 18: Stack

Algorithm for deletion• Start

• If TOP()=isempty();

return TRUE;

exit

• Top=Top-1;

• exit

Page 19: Stack

Algorithm for traversing• Start

• Print maxstack();

• exit

Page 20: Stack

EVALUATION OF EXPRESSION

Page 21: Stack

Expression evaluation• An expression is a series of operators and

operands

• Operators: +, -, *, /, %, ^ (exponentiation)

• Operands: the values going to be operated

• Arithmetic expression is made up– Operands (Numeric Variables or Constants)

– Arithmetic Operators (+, -, *, /)

– Power Operator (^)

– Parentheses

• The Expression is always evaluated from left to right

Page 22: Stack

• Order in which the expression is evaluated is:– If the expression has parenthesis, then they are

evaluated first– Exponential (^) is given highest priority– Multiplication (*) and division (/) have the next highest

priority– Addition (+) and subtraction (-) have the lowest priority

• Evaluation of Expression Steps to evaluate the following expression:

(2^3 + 6) * 2 – 9 / 3

= (8 + 6) * 2 – 9 / 3

= 14 * 2 – 9 / 3

= 28 – 3

= 25

Page 23: Stack

• Stacks are useful in evaluation of arithmetic expressions. Consider the expression

• 5 * 3 +2 + 6 * 4

The expression can be evaluated by first multiplying 5 and 3, storing the result in A, adding 2 and A, saving the result in A. We then multiply 6 and 4 and save the answer in B. We finish off by adding A and B and leaving the final answer in A.

• A = 15 2 + = 17

• B = 6 4 * = 24

• A = 17 24 + = 41

Page 24: Stack

5 * 3 +2 + 6 * 4

• We can write this sequence of operations as follows:

5 3 * 2 + 6 4 * +

• This notation is known as postfix notation. We shall shortly show how this form can be generated using a stack. Basically there are 3 types of notations for expressions. The standard form is known as the infix form. The other two are postfix and prefix forms.

• Infix: The usual form, operator is between operands

• Prefix: the operator is in front of the operand(s)

• Postfix: the operand(s) is(are) in front of the operator

Page 25: Stack

Infix, Prefix (Polish) and Postfix (Reverse Polish) Notation

Infix Prefix(Polish Notation)

Postfix(Reverse Polish

Notation)

A+B +AB AB+

A*B *AB AB*

A/B /AB AB/

A-B -AB AB-

Page 26: Stack

Evalution of postfix expression:

Page 27: Stack

6523+8*+3+*

Page 28: Stack

Infix to Postfix Conversion**Convert infix expression

A+B*C+(D*E+F)*G

into postfix

A + B * C + ( D * E + F ) * G

1. Scanned from left to right. First operand read is A and passed to output

Stack

Output: A

Page 29: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

2. Next the ‘+’ operator is read, at this stage, stack is empty. Therefore no operators are popped and ‘+’ is pushed into the stack. Thus the stack and output will be:

+

Stack

Output: A

Page 30: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

3. Next the ‘B’ operand is read and passed to the output. Thus the stack and output will be:

+

Stack

Output: AB

Page 31: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

4. Next the ‘*’ operator is read, The stack has ‘+’ operator which has lower precedence than the ‘*’ operator. Therefore no operators are popped and ‘*’ is pushed into the stack. Thus the stack and output will be:

*

+

Stack

Output: AB

Page 32: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

5. Next the ‘C’ operand is read and passed to the output. Thus the stack and output will be:

*

+

Stack

Output: ABC

Page 33: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

6. Next the ‘+’ operator is read, The stack has ‘*’ operator which has higher precedence than the ‘+’ operator. The Stack is popped and passed to output. Next stack has ‘+’ operator which has same precedence than the ‘+’ operator. The Stack is popped and passed to output. Now stack is empty, therefore no operators are popped and ‘+’ is pushed into the stack. Thus the stack and output will be:

+

Stack

Output: ABC*+

Page 34: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

7. Next the left parenthesis ‘(’ is read, Since all operators have lower precedence than the left parenthesis, it is pushed into the stack. Thus the stack and output will be:

(

+

Stack

Output: ABC*+

Page 35: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

8. Next the ‘D’ operand is read and passed to the output. Thus the stack and output will be:

(

+

Stack

Output: ABC*+D

Page 36: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

9. Next the ‘*’ operator is read. Now, left parenthesis ‘(‘ has higher precedence than ‘*’; it can not be popped from the stack until a right parenthesis ‘)’ has been read. Thus the stack is not popped and ‘*’ is pushed into the stack. Thus the stack and output will be:

*

(

+

Stack

Output: ABC*+D

Page 37: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

10.Next the ‘E’ operand is read and passed to the output. Thus the stack and output will be:

*

(

+

Stack

Output: ABC*+DE

Page 38: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

11.Next the ‘+’ operator is read, The stack has ‘*’ operator which has higher precedence than the ‘+’ operator. The Stack is popped and passed to output. Next stack has left parenthesis ‘(’ which has not been popped and ‘+’ operator is pushed into the stack. Thus the stack and output will be:

+

(

+

Stack

Output: AB*+DE*

Page 39: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

12.Next the ‘F’ operand is read and passed to the output. Thus the stack and output will be:

+

(

+

Stack

Output: ABC*+DE*F

Page 40: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

13.Next the ‘)’ has encountered now popped till ‘( ‘ and passed to the output. Thus the stack and output will be:

+

Stack

Output: ABC*+DE*F+

Page 41: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

14.Next the ‘*’ operator is read, The stack has ‘+’ operator which has lower precedence than the ‘*’ operator. Therefore no operators are popped and ‘*’ is pushed into the stack. Thus the stack and output will be:

*

+

Stack

Output: ABC*+DE*F+

Page 42: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

15.Next the ‘G’ operand is read and passed to the output. Thus the stack and output will be:

*

+

Stack

Output: ABC*+DE*F+G

Page 43: Stack

Example (Infix to Postfix Conversion)

A + B * C + ( D * E + F ) * G

16.The end of expression is encountered. The operators are popped from the stacked and passed to the output in the same sequence in which these are popped. Thus the stack and output will be:

Stack

Output: ABC*+DE*F+G*+

Page 44: Stack

Summary• A stack is one of the most basic data structures in

the study.

• It only has one end for insert and delete.

• An item in a stack is LIFO while it is FIFO in a queue

• Stack is used in many hidden areas of computer systems

• One popular important application is expression evaluation

• An expression can be in three forms: infix, prefix, and postfix

• It is common to evaluate an expression by using a postfix format