Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Stack (part 1).

Similar presentations


Presentation on theme: "Chapter 5 Stack (part 1)."— Presentation transcript:

1 Chapter 5 Stack (part 1)

2 What is Stack? A stack is a data structure with the property that only the top element of the stack is accessible A stack is a list of homogeneous elements In a stack, addition and deletion of elements occurs only at one end, called the top of the stack A stack is a Last In First Out (LIFO) data structure

3 Last In First Out (LIFO) algorithm
In LIFO data structure, top element of a stack is the last element to be added to the stack Elements are added and removed from one end known as top The item that is added last will be removed first.

4 Add and remove element in stack
occurs at the top of the stack Adding element Remove element

5 Example of stack: Stack of coins Stack of books Stack of trays
Stack of boxes

6 Stack Operations Assume that there are 5 boxes to be added into a stack namely A, B, C, D and E. Initially, the stack is empty.

7 Stack Operations (cont..)

8 Stack Specification The Java API includes a Stack class as part of the package java.util. The ADT specification is quite simple and there are only five (5) operations ina ddition to the constructor.

9 The Stack ADT Description:
This ADT describes a dynamic stack whose element type is left unspecified. Dynamic means the size is not fixed. Properties: Stack is LIFO data structure. All accesses to elements are done from the element referenced by top. All stack operations refer to the topmost element in the stack. Insertion / Addition of new elements are done “above” the top element.

10 The Stack ADT Attributes: size: the number of elements in the stack.
top: the topmost element of the stack. (the top is set to null when the stack is empty)

11 The Stack ADT (cont..) Operations Stack( ) : the constructor
Pre-condition : None Task : to initialize the stack attributes Post-condition: Size is zero Top refers to null Returns : Nothing

12 The Stack ADT (cont..) Example: Start with an empty stack
The bottom of the stack is at index 0 and the topmost element is at index size ( ) -1. Coding example: Stack name = new Stack(); // this statement will create a new stack named name and initally the stack is empty. So, the value of top is null.

13 The Stack ADT (cont..) Operations push (data_type element )
Pre-condition : None Task : to insert / add a new element onto the top of the stack Post-condition: Element is placed on top of the stack Size is incremented by 1 Top refers to the element pushed Returns : Nothing

14 EXAMPLE Add an object to the top of the stack name.push(“Ali”);
name.push(“Siti”); Top Ali [0] name Siti [1] Ali [0] name

15 The Stack ADT (cont..) Operations pop ( )
Pre-condition : isEmpty() is false Task : remove and return the element at top Post-condition: The top element id no longer in the stack Size is decremented by 1 Top refers to the element below the previous topmost element or null if the stack is empty Returns : the element removed

16 Example Remove an object from the top of the stack name.pop();
This statement will return “Sti”. After Before Siti [1] Ali [0] name Ali [0] name

17 REMOVE ALL OBJECTS FROM THE STACK
It can be done by repeatedly calling pop() until the stack is empty while (!name.isEmpty() { System.out.println(name.pop()); } After the loop is completely iterated, the stack is now empty.

18 The Stack ADT peek ( ) Pre-condition : isEmpty() is false
Task : return the element at top Post-condition : The stack is unchanged Returns : the element referenced by top

19 Example To retrieve the object from the top of the stack (without removing the object) and display the object: System.out.println (name.peek());

20 The Stack ADT size ( ) Pre-condition : none
Task : determine teh number of elements in the stack Post-condition : the stack is unchanged Returns : the number of elements in the stack

21 The Stack ADT isEmpty ( ) Pre-condition : none
Task : determine if the stack has any elements in it Post-condition : the stack is unchanged Returns : true if the stack is empty (size = 0), false if the stack is not empty

22 Example To determine whether a stack is empty (or how many elements are in it) if (!name.isEmpty()) System.out.println(“Size=“+ name.size()); else System.out.println(“The stack is empty”);


Download ppt "Chapter 5 Stack (part 1)."

Similar presentations


Ads by Google