CS2210 Data Structures and Algorithms

Similar documents
Lists and Sequences. 1/18/2005 4:12 AM Sequences 1

Lists. Outline. COMP9024: Data Structures and Algorithms. Lists. Array List ADT. Array-Based Implementation. Array lists Node lists Iterators

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Lists and Sequences. 1/22/ :32 PM Sequences 1

Introduction to Programming Using Java (98-388)

Java Primer 1: Types, Classes and Operators

Data Structure. Recitation III

Vectors. 9/14/2007 2:55 PM Vectors 1

Sequences Outline and Required Reading: Sequences ( 5.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Linked Lists. Singly Linked Lists (3.2) CSE 2011 Winter /01/2011

Programming II (CS300)

Subclassing for ADTs Implementation

Object-Oriented Programming

Data Structures and Algorithms " Arrays and Linked Lists!

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

ITI Introduction to Computing II

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Object Oriented Software Design

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Linked Lists. Chapter 4

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Today s lecture. CS 314 fall 01 C++ 1, page 1

Lecture 7: Data Abstractions

Iterators and Sequences

About this exam review

CS/ENGRD 2110 SPRING 2018

Data Structures (CS301) LAB

List ADT. B/W Confirming Pages

CS304 Object Oriented Programming Final Term

CH7. LIST AND ITERATOR ADTS

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Spring 2008 Data Structures (CS301) LAB

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS Introduction to Data Structures Week 1 Thursday

DATA STRUCTURES CHAPTER 1

CPSC 35 Midterm Exam

Chapter 5 Object-Oriented Programming

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

03/31/03 Lab 7. Linked Lists

CMPT 225. Lecture 6 linked list

Java Review: Objects

Chapter 11. Abstract Data Types and Encapsulation Concepts

CMSC 132: Object-Oriented Programming II

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

Programming overview

C a; C b; C e; int c;

ITI Introduction to Computing II

CS111: PROGRAMMING LANGUAGE II

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Java Software Solutions for AP Computer Science 3rd Edition, Lewis et al. 2011

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

CSIS 10B Lab 2 Bags and Stacks

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

EXAM Computer Science 1 Part 1

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Objects and Iterators

CSI33 Data Structures

Introduction to Computing II (ITI 1121) Final Examination

Code Reuse: Inheritance

CS 206 Introduction to Computer Science II

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Programming II (CS300)

ITI Introduction to Computing II

Programming II (CS300)

INFO0902 Data Structures and Algorithms

1. Stack Implementation Using 1D Array

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

ADTs, Arrays, Linked Lists

Introduction to Computer Science II CS S-20 Linked Lists III

Arrays. Array Definition

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Linked Lists. Chapter 12.3 in Savitch

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Dynamic Data Structures

Lecture 4. The Java Collections Framework

The Java Collections Framework. Chapters 7.5

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Lecture 5: Methods CS2301

CS 216 Exam 1 Fall SOLUTION

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 4 Defining Classes I

Lecture No.04. Data Structures

General Insert Operation

CS1004: Intro to CS in Java, Spring 2005

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

The Java Collections Framework and Lists in Java Parts 1 & 2

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Transcription:

CS2210 Data Structures and Algorithms Lecture 3: ADT and Java interfaces Instructor: Olga Veksler 2004 Goodrich, Tamassia

Outline Review Data Structures Abstract Data Types 2 Principles of OO Programming encapsulation reusability Java interfaces can be used to specify ADT in Java helps with encapsulation 2

Data Structures Data Structures Is a systematic way of organizing and accessing data Examples Stacks, queues, lists 3

Abstract Data Types ADT is a mathematical model of data structure that specifies type of data stored operations and their parameters supported on the data ADT specifies what each operation does, but not how it does it 4

ADT Example: List d a f c List stores elements sequentially Each item has position p p is an abstract place where we can be in the list Operations for traversal IsEmpty() return true if list is empty, false otherwise first() return position p of first elements in the list, ERROR if list is empty after(p) return position of element of the list following the one at position p; ERROR if p is the last position islast(p) true if p is the last item in the list, false otherwise 5

ADT Example: List Operations for insertion and removal replaceelement(p,e) replace element at position p with e, returning the element formerly at position p remove(p) remove and return element at position p insertfirst(e) insert new element e as the first element, return position of newly inserted element e insertafter(p,e) insert new element e after position p 6

ADT implementation To implement an ADT, we use following principles of Object Oriented Programming encapsulation (information hiding) reusability 7

Encapsulation User of data structure should not need to know details of its implementation Creator of data structure should be able to change implementation without affecting applications that use it Therefore implementation information should be hidden

Reusability Encapsulation promotes reusability If data structure is useful for one application, it is probably useful for another application Therefore we should design it to be as reusable as possible

Non-Reusable List Implementation Say we need a list of integers A non reusable way to implement a list directly in Java code class Node{ int n; Node next; //integer in this node // next node in the list In application, to create an empty new linked list, use Node head = null; to add a new element in front, use Node newnode = new Node(); newnode.n = i; newnode.next = head; //head=list s first element head = newnode;

Non-reusable List Disadvantages We must know and use next in applications If we want to re-implement List using an array, must change all the code To get a list of String, must copy and edit code 11

Reusable List implementation Define a class List class List{ boolean islast(position p){ ; Position insertfirst(object element){ ; Object remove(position p){ In application, declare a list List data = new List(); Use only the defined methods to access data data.insertfirst(newelement) 12

Reusable List Implementation Advantages User of class does not have to know about next attribute We could re-implement List using an array, if needed Each node contains an Object, not int, therefore could reuse the code for list of String 13

Object Oriented Approach to Implementing ADT We will follow Object Oriented approach of Java to implement ADT Start with ADT Define a class to implement the ADT Make public only the methods specified explicitly by an ADT Hide implementation details (encapsulation) Also make implementation as reusable as possible 14

Java Interface Interface a way to inform about various methods that an object supports to describe/enforce ADT for encapsulation Interface is simply a list of method declarations with no data and no bodies 15

Example of List Interface Public interface List { public boolean isempty(); public Position first(); public Position after(position p); public boolean islast(position p); public Object remove(position p); public Position insertfirst(object e); public Position insertafter(position p, Object e) 16

Java Interface We use a class to implement an interface When a class implements an interface, it must implement all methods declared in the interface The implementing class can have other objects and other methods (not from the interface) Interface enforces the implementing class to have certain methods with specified signatures signature is the list of methods declared in interface 17

List Interface Implementation public class NodeList implements List { protected int numelts; //number of items protected Dnode header, trailer; public NodeList(){ numelts = 0; header = new DNode(null,null,null); trailer = new DNode(header,null,null); header.setnext(trailer); public boolean isempty(){return (numelts<1); public boolean isfirst(position p) { Dnode v = checkposition(p); return v.getprev() == header;

Multiple Inheritance in Interfaces Multiple inheritance is not allowed for classes public class MyClass extends MySuperClass But multiple inheritance is allowed for Interfaces public class BoxedItem implements Sellable,Transportable

Interface as a Type Interface defines a new reference data type can be declared but not instantiated Suppose Position is an Interface Legal: public Position p; Cannot directly instantiate object of interface type Must use a class that implements Interface to instantiate Illegal:public Position p = new Position(); Legal: public Position p = new classimplementingposition(); 20

Interface as a Type public interface Person{ public boolean equalto(person other); null pointer public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid())); Person p; p Student s = new Student(); s id;other staff 21

Interface as a Type Person p; Student s = new Student(); p s id;other staff p = s; // no casting of s into type Person is required public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Interface as a Type Person p; Student s = new Student(); p = s; Person q; p s q id;other staff public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Interface as a Type Person p; p Student s = new Student(); s p = s; Person q; q p.equalto(q) // will not compile, q is not pointing to any object p.equalto(s) // will not compile s is not of type Person id;other staff public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Interface as a Type Person p; Student s = new Student(); p = s; Person q; p.equalto( (Person) s) // this is fine p s q id;other staff public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Interface as a Type Person p; Student s = new Student(); p = s; Person q; p s q id;other staff p.getid() // will not compile, p is of type Person, does not know // about method getid public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Interface as a Type Person p; Student s = new Student(); p = s; what s sees: String id; getid() equalto() p s id;other staff what p sees: equalto() public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Person p; Interface as a Type Student s = new Student(); p = s; p s q id;other staff Person q; f Student f = (Student) p // casting is required f.getid() // this is fine, as opposed to p.getid() public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Interface as a Type Person p; Student s = new Student(); p = s; Student f = (Student) p // casting is not required // casting is required from type A into a wider class B requires no explicit casting from type A into a narrower class B requires explicit casting public interface Person{ public boolean equalto(person other); public class Student implements Interface Person{ String id; public String getid() {return id; public boolean equalto(person other) { Student otherst = (Student) other; return(id.equals(otherst.getid()));

Doubly Linked List Class Node { private Node prev,next; private Object element; public Node( ) //constructor Empty linked list Non-empty linked list header trailer header f c trailer

List: Implementing Positions with Interface Want to support positions in a linked list User may want a f c to look up an element at a position p to get the position q after position p etc. One way to support positions would be to give the user a direct reference to a node

List: Implementing Positions with Interface Class Node { private Node prev,next; private Object element; public Node setnext( ){ ; public Node setelement(){; a If we give user reference to node, the user can change element stored at node with node.setelement(goodgrade) change where node.next points to with node.setnext(newlocation) erase the whole list User should change element stored at node only through list methods, such as list.replaceelement(p,newelement) Never want the user to change node.next f

List: Implementing Positions with Interface How to give node position without letting user do any damage? Before giving a reference to a node, wrap it up (cast) to Position interface Position interface allows only to look up stored element public interface Position{ Object element(); what n sees: setnext() getnext() element() a Node n f Position p what p sees: element(

Implementing Positions in Doubly Linked List public interface Position{ Object element(); Node class implements interface Position must have method element() Class Node implements Position{ public Object element(){ a f Node n Position p Inside linked list, we see nodes as of type Node Outside linked list we see nodes as of type Position

Implementing Positions in Doubly Linked List Class Node implements Position{ private Node prev,next; private Object element; public Node() //constructor public Object element(){ if (prev == null next == null) handle error somehow else return element; 35

Implementing Positions in Doubly Linked List linked list methods takes as input parameters of type Position from the outside world Example: public Position prev(position p){... Position p actually refers to a specific node in the linked list a f Position p For internal use, to find the previous position, convert from Position p back to the Node class

Implementing Positions in Doubly Linked List Class NodeList implements List{ protected Node checkposition(position p){ \\first check if valid position if (p == null p == header p == trailer ) handle the error somehow Node temp = (Node) p; return temp;... checkposition() converts p from Position back to Node

Implementing Positions in Doubly Linked List Class NodeList implements List{... public Position prev(position p){ Node v = checkposition(p); Node prev = v.getprev(); if (prev == header ) handle error somehow return prev; //no explicit cast required... v.getprev() is a public method of class Node prev is automatically cast into type Position 38

List: Implementing Positions with Interface Book makes extensive use of Position interface all information about position implementation is hidden positions come up with other data structures as well 39