A type is a collection of values

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Stacks, Queues, and Deques
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
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.
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
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.
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.
Abstract Data Type (ADT) & Stacks
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
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.
Data Structures and Algorithms Lecture 3 Instructor: Quratulain Date: 8 th September, 2009.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Sections 3.4 Formal Specification
Chapter 14 – Exception Handling
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Objectives You should be able to describe: Interactive Keyboard Input
Yanal Alahmad Java Workshop Yanal Alahmad
Exceptions, Interfaces & Generics
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures ADT List
Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Adapted from Pearson Education, Inc.
Stacks Abstract Data Types (ADTs) Stacks
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lecture 11 Objectives Learn what an exception is.
Stacks.
Introduction to Data Structure
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
CS210- Lecture 3 Jun 6, 2005 Announcements
Java Programming: From Problem Analysis to Program Design, 4e
Corresponds with Chapter 5
Abstract Data Types Stacks CSCI 240
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of two values, true and false; the byte type consists of all integers between -128 and +127. A data type is a type + a set of operations on data of the type. For example, integers of type byte plus {+, -, *, /} comprise the data type byte. An integer array is a data type which has assign and search operations associated with it. An abstract data type is a data type solely defined in terms of a type and a set of operations on that type. Each operation is defined in terms of its input and output without specifying how the data type is implemented. A stack is an example of an ADT, defined in terms of push and pop operations. A data structure is an implementation of an ADT. The Stack ADT, for example, an be implemented by means of an array.

Levels of abstraction in data specification Any data can be defined in two formats: logical and physical. An ADT defines data in a logical format -- as a conceptual model, which is completely independent of its implementation. Consider, for example, a matrix of integers. At a logical level, we are not interested in how this matrix is implemented. The only items of interest are what type of data this matrix consists of, and what operations will be performed on this data. At a physical level, the associated data structure representing the Matrix ADT can be implemented with a different degree of abstraction by means of: a high level language, in which case the Matrix ADT can be defined as an array of integers. in assembly language (or byte code) it is represented as a sequence of words in computer memory. in machine language it is represented as a sequence of electronic bits.

The Matrix example (cont.) The ADT Matrix, in more formal terms, can de defined as a collection of data items of type MatrixData arranged as a rectangular grid, and associated with the following set of operations: Retrieve (M, row#, col#) Assign (M, row#, col#, value) To implement the ADT matrix, we can use a two-dimensional array. For example: int[ ][ ] M = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}} The retrieve and assign operations can be implemented as follows: item = M[1][2] M[2][3] = item

Representation of two-dimensional arrays in computer memory Computer memory is a linear sequence of memory cells. There are two ways to translate a two-dimensional array into a linear sequence: 1. Row by row (this is called the row-major form). To implement assign and retrieve operations, we must be able to find a particular entry. This can be done by means of the following formula (called a mapping function): (# of columns * (i - 1) + j), where i is the specified row#, and j is the specified col#. 2. Column by column (this is called the column-major form) in which case the mapping function is (# of rows * (j - 1) + i).

The Stack ADT Definition A stack is a restricted list in which entries are added and removed from the same end, called the top. This strategy is known as last-in-first-out (LIFO) strategy. Operations (methods) on stacks: push (item) Inserts item on the top of the stack pop () Removes the top item size () Returns the number of items in the stack empty () Returns true if the stack is empty full() Returns true if the stack is full ontop() Returns the top element without removing it from the stack There is a built-in class, Stack, in the java.util package, but often it is desirable to have your own implementation. Next, we discuss a specific implementation of the Stack ADT.

The Stack Interface in two versions public interface Stack { public void push (int item); public int pop(); public int size(); public boolean empty(); public boolean full(); public int ontop(); } version 2: public interface Stack { public void push (int item) throws StackFullException; public int pop() throws StackEmptyException; public int size(); public boolean empty(); public boolean full(); public int ontop() throws StackEmptyException; }

The Stack ADT -- an array implementation (version 1) public int pop () { int i = stackADT[top]; top--; return i; } public boolean empty () { return (top == -1); public boolean full () { return (top == size - 1); public int ontop () { int i = pop(); push(i); public int size () { return (top + 1); class StackADT implements Stack { final int MAXSIZE = 100; private int size; private int[] stackADT; private int top = -1; public StackADT () { size = MAXSIZE; stackADT = new int[size]; } public StackADT (int inputsize) { size = inputsize; public void push (int number) { top++; stackADT[top] = number;

A note on JAVA exceptions Exceptions are events occurring during program execution which make continuation impossible or undesirable. Examples of exceptions: arithmetic overflow, array reference with index out of bounds, invalid user entry, etc. When an exception occurs, an exception handler is automatically invoked to resolve the problem or terminate the program in a controlled manner. There are two kinds of exceptions in JAVA: Implicit (built-in) exceptions which are signals from the Java Virtual Machine to the program indicating a violation of a semantic constraint of the Java language. Example: an attempt to index outside the array’s bounds will automatically throw an ArrayIndexOutOfBoundsException. Explicit exceptions which are intended to capture possible errors anticipated by the program designer. Example: user input outside the specified range. To define such an exception, a new exception class may have to be defined.

Defining new exception classes Version 2 of the Stack ADT implementation requires the following two exception classes to be defined: class StackEmptyException extends Exception { public StackEmptyException (String message) { System.out.println (message); } } class StackFullException extends Exception { public StackFullException (String message) { To throw an exception, we must create a new object of the exception’s type, i.e: throw new StackFullException (“The stack is full.”); The string serving as a parameter for exception constructor must be displayed by the corresponding catch statement, i.e. catch (StackFullException exception) { showStatus (exception.toString()); }

The Stack ADT -- an array implementation (version 2) public void push (int number) throws StackFullException { if (size() == size) throw new StackFullException ("The stack is full."); top++; stackADT[top] = number; } public int pop () throws StackEmptyException { if (empty()) throw new StackEmptyException ("The stack is empty."); int i = stackADT[top]; top--; return i; public int ontop () throws StackEmptyException { int i = pop(); try { push(i); } catch (StackFullException e) { System.out.println ("The stack is full."); } ............ // size, empty and full methods follow class StackEmptyException extends Exception { public StackEmptyException (String message) { System.out.println (message); } } class StackFullException extends Exception { public StackFullException (String message) { class StackADT implements Stack { final int MAXSIZE = 100; private int size; private int[] stackADT; private int top = -1; public StackADT () { size = MAXSIZE; stackADT = new int[size]; } public StackADT (int inputsize) { size = inputsize;

Example application of the Stack ADT using version 1 class StackAppl { public static void main (String[] args) throws IOException { Scanner scan = new Scanner(System.in); System.out.print ("Enter stack size: "); System.out.flush(); int size = scan.nextInt(); StackADT stack = new StackADT(size); int i = 2; while (!stack.full()) { stack.push(i); System.out.println (stack.ontop() + " is the top element."); i = i + 2; } System.out.println ("The current stack contains " + stack.size() + " elements."); while (!stack.empty()) System.out.println (stack.pop() + " is removed from the stack."); if (stack.empty()) System.out.println ("The stack is empty."); else System.out.println ("There are more elements on the stack."); }

Example application of the Stack ADT using version 2 class StackAppl2 { public static void main (String[] args) throws IOException { Scanner scan = new Scanner(System.in); System.out.print ("Enter stack size: "); System.out.flush(); int size = scan.nextInt(); StackADTv2 stack = new StackADTv2(size); int i = 2; try { // two exceptions, StackFullException and for (int j = 1; j <= 7; j++) { // StackEmptyException, must be watched for stack.push(i); System.out.println (stack.ontop() + " pushed"); i = i + 2; } catch (StackFullException e) { System.out.println ("The stack is full."); } catch (StackEmptyException e) { System.out.println ("The stack is empty."); } System.out.println ("The current stack contains " + stack.size() + " elements.") try { for (int j = 1; j <= 7; j++) { System.out.println (stack.pop() + " poped"); } } System.out.println ("The stack is empty."); } } }