Stacks Chapter 4 1 4 1 Stacks A
- Slides: 31
Stacks Chapter 4 1
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. : 1. Clear() 2. is. Empty() 3. Push(el) 2 4. Pop()
�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 3 brackets, and comment delimiters. �Ex: a= b + (c-d) * (e-f) �The program could have nested
�The delimiters matching algorithm in C++ 4
5
6
7
�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 8 the reverse order to the order of their addition : therefore, the lower elements are those that have been on the stack the longest.
9
Basic Operations on a Stack �Initialize. Stack: �Initializes the stack to an empty state. �Destroy. Stack: �Removes all the elements from the stack, leaving the stack empty. �Is. Empty. Stack: �Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false. �Is. Full. Stack: �Checks whether the stack is full. If full, it returns true; otherwise, it returns false 10
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. 11
�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 entire stack � Infix, Postfix and Prefix 12
In stack , �no search, �no adding in arbitrary positions, �no sorting, �no access to anything beyond the top element. 13
Top Check for enough room, (no overflow) Data Top Push operation 14
Check if empty, (no underflow) Data Top Pop operation 15 Top
Check if empty, (no underflow) Data Top Stack top operation 16 Top
Head count Top Data nodes Top (a) Physical (a) Conceptual 17
Stack Linked List Implementation count top Stack head structure Stack count<integer> top<node pointer> end stack node data next data<datatype> next<node pointer> end node Stack node structure 18
Stack Algorithms �Create stack ? count ? top (a) Before Create stack algorithm create. Stack Initializes metadata for a stack. head = null 2 stack. count = 0 3 Return end create. Stack 0 count top (b) After Create 19
�Push stack algorithm push. Stack Insert (push) data into a new node in the liked list. Post data have been pushed in stack Return true if successful, false if memory overflow If (stack full) 1 1 else 2 1 2 3 4 5 6 3 4 successes = false allocate (newptr) newptr->data = data newptr->next = stack. top = newptr stack. count = stack. count + 1 successes = true end if Return successes end push. Stack 20
�Pop stack algorithm pop. Stack This algorithm pops the item on the top of the stack and returns it to the user Post data have been returned to calling algorithm Return true if successful, false if underflow If (stack empty) 1 1 else 2 1 2 3 4 5 6 3 4 successes = false 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 pop. Stack 21
Stack Top algorithm Stacktop This algorithm receives the data from the top of the stack without changing the stack. Post Return If (stack empty) 1 1 1 2 4 successes = false else 2 3 data have been returned to calling algorithm true if data returned, false if underflow dataout = stack. top->data successes = true end if return successes end Stacktop 22
Empty Stack algorithm empty. Stack Determines if stack is empty and returns a Boolean. Post Return If (stack not empty) 1 1 1 4 result = false else 2 3 returns stack status Boolean, true: stack empty, false: stack contains data result = true end if return result end empty. Stack 23
Full Stack algorithm full. Stack Determines if stack is full and returns a Boolean. Post Return If (memory available) 1 1 1 4 result = false else 2 3 returns stack status Boolean, true: stack full, false: stack is empty result = true end if return result end full. Stack 24
Stack Count algorithm Stackcount Returns the number of elements currently in stack. Post Return 1 returns stack count integer count of number of elements in stack return (stack. count) end Stackcount 25
Destroy Stack algorithm destroy. Stack This algorithm releases all nodes back to dynamic memory Loop (stack. top not null) 1 1 2 3 4 temp = stack. top Stack. top = stack. top->next recycle (temp) end loop Stack. count return = 0 end destroy. Stack 26
Infix, Postfix and Prefix � Infix, Postfix and Prefix notations are three different but 27 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
� Postfix notation (also known as "Reverse Polish 28 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-toright, 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".
� Prefix notation (also known as "Polish notation"): + X Y � Operators are written before their operands. The 29 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).
30
31
- Pda advance directive
- Cqueue
- Five ocean basins
- Java stacks and queues
- Types of stacks
- Stacks in data structures
- Java stack exercises
- What are stacks
- Stacks+routined
- 6 stacks
- Speedstacks com go
- The individual flattened stacks of membrane
- Stacks internet
- Coastal landforms pictures
- Angle stacks
- Summary of the red tent
- What happened in the great gatsby chapter 8
- Chapter 10 chemical reactions
- Chapter 11 study guide stoichiometry
- Chapter 9 chapter assessment chemical reactions
- Similarity ratio definition
- Chapter 6 career readiness chapter review
- 7 ionic and metallic bonding
- Chapter 9 surface water chapter assessment answer key
- Representing motion physics answers
- Chemistry the central science 14th edition
- Chapter 7 ionic and metallic bonding chapter answer key
- Chapter test a chapter 4 population ecology answer key
- Chapter 2 standardized test practice answers
- Book of philippians background
- Chapter 7 chapter assessment ionic compounds and metals
- Chapter 7 chapter assessment ionic compounds and metals