+ Abstract Data Types

Similar documents
Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Dynamic Data Structures. John Ross Wallrabenstein

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Collections, Maps and Generics

5 Phases of Software Life Cycle

Dynamic Data Structures

Linked Lists

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

About this exam review

Introduction to Linked Lists

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

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

Java Collections Framework

CS 251 Intermediate Programming Inheritance

CP2 Revision. theme: dynamic datatypes & data structures

The time and space are the two measure for efficiency of an algorithm.

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

Run-Time Data Structures

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

COMP 401 Fall Recitation 7: Factories and Lists

Data Structure Series

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

Topic 11 Linked Lists

Introduction to Programming in C Department of Computer Science and Engineering

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

However, in C we can group related variables together into something called a struct.

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

Singly (Simply) Linked Lists

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Largest Online Community of VU Students

Java Review: Objects

ADTs, Arrays, Linked Lists

Data Structure (CS301)

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

cs Java: lecture #6

Programming II (CS300)

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

Chapter 14 Abstract Classes and Interfaces

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Ashish Gupta, Data JUET, Guna

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Summer Final Exam Review Session August 5, 2009

CSE 230 Intermediate Programming in C and C++

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

Linked Lists and Abstract Data Structures A brief comparison

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Review sheet for Final Exam (List of objectives for this course)

Doubly linked lists and freelist node caches

Data Structure. Recitation III

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

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

ITI Introduction to Computing II

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

Separate Compilation Model

Lecture 10 Notes Linked Lists

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Linked Lists. Chapter 12.3 in Savitch

The list abstract data type defined a number of operations that all list-like objects ought to implement:

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

182 review 1. Course Goals

Programming Exercise 14: Inheritance and Polymorphism

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

[2:3] Linked Lists, Stacks, Queues

Introduction to Computer Science I

Lecture 10 Notes Linked Lists

C++ Important Questions with Answers

CS106X Final Review. Rachel Gardner and Jared Bitz. slides adapted from Ashley Taylor, Anton Apostolatos, Nolan Handali, and Zachary Birnholz

Class 26: Linked Lists

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

Topic 7: Algebraic Data Types

Arrays. Array Definition

CMSC 341 Lecture 7 Lists

Programming II (CS300)

6.001 Notes: Section 6.1

Introduction to Object-Oriented Programming

Generics. IRS W-9 Form

Basic Data Structures (Version 7) Name:

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Chapter 17: Linked Lists

Project Compiler. CS031 TA Help Session November 28, 2011

Collection Considerations

Arrays. myints = new int[15];

Garbage Collection (1)

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CE204 Data Structures and Algorithms Part 2

Structure of Programming Languages Lecture 10

Lecture 9: Lists. MIT-AITI Kenya 2005

CS/ENGRD 2110 SPRING 2018

Inheritance. Transitivity

Transcription:

Linked Lists

Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure.

Abstract Data Types Abstract Data Types are a taxonomy of data structures. Moreover, an abstract data type is an abstraction. A contract for a type of data structure. There are typically many data structures for a given ADT Ex A Tree is an ADT Binary Search Tree is a concrete data structure Red-black Tree is another.

Abstract Data Type: List A List is an abstract data type that represents an ordered sequence of values, where the same value may occur more than once. Some operation it may provide: a constructor for creating an empty list; an operation for testing whether or not a list is empty; an operation for appending an entity to a list Does this sound familiar?

ArrayList An ArrayList is an example of an implementation of the List ADT (In fact, there is a List interface which ArrayList implements) So as we know an ArrayList is backed by an array However, thats not the only data structure that is is part of the class of data structures know as Lists

Linked List A B C NULL Head A linked list is a series of connected nodes Each node contains at least: A piece of data Reference (pointer) to the next node in the list Head: pointer to the first node The last node points to NULL

Linked List A B C NULL Head In order to traverse the list, you follow the pointer from each node to the next. Note that the nodes are not contiguous in memory! We create a new Node very time we add something to the List Prepending to a list is very fast Inserting into a sorted list is very fast

Recursive Data Type A B C NULL Head Note that each Node has a reference to another Node. How would this look in code? A recursive data type is a class that may contain other values of the same type. This is the other common use case of recursion.

Versus Arrays: Advantages A B C NULL Head They are dynamically sized: a linked list can easily grow and shrink in size without expensive memory allocations With arrays we have to declare a fixed size, and resizes are costly! Easy and fast insertions and deletions To insert or delete an element in an array we may need to move every other element in the array to accommodate. With a linked list, no need to move other nodes, only adjust some pointers.

Versus Arrays: Disadvantages A B C NULL Head Access to any particular node in a linked data structure requires following a chain of references that stored in it. No way to randomly access a node in the middle of the array For example, A HashMap could not be implemented efficiently using a linked list.

LinkedList in Java There is a LinkedList in the Java Collections Library that is a sibling to the ArrayList. They both implement the List interface (which represents the list ADT) http://docs.oracle.com/javase/7/docs/api/java/util/linkedlist.html

Programming Example We will look at a simplified linked list with only a few operations: isempty: determine whether or not the list is empty insert: insert a new node at the end of the list print: print the entire contents of the linked list See linkedlist/linkedlisttest.java

Variation: Doubly Linked List A B C Head What we ve see before is whats known as a singly linked list There are others, for example, the doubly linked list Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards

Generics

Parametric Polymorphism There are many types of polymorphism. So far in this course we ve seen.. Method overloading Using a subclass as its superclass in arrays or with methods There is another kind called parametric polymorphism You ve actually see this already too, though I don t think I used this name.

Parametric Polymorphism Parametric polymorphism is a way to make a language more expressive, while still maintaining static type-safety. Remember, we have a static type system in Java to help write more correct code. A function or a class can be written such that it can handle values identically without depending on their type. Such functions and data types are called generic and form the basis of generic programming.

Generic Programming Generic programming is a style of programming in which algorithms are written in terms of types to-be-specified-later Moreover, generics allow you to abstract over types. A common case for generic programming is container data structures, such as a List or a Map.

Generic Programming Those last few slides had words on them.

Generics in Java Java Generics is an example of a generic programming language construct. Common examples are found in the Collections library. Do you recall seeing this before? What does this mean?

Generics in Java Generics were introduced in Java 5, which was released in 2004. Therefore you can use Java without them, but there are many benefits to using them. Prior to Java 5, all Collection types worked by Type-Erasure That means that any reference type put into any collection was treated as an Object! (Remember Object is the root super class for all classes in Java, every reference type is-a Object)

Use in Java Collections Note the cast on line 4 The compiler can only guarantee that an Object will be returned. To ensure the assignment to a variable of type String is type-safe, the cast is required. There is the possibility of an error, since the programmer may be mistaken.

Use in Java Collections Enter Generics. Notice the type declaration on line 7. It specifies that this is a List of of String, written ArrayList<String>. Why? Type-safety! The compiler can now say for certain that there are only strings in the ArrayList. Every place you try and add to the list, the compiler will check to see if its String

Use in your code Great, so we can use Generics to have more reliable code when we use the Java Collection Library. Wouldn t it be useful to be able to use Generics in our own classes? Remember our StackOfCharacters.java earlier in the semester? Wouldn t it be annoying if we had to write a stack implementation for every type or class we want to store in a stack??

Use in your code We can! Here is a partial Stack data structure implementation using Generics. By changing our class to have Type Parameters we can let the user of our class specify what type our stack will contain, just like ArrayList or HashMap

Use in your code Note the <E> syntax in the class definition. This is saying This class takes a type parameter Moreover, if you use this class, you must tell me what type it will contain

Use in your code Note on line 2, we can use this type parameter, to a limited extent, as if it were a class. We can pass it to the ArrayList as its type parameter. In other words, <E> says There exists a type E of which this class and the ArrayList will contain

Use in your code Throughout the rest of the class we can use E as if it were a class (but we cannot call any methods on it, since it could be anything)

Use in your code Note line 19 We define an instance of our GenericStack and give a type parameter of String

Use in your code Note line 20 We push a String onto our stack. All is well.

Use in your code Note line 21 We attempt to push an int onto our GenericStack<String> What happens?

Use in your code Note line 21 We attempt to push an int onto our GenericStack<String> What happens? Compile error. It is impossible to the wrong type in our Stack!

Programming Example Lets convert our StackOfCharacters to a Stack that can be used for any type. See the stack package

Programming Example Lets convert our LinkedList of String to a LinkedList that can be used for any type. See the linkedlist package