ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Similar documents
Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

March 13/2003 Jayakanth Srinivasan,

STACKS AND QUEUES. Problem Solving with Computers-II

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

15. Stacks and Queues

Linked List. April 2, 2007 Programming and Data Structure 1

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Computer Science BS Degree - Data Structures (I2206) Abstract Data Types (ADT)

Stack Abstract Data Type

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Postfix (and prefix) notation

The Stack and Queue Types

Stacks and Queues. CSE Data Structures April 12, 2002

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stacks, Queues and Hierarchical Collections

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

UNIT 3

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Data Structures Week #3. Stacks

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Lecture 12 ADTs and Stacks

DATA STRUCTURE UNIT I

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CS 445: Data Structures Final Examination: Study Guide

CSC148H Week 3. Sadia Sharmin. May 24, /20

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

12 Abstract Data Types

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Lecture 4 Stack and Queue

CS W3134: Data Structures in Java

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

3137 Data Structures and Algorithms in C++

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

CSC 273 Data Structures

Stack and Its Implementation

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

.:: UNIT 4 ::. STACK AND QUEUE

Stacks. Revised based on textbook author s notes.

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Data Abstraction and Specification of ADTs

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CS 206 Introduction to Computer Science II

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Data Structure - Stack and Queue-

An Introduction to Trees

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Types of Data Structures

Abstract Data Types and Stacks

Programming. Lists, Stacks, Queues

[CS302-Data Structures] Homework 2: Stacks

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING UNIT-1

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Content: Learning Objectives

Formal Languages and Automata Theory, SS Project (due Week 14)

Lecture No.04. Data Structures

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

PA3 Design Specification

CS24 Week 4 Lecture 2

CS 106B Lecture 5: Stacks and Queues

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Abstract Data Types. Stack ADT

Stacks and their Applications

Stacks Fall 2018 Margaret Reid-Miller

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Chapter 9 STACK, QUEUE

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CMPT 225. Lecture 9 Stack

CSE 214 Computer Science II Stack

infix expressions (review)

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Programming Abstractions

Week 6. Data structures

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Infix to Postfix Conversion

BBM 201 DATA STRUCTURES

Lecture P5: Abstract Data Types

Programming Abstractions

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

BBM 201 DATA STRUCTURES

Lecture P5: Abstract Data Types

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).


Transcription:

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

ABSTRACTION To understand a system, it should be enough to understand what its components do without knowing how Watching a television We operate the tv through its interface remote control and buttons. We do not need to open the tv up and see inside to use it. When designing a new library, it is important to understand what are the abstract properties of the data types we want provide? which operations do we need to create (destroy), query, and manipulate objects of these types? Do we need to or want to know how FILE * is implemented? Or just HOW to use it?

ABSTRACT DATA TYPES A data type is... a set of values (atomic or structured values) a collection of operations on those values An abstract data type is... an approach to implementing data types separates interface from implementation builders of the ADT provide an implementation Users/clients of the ADT see only the interface A client can not see the implementation through the interface They do not know if you used an array, a linked list etc or anything else. This allows the implementation to change without breaking client code. Facilitates decomposing problems into smaller parts

ADTS IN C The interface is a contract between the client and the implementation Defined in the.h file typedef of the ADT Function prototypes fix function names and types The implementation is the inner workings of the ADT Implemented in.c file/s Structs the actual representation of the data type function implementations static functions local typedefs

PUSHDOWN STACK OR LAST-IN, FIRST-OUT (LIFO) QUEUE Two basic operations to manipulate a stack Insert (push) a new item Remove (pop) the most recently inserted item An operation to create a stack Create an empty stack An operation to query the state of the stack Check if stack is empty Applications backtracking search, function call stacks, evaluation of expressions

STACK ADT IMPLEMENTATION 1: USING ARRAYS Array as stack fill items into s[0], s[1],... maintain a counter of the number of pushed items pre-allocate array given maximum number of elements Push a a

STACK ADT IMPLEMENTATION 1: USING ARRAYS Array as stack fill items into s[0], s[1],... maintain a counter of the number of pushed items pre-allocate array given maximum number of elements Push a, push b a b

STACK ADT IMPLEMENTATION 1: USING ARRAYS Array as stack fill items into s[0], s[1],... maintain a counter of the number of pushed items pre-allocate array given maximum number of elements Push a, push b, push c a b c

STACK ADT IMPLEMENTATION 1: USING ARRAYS Array as stack fill items into s[0], s[1],... maintain a counter of the number of pushed items pre-allocate array given maximum number of elements Push a, push b, push c, pop a b

STACK ADT IMPLEMENTATION 1: USING ARRAYS Array as stack fill items into s[0], s[1],... maintain a counter of the number of pushed items pre-allocate array given maximum number of elements Push a, push b, push c, pop, push d a b d

STACK ADT IMPLEMENTATION 2: USING LISTS List as stack add node to front of the list when pushing take node from front of the list when popping push (a) a

STACK ADT IMPLEMENTATION 2: USING LISTS List as stack add node to front of the list when pushing take node from front of the list when popping push (a) push (b) b a

STACK ADT IMPLEMENTATION 2: USING LISTS List as stack add node to front of the list when pushing take node from front of the list when popping push (a) push (b) c b a push (c)

STACK ADT IMPLEMENTATION 2: USING LISTS List as stack add node to front of the list when pushing take node from front of the list when popping push (a) push (b) b a pop()

STACK ADT IMPLEMENTATION 2: USING LISTS List as stack add node to front of the list when pushing take node from front of the list when popping push (a) push (b) d b a push (c) pop() push (d)

EXAMPLE: BALANCING BRACKETS Example of stack ADT use on sample input: ( [ { } ] ) Next char Stack Check (start) (empty) - ( ( - [ ( [ - { ( [ { - } ( [ { vs } ] ( [ vs ] ) (empty) ( vs ) (eof) (empty) -

INFIX, PREFIX AND POSTFIX EXPRESSIONS Infix 2 + 3 Prefix + 2 3 Postfix 2 3 +

STACK ADT CLIENT EXERCISE: POSTFIX EXPRESSION EVALUATION Task: Given an expression in postfix notation, return its value: %./eval_postfix 5 9 8 + 4 6 * * 7 + * 2075 How can we evaluate a postfix expression? We use a stack When we encounter a number, push it When we encounter an operator, pop the two topmost numbers, apply the operator to those numbers, and push the result on the stack

FIRST-IN, FIRST-OUT (FIFO) QUEUE Two basic operations to manipulate the queue insert (put) new item delete (get) the least recently inserted item An operation to create a queue Create an empty queue An operation to query the state of the queue Check if queue is empty

QUEUE ADT IMPLEMENTATION1: USING LISTS List as queue add node to end of the list when pushing take node from front of the list when removing put(a) a b a

QUEUE ADT IMPLEMENTATION1: USING LISTS List as queue add node to end of the list when pushing take node from front of the list when removing put(a) put (b) a b a b

QUEUE ADT IMPLEMENTATION1: USING LISTS List as queue add node to end of the list when pushing take node from front of the list when removing put(a) put (b) a b a b c put (c)

QUEUE ADT IMPLEMENTATION1: USING LISTS List as queue add node to end of the list when pushing take node from front of the list when removing put(a) put (b) a b b c put (c) get()

QUEUE ADT IMPLEMENTATION1: USING LISTS List as queue add node to end of the list when pushing take node from front of the list when removing put(a) put (b) a b b c d put (c) get() put (d)

QUEUE ADT IMPLEMENTATION 2: USING ARRAYS Array as queue fill items into s[0], s[1],... maintain a counter for beginning and end of queue pre-allocate array given maximum number of elements roll over when reaching end of array put(a) a

QUEUE ADT IMPLEMENTATION 2: USING ARRAYS Array as queue fill items into s[0], s[1],... maintain a counter for beginning and end of queue pre-allocate array given maximum number of elements roll over when reaching end of array put(a) a b put (b)

QUEUE ADT IMPLEMENTATION 2: USING ARRAYS Array as queue fill items into s[0], s[1],... maintain a counter for beginning and end of queue pre-allocate array given maximum number of elements roll over when reaching end of array put(a) a b c put (b) put(c)

QUEUE ADT IMPLEMENTATION 2: USING ARRAYS Array as queue fill items into s[0], s[1],... maintain a counter for beginning and end of queue pre-allocate array given maximum number of elements roll over when reaching end of array put(a) b c put (b) put(c) get()

QUEUE ADT IMPLEMENTATION 2: USING ARRAYS Array as queue fill items into s[0], s[1],... maintain a counter for beginning and end of queue pre-allocate array given maximum number of elements roll over when reaching end of array put(a) b c d put (b) put(c) get() put(d)

TESTING Testing cannot establish that a program is correct would need to show for all possible inputs it produces the correct output This is impossible except in trivial cases We can only choose this subset well! Different types of parameters require different types of testing numeric: check the value, +ve, -ve, 0, large values, boundary cases etc. string: check the length, empty, 1 element, many elements properties like increasing order, decreasing order, random order

EXERCISE Think of some test cases for finding the maximum in an un-ordered array

BLACK BOX VS WHITE BOX TESTING Black Box Testing: Testing code from the outside: Checks behaviour Does tested input result in the correct output? Program does not know about the underlying implementation If the implementation changes the tests should still pass White Box Testing: Testing code from the inside: Checks code structure Tests internal functions Tests rely on and can access the implementation

ASSERT BASED TESTING How to use assert: use while developing, testing and debugging a program to make sure pre- and postconditions are valid not in production code! it aborts the program, error message useful to the programmer, but not to the user of the application Use exception handlers in production code to terminate gracefully with a sensible error message (if necessary)