M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.

Slides:



Advertisements
Similar presentations
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CHAPTER 7 Queues.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 3 Introduction to Collections – Stacks Modified
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
CSC 231 Stacks 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Stacks and Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
Stacks and Queues.
Building Java Programs
Building Java Programs Chapter 14
Stacks.
Stacks, Queues, and Deques
Stacks and Queues.
slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
slides created by Marty Stepp
Chapter 5 Stack (part 1).
slides created by Marty Stepp
Stacks, Queues, and Deques
Stacks and Queues.
A type is a collection of values
Presentation transcript:

M180: Data Structures & Algorithms in Java Stacks Arab Open University 1

Abstract Data Types (ADTs) ADT: A specification of a collection of data & the operations that can be performed on it. Stack: Retrieves elements in the reverse of the order they were added. 2 stack top3 2 bottom1 pop, peek push

Stacks Stack: A collection based on the principle of adding elements and retrieving them in the opposite order. –Last-In, First-Out ("LIFO") –Elements are stored in order of insertion. We do not think of them as having indexes. –Client can only add/remove/examine the last element added (the "top"). Basic stack operations: –push: Add an element to the top. –pop: Remove the top element. –peek: Examine the top element. 3

4 Conceptual View of a Stack bottom of stack (old) top of stack New object is added as the new top element of the stack Adding an element new top bottom

5 Conceptual View of a Stack Object is removed from the top of the stack Removing an element top bottom new top bottom

6 Stack Operations push: add an element at the top of the stack pop: remove the element at the top of the stack peek: examine the element at the top of the stack It is not legal to access any element other than the one that is at the top of the stack!

7 Java Interfaces Abstract method : a method that does not have an implementation, i.e. it just consists of the header of the method: Return type method name (parameter list)

8 Java Interfaces Java has a programming construct called an interface that we use to formally define what the operations on a collection are in Java. Java interface: a list of abstract methods and constants –Must be public –Constants must be declared as final static

9 Java Interface for Stack ADT public interface StackADT { // Adds one element to the top of this stack public void push (T element); // Removes and returns the top element from this stack public T pop( ); // Returns without removing the top element of this stack public T peek( ); // Returns true if this stack contains no elements public boolean isEmpty( ); // Returns the number of elements in this stack public int size( ); // Returns a string representation of this stack public String toString( ); }

10 Generic Types What is this in the interface definition? It represents a generic type –For generality, we can define a class (or interface) based on a generic type rather than an actual type –Example: we define a Stack for objects of type T The actual type is known only when an application program creates an object of that class

Stack limitations You cannot loop over a stack in the usual way. Stack s = new Stack ();... for (int i = 0; i < s.size(); i++) { do something with s.get(i); } Instead, you pull elements out of the stack one at a time. –common idiom: Pop each element until the stack is empty. while (!s.isEmpty()) { do something with s.pop(); }

12 Array implementation of stacks To implement a stack, items are inserted and removed at the same end (called the top). Efficient array implementation requires that the top of the stack be towards the center of the array, not fixed at one end. To use an array to implement a stack, you need both the array itself and an integer. The integer tells you either: –Which location is currently the top of the stack, or –How many elements are in the stack

13 An Array-Based Implementation An array that implements a stack; its first location references (a) the top of the stack; (b) the bottom of the stack

14 Pushing and popping If the bottom of the stack is at location 0, then an empty stack is represented by top = -1 or count = 0. To add (push) an element, either: –Increment top and store the element in stk[top], or –Store the element in stk[count] and increment count. To remove (pop) an element, either: –Get the element from stk[top] and decrement top, or –Decrement count and get the element in stk[count] top = 3 or count = stk:

15 After popping When you pop an element, do you just leave the “deleted” element sitting in the array? The surprising answer is, “it depends” –In Java, if the array contains objects, you should set the “deleted” array element to null –Why? To allow it to be garbage collected! top = 2 or count = stk:

Defining Stack Operations public class AStack implements StackADT { Integer arr[]; int top,size; AStack(int n) { size = n; top = -1; arr = new Integer[size]; } public boolean isEmpty() { if(top == -1) return true; else return false; } << Continue 16

Defining Stack Operations – Cont’ public void pop() { if(top>=0) { System.out.println("the deleted element is "+arr[top]); } else { System.out.println("stack is empty"); } } << Continue 17

Defining Stack Operations – Cont’ public void push(T element) { if(top==size-1) { System.out.println("stack over flow"); } else { top=top+1; arr[top]=(Integer)element; System.out.println("added succesfully"); } } public T top() { if(top>=0) return (E)arr[top]; else return null; } 18

19 Error checking There are two stack errors that can occur: –Underflow: trying to pop (or peek at) an empty stack –Overflow: trying to push onto an already full stack For underflow, you should throw an exception –If you don’t catch it yourself, Java will throw an ArrayIndexOutOfBounds exception –You could create your own, more informative exception For overflow, you could do the same things –Or, you could check for the problem, and copy everything into a new, larger array

Complete Stack Code See the attached MS Word document.MS Word document 20