SlideServe
Menu
Préférences
© SlideServe · v2026

CSE 246 Data Structures and Algorithms

arlo avatar
9 diapositives · 208 Views · Sep 07, 2014

This lecture explores the evaluation of postfix expressions using a stack, detailing the algorithm step-by-step. It describes how to scan a postfix string, pushing operands onto the stack and applying operators to the top two operands as they are encountered. The stack structure is implemented in Java, highlighting the process of defining a Stack interface and creating a concrete class to handle various stack operations. The instructional content is aimed at enhancing understanding of stack operations and abstract data types in programming.

CSE 246 Data Structures and Algorithms - slide 1
01 / 9CSE 246 Data Structures and Algorithms
1 / 17

Télécharger la présentation

CSE 246 Data Structures and Algorithms
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CSE 246Data Structures and Algorithms Spring2011 Lecture#12

  2. Evaluating Postfix • Each operator in a postfix expression refers to the previous two operands. • Each time we read an operand, we push it on a stack. • When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack. Quratulain

  3. Postfix Expression evaluation Algorithm In english:Scan the Postfix string from left to right. • Initialize an empty stack. • If the scanned character is an operand, add it to the stack. If the scanned character is an operator, there will be at least two operands in the stack. • If the scanned character is an Operator, then we store the top most element of the stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp. Let the result of this operation be retVal. Pop the stack and Push retVal into the stack. • Repeat this step till all the characters are scanned. • After all characters are scanned, we will have only one element in the stack. Return topStack as result Quratulain

  4. Algorithm for postfix evalution Opndstk = the emty stack; While (not end of input) { Symb = next input character; If (symb is an operand) Push(opndstk,symb); Else { Opnd2=pop(opndstk); Opnd1=pop(opndstk); Value = result of applying symb to opnd1 and opnd2; Push(opndstk, value); } } Return (pop(opndstk)); Quratulain

  5. A Stack Interface in Java • The stack data structure is included as a "built-in" class in the java.util package of Java. • it is instructive to learn how to design and implement a stack "from scratch.“ • Implementing an abstract data type in Java involves two steps • Define interface • Define exceptions for any error conditions that can arise. • Provide a concrete class that implements the methods of the interface associated with that ADT. Quratulain

  6. Stack interface public interface Stack <E> { public int size(); public booleanisEmpty(); public E top() throws EmptyStackException; public void push( E element); public E pop() throws EmptyStackException; } Quratulain

  7. Quratulain

  8. Quratulain

  9. Implementing a Stack with a Generic Linked List Left as homework Quratulain