1 / 31

Stacks

Stacks. Chapter 4. 4.1 Stacks . A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called LIFO. A stack is defined in terms of operations that change its status and operations that check this status.: Clear() isEmpty ()

kimi
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks Chapter 4

  2. 4.1 Stacks • A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. • Its called LIFO. • A stack is defined in terms of operations that change its status and operations that check this status.: • Clear() • isEmpty() • Push(el) • Pop() • topEl()

  3. Stack is useful when data have to be stored and then retrieved in reverse order. • An application example of the stack is in matching delimiters in a program. • parentheses, square brackets, curly brackets, and comment delimiters. • Ex: a= b + (c-d) * (e-f) • The program could have nested delimitiers.

  4. The delimiters matching algorithm in C++

  5. A stack is a last in, first out (LIFO) abstract data type and data structure . A stack can have any abstract data type as an element ,but is characterized by only two fundamental operations: push and pop. • Elements are removed from the stack in the reverse order to the order of their addition :therefore, the lower elements are those that have been on the stack the longest.

  6. Basic Operations on a Stack • InitializeStack: • Initializes the stack to an empty state. • DestroyStack: • Removes all the elements from the stack, leaving the stack empty. • IsEmptyStack: • Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false. • IsFullStack: • Checks whether the stack is full. If full, it returns true; otherwise, it returns false

  7. Basic Operations on a Stack • Push: • Add new element to the top of the stack • The input consists of the stack and the new element. • Prior to this operation, the stack must exist and must not be full • Top: • Returns the top element of the stack. • Prior to this operation, the stack must exist and must not be empty. • Pop: • Removes the top element of the stack. • Prior to this operation, the stack must exist and must not be empty.

  8. Operations: • initialize • destroy • build from given data (set of elements) • check if it is empty • get the total size of the stack • add an element to the top of the stack [PUSH] • delete an element from the top of the stack [POP] • get the data from the element at the top of the stack • update the data of the top element • print the data of the top element • print the entire stack • Infix, Postfix and Prefix

  9. In stack , • no search, • no adding in arbitrary positions, • no sorting, • no access to anything beyond the top element.

  10. Check for enough room, (no overflow) Data Push Top Top operation

  11. Check if empty, (no underflow) Data Top Pop Top operation

  12. Check if empty, (no underflow) Data Top Top Stack top operation

  13. Head count Top Top Data nodes (a) Physical (a) Conceptual

  14. Stack Linked List Implementation Stack count <integer> top <node pointer> end stack node data <datatype> next <node pointer> end node count top Stack head structure data next Stack node structure

  15. stack ? ? count top (a) Before Create stack 0 count top (b) After Create Stack Algorithms • Create stack algorithm createStack Initializes metadata for a stack. stack.head= null • stack.count = 0 • Return end createStack

  16. algorithm pushStack Insert (push) data into a new node in the liked list. Postdata have been pushed in stack Return true if successful, false if memory overflow • If (stack full) • successes = false • else • allocate (newptr) • newptr->data = data • newptr->next = stack.top • stack.top = newptr • stack.count = stack.count + 1 • successes = true • end if • Return successes • end pushStack • Push stack

  17. algorithm popStack This algorithm pops the item on the top of the stack and returns it to the user Postdata have been returned to calling algorithm Return true if successful, false if underflow • If (stack empty) • successes = false • else • dptr = stack.top • dataout = stack.top->data • stack.top = stack.top->next • stack.count = stack.count – 1 • Recycle (dptr) • successes = true • end if • return successes • end popStack • Pop stack

  18. Stack Top algorithm Stacktop This algorithm receives the data from the top of the stack without changing the stack. Post data have been returned to calling algorithm Return true if data returned, false if underflow • If (stack empty) • successes = false • else • dataout = stack.top->data • successes = true • end if • return successes • end Stacktop

  19. Empty Stack algorithm emptyStack Determines if stack is empty and returns a Boolean. Post returns stack status Return Boolean, true: stack empty, false: stack contains data • If (stack not empty) • result = false • else • result = true • end if • return result • end emptyStack

  20. Full Stack algorithm fullStack Determines if stack is full and returns a Boolean. Post returns stack status Return Boolean, true: stack full, false: stack is empty • If (memory available) • result = false • else • result = true • end if • return result • end fullStack

  21. Stack Count algorithm Stackcount Returns the number of elements currently in stack. Post returns stack count Return integer count of number of elements in stack • return (stack.count) • end Stackcount

  22. Destroy Stack algorithm destroyStack This algorithm releases all nodes back to dynamic memory • Loop (stack.top not null) • temp = stack.top • Stack.top = stack.top->next • recycle (temp) • end loop • Stack.count = 0 • return • end destroyStack

  23. Infix, Postfix and Prefix • Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands. • Infix notation: X + Y • Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer." • Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction.

  24. Postfix notation (also known as "Reverse Polish notation"): X Y + • Operators are written after their operands. The infix expression given above is equivalent to A B C + * D / The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication. Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C". We can add (totally unnecessary) brackets to make this explicit: ( (A (B C +) *) D /) Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the "/" uses the result of the multiplication and the "D".

  25. Prefix notation (also known as "Polish notation"): + X Y • Operators are written before their operands. The expressions given above are equivalent to / * A + B C D As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear: (/ (* A (+ B C) ) D) • Although Prefix "operators are evaluated left-to-right", they use values to their right, and if these values themselves involve computations then this changes the order that the operators have to be evaluated in. In the example above, although the division is the first operator on the left, it acts on the result of the multiplication, and so the multiplication has to happen before the division (and similarly the addition has to happen before the multiplication). 

More Related