Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks and Queues Chapter 4.

Similar presentations


Presentation on theme: "Stacks and Queues Chapter 4."— Presentation transcript:

1 Stacks and Queues 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

6

7

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

9

10 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

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

12 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

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

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

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

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

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

18 Stack Linked List Implementation
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

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

20 Push stack If (stack full) else end if Return successes end pushStack
algorithm pushStack 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) 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

21 Pop stack If (stack empty) else end if return successes end popStack
algorithm popStack 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) 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

22 Stack Top If (stack empty) else end if return successes end Stacktop
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

23 Empty Stack If (stack not empty) else end if return result
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

24 Full Stack If (memory available) else end if return result
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

25 Stack Count return (stack.count) end Stackcount 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

26 Destroy Stack Loop (stack.top not null) end loop Stack.count = 0
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


Download ppt "Stacks and Queues Chapter 4."

Similar presentations


Ads by Google