Stack

780 views

Published on

data structure

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
780
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Stack

  1. 1. STACK
  2. 2. 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, mustused 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 tobe removed
  3. 3. Examples:
  4. 4. Characteristics of a StackStructure• A stack is a collection of elements, which can bestored and retrieved one at a time.• Elements are retrieved in reverse order of their timeof storage, i.e. the latest element stored is the nextelement 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 untilthe latest element (usually referred to as the topelement) has been retrieved.
  5. 5. ▓ 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 pusheddue to absence of space)
  6. 6. 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.
  7. 7. 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 holdselements less than its capacity, the status of stack isknown as stack underflow.TOP=NULL▓ Stack Overflow░ When the stack contains equal number of elementsas per its capacity and no more elements can beadded, the status of stack is known as stack overflow.TOP=MAXSTK
  8. 8. How the stack routines workempty stack; push(a), push(b); popempty stacktop = 0apush(a)top = 1bapush(b)top = 2apop()top = 1
  9. 9. 2Top0123
  10. 10. 2Top0123Poped an item
  11. 11. 2Top012342
  12. 12. 2Top012334
  13. 13. 2Top012352STACK OVERFLOW
  14. 14. Operation performedOn stack• Tarversing• Insertion• Deletion
  15. 15. Algorithm for Insertion• Start• If TOP()=maxstack();Return TRUEExit• Top=Top+1;• Top=Top[new value];• Exit
  16. 16. Algorithm for deletion• Start• If TOP()=isempty();return TRUE;exit• Top=Top-1;• exit
  17. 17. Algorithm for traversing• Start• Print maxstack();• exit
  18. 18. EVALUATIONOFEXPRESSION
  19. 19. Expression evaluation• An expression is a series of operators andoperands• 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 leftto right
  20. 20. • Order in which the expression is evaluated is:– If the expression has parenthesis, then they areevaluated first– Exponential (^) is given highest priority– Multiplication (*) and division (/) have the nexthighest priority– Addition (+) and subtraction (-) have the lowestpriority• Evaluation of ExpressionSteps to evaluate the following expression:(2^3 + 6) * 2 – 9 / 3= (8 + 6) * 2 – 9 / 3= 14 * 2 – 9 / 3= 28 – 3= 25
  21. 21. • Stacks are useful in evaluation of arithmeticexpressions. Consider the expression• 5 * 3 +2 + 6 * 4The expression can be evaluated by firstmultiplying 5 and 3, storing the result inA, adding 2 and A, saving the result in A. Wethen multiply 6 and 4 and save the answer in B.We finish off by adding A and B and leaving thefinal answer in A.• A = 15 2 + = 17• B = 6 4 * = 24• A = 17 24 + = 41
  22. 22. 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 shortlyshow how this form can be generated using a stack.Basically there are 3 types of notations for expressions. Thestandard form is known as the infix form. The other two arepostfix 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
  23. 23. Infix, Prefix (Polish) and Postfix(Reverse Polish) NotationInfix Prefix(Polish Notation)Postfix(Reverse PolishNotation)A+B +AB AB+A*B *AB AB*A/B /AB AB/A-B -AB AB-
  24. 24. Evalution of postfix expression:
  25. 25. 6523+8*+3+*
  26. 26. Infix to Postfix Conversion**Convert infix expressionA+B*C+(D*E+F)*Ginto postfixA + B * C + ( D * E + F ) * G1. Scanned from left to right. Firstoperand read is A and passed tooutputStackOutput: A
  27. 27. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G2. Next the ‘+’ operator is read, at thisstage, stack is empty. Therefore nooperators are popped and ‘+’ ispushed into the stack. Thus thestack and output will be:+StackOutput: A
  28. 28. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G3. Next the ‘B’ operand is read andpassed to the output. Thus the stackand output will be:+StackOutput: AB
  29. 29. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G4. Next the ‘*’ operator is read, Thestack has ‘+’ operator which haslower precedence than the ‘*’operator. Therefore no operatorsare popped and ‘*’ is pushed intothe stack. Thus the stack and outputwill be:*+StackOutput: AB
  30. 30. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G5. Next the ‘C’ operand is read andpassed to the output. Thus the stackand output will be:*+StackOutput: ABC
  31. 31. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G6. Next the ‘+’ operator is read, Thestack has ‘*’ operator which hashigher precedence than the ‘+’operator. The Stack is popped andpassed to output. Next stack has ‘+’operator which has sameprecedence than the ‘+’ operator.The Stack is popped and passed tooutput. Now stack is empty,therefore no operators are poppedand ‘+’ is pushed into the stack.Thus the stack and output will be:+StackOutput: ABC*+
  32. 32. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G7. Next the left parenthesis ‘(’ isread, Since all operators have lowerprecedence than the leftparenthesis, it is pushed into thestack. Thus the stack and output willbe:(+StackOutput: ABC*+
  33. 33. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G8. Next the ‘D’ operand is read andpassed to the output. Thus the stackand output will be:(+StackOutput: ABC*+D
  34. 34. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G9. Next the ‘*’ operator is read. Now,left parenthesis ‘(‘ has higherprecedence than ‘*’; it can not bepopped from the stack until a rightparenthesis ‘)’ has been read. Thusthe stack is not popped and ‘*’ ispushed into the stack. Thus thestack and output will be:*(+StackOutput: ABC*+D
  35. 35. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G10.Next the ‘E’ operand is read andpassed to the output. Thus the stackand output will be:*(+StackOutput: ABC*+DE
  36. 36. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G11.Next the ‘+’ operator is read, Thestack has ‘*’ operator which hashigher precedence than the ‘+’operator. The Stack is popped andpassed to output. Next stack hasleft parenthesis ‘(’ which has notbeen popped and ‘+’ operator ispushed into the stack. Thus thestack and output will be:+(+StackOutput: AB*+DE*
  37. 37. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G12.Next the ‘F’ operand is read andpassed to the output. Thus the stackand output will be:+(+StackOutput: ABC*+DE*F
  38. 38. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G13.Next the ‘)’ has encountered nowpopped till ‘( ‘ and passed to theoutput. Thus the stack and outputwill be:+StackOutput: ABC*+DE*F+
  39. 39. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G14.Next the ‘*’ operator is read, Thestack has ‘+’ operator which haslower precedence than the ‘*’operator. Therefore no operatorsare popped and ‘*’ is pushed intothe stack. Thus the stack and outputwill be:*+StackOutput: ABC*+DE*F+
  40. 40. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G15.Next the ‘G’ operand is read andpassed to the output. Thus the stackand output will be:*+StackOutput: ABC*+DE*F+G
  41. 41. Example (Infix to Postfix Conversion)A + B * C + ( D * E + F ) * G16.The end of expression isencountered. The operators arepopped from the stacked andpassed to the output in the samesequence in which these arepopped. Thus the stack and outputwill be:StackOutput: ABC*+DE*F+G*+
  42. 42. 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, andpostfix• It is common to evaluate an expression by using a postfixformat

×