Stacks Chapter 4 1 4 1 Stacks A

  • Slides: 31
Download presentation
Stacks Chapter 4 1

Stacks Chapter 4 1

4. 1 Stacks �A stack is a linear data structure that can be accessed

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

�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

�The delimiters matching algorithm in C++ 4

5

5

6

6

7

7

�A stack is a last in, first out (LIFO) abstract data type and data

�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

9

Basic Operations on a Stack �Initialize. Stack: �Initializes the stack to an empty state.

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

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) �

�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

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

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 Pop operation 15 Top

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

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

Head count Top Data nodes Top (a) Physical (a) Conceptual 17

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 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 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

�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

�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 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.

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.

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

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

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

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 + �

� 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

� 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

30

31

31