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

Size: px
Start display at page:

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

Transcription

1 Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references: Online Java Tutorial at java.sun.com/docs/books/tutorial/collections/ General-Purpose Implementations A set of implementations for the Collection interfaces provided in java.util: Interface Set List Map Implementation HashSet, TreeSet, ArrayList, LinkedList, HashMap, TreeMap, More than one implementation is provided for each interface: each implementation uses a different data structure each is optimized for some operations, but might not be so good for others users can define their own implementations if they need to List Implementations 2

2 General-Purpose Implementations (continued) Each implementation of Collection has a default constructor a constructor with a Collection parameter (used to transform one type of collection to another) Sorted collections have two more constructors one that takes a Comparator another that takes a SortedCollection List Implementations 3 ArrayList Implementation: CArrayList List elements are stored in an array in positions 0 to size-1 The get and set positional access operations are O(1). We can access items in the list in a random order much like the tracks on a CD. We call such a structure a random access data structure. Adding at the end is O(1) if there is space O(n) if the array is full o need to create a larger array and copy the n elements of the old array o can use System.arraycopy() to copy the array efficiently If we have an estimate of the max size of the list, we can create a large enough array using CArrayList(int initialcapacity) List Implementations 4

3 Array Implementation: CArrayList Adding and removing an element at a given index is expensive we need to shift part of the array both operations are O(n) on average removing using an iterator is also O(n) on average List Implementations 5 CArrayList Implementation Example public class CArrayList<E> { public static final int INIT_CAPACITY = 10; private static final int INCREMENT_FACT = 2; private E[] data; private int count; private int capacity; public CArrayList() { data = (E[]) new Object[INIT_CAPACITY]; count = 0; capacity = INIT_CAPACITY; List Implementations 6

4 CArrayList Example (cont d) public boolean add(e element) { // if array is full, increase its capacity if (count == capacity) { growarray(); data[count] = element; count++; return true; List Implementations 7 CArrayList Example (cont d) private void growarray() { capacity *= INCREMENT_FACT; E[] temp = (E[]) new Object[capacity]; System.arraycopy(data, 0, temp, 0, count); data = temp; // rest of implementation available for // download from course web site List Implementations 8

5 Array Implementation: CArrayList The array based implementation of our list has one very nice feature: we can get an item at a particular index in O(1) time and some disadvantages: it takes O(n) time on average to insert or remove from the list expanding the list is an O(n) operation if the list fills up In the next section we examine another implementation of the List interface that removes the above disadvantages but also loses the O(1) time for accesses. List Implementations 9 Singly Linked List A singly linked list (or just linked list) consists of a series of nodes Each node has two fields: the data stored in that node a link (reference) to the next node in the list the link in the last node is null the list is accessed through a reference to its first node (head) Example: A linked list of integers: head Initially, list is empty; nodes are added one at a time as needed List Implementations 10

6 Singly Linked List A linked list has the advantage that it grows or shrinks dynamically as items are added to or removed from the list. It has the disadvantage that you cannot get an element in the list in O(1) time. If you want to get the item at the end of the list, you have to traverse the entire list to reach it. We call such a structure a sequential access data structure. Before we consider a full-blown implementation of the List interface using a linked list, we will learn how to perform a few basic operations on a linked list like inserting an item at the head of the list, inserting at the end of the list and removing an item from the list. List Implementations 11 Java Declaration of Singly Linked List Need to define a class Node to represent a node in the linked list. This class will be a private static inner class of CLinkedList - the class that implements the linked list. We make it static because instances of the inner class do not need to access any of the data associated with the outer class. Given that the class is a private inner class, it is not accessible outside of the containing class. List Implementations 12

7 Java Declaration of Node class private static class Node<E> { E element; Node<E> next; Node() { this(null, null); Node( E obj ) { this(obj, null); Node(E obj, Node nxt) { element = obj; next = nxt ; List Implementations 13 Linked List Class We now consider the implementation of some of the methods of the CLinkedList class: public class CLinkedList<E> { private Node<E> head; private Node<E> tail; private int numitems; public CLinkedList() { head = tail = null; numitems = 0; List Implementations 14

8 Linked List Operations cont d // add to end of list void add(e obj) { Node<E> newnode = new Node<E>(obj); Node<E> cur = head; if (cur == null) { head = tail = newnode; else { tail.next = newnode; tail = newnode; numitems++; List Implementations 15 Linked List Operations cont d When we remove an object from a list, not only we need a reference to the node containing the object we want to remove, but we also need a reference to the previous node so that we keep update the next links correctly. boolean remove(e obj) { Node<E> cur = head; Node<E> prev = null; // find a reference to the node containing obj // and to the previous node while(cur!= null &&!cur.element.equals(obj)) { prev = cur; cur = cur.next; List Implementations 16

9 Linked List Operations cont d // if we failed to find obj in list if (cur == null) return false; if (cur == head && head == tail) head = tail = null; else if (cur == head) head = cur.next; else if (cur == tail) { tail = prev; tail.next = null; else prev.next = cur.next; numitems--; return true; List Implementations 17 Doubly Linked List Singly linked lists have the following disadvantages: you can traverse the list in only one direction o Recall that in the Java collections framework, the List interface allows a client to get a ListIterator over the list and that this iterator can move either forwards or backwards through the list. if you have a reference to a node that you want to delete or you have a reference to a node and want to insert a new node before it, you have to traverse the list to find a pointer to the previous node. A doubly linked list is a variation of a linked list that does not have the disadvantages listed above. List Implementations 18

10 Doubly Linked List (cont'd) In a doubly linked list each node has two links: one to the next node, one to the previous node Example: A doubly linked list of integers: head In last node, link to next node is null In first node, link to previous node is null Now we can easily find the previous node traverse the list in reverse order List Implementations 19 Java Declaration of Doubly Linked List private static class Node<E> { E element; Node<E> previous; Node<E> next; Node() { this(null, null, null) ; Node(E obj) { this(obj, null, null) ; Node(E obj, Node prv, Node nxt) { element = obj; previous = prv; next = nxt; List Implementations 20

11 Doubly Linked List Class We now consider the implementation of some of the methods of the GLinkedList class: public class GLinkedList<E> { private Node<E> head; private Node<E> tail; private int numitems; public GLinkedList() { head = tail = null; numitems = 0; List Implementations 21 Some Operations on Doubly Linked Lists // add to end of list void add(e obj) { Node<E> newnode = new Node<E>(obj, tail, null); if( head == null ) head = tail = newnode; else { tail.next = newnode; tail = newnode; numitems++; List Implementations 22

12 Some Operations cont d Remove an object from the list (removes only first instance found): boolean remove(e obj) { Node<E> cur = head; // look for a node containing obj while (cur!= null && cur.element.equals(obj)) cur = cur.next; // if we failed to find obj in the list if(cur == null) return false; List Implementations 23 Some Operations cont d if (cur == head && head == tail) head = tail = null; else if (cur == head) { head = cur.next; head.prev = null; else if (cur == tail) { tail = cur.prev; tail.next = null; else { cur.next.previous = cur.previous; cur.previous.next = cur.next; numitems--; return true; List Implementations 24

13 Linked List Implementation of List: GLinkedList GLinkedList is in Examples : implements List interface uses a doubly linked list with references to the head and tail nodes uses modcount to count # of modifications o an iterator uses this to detect concurrent modifications GListIterator (in same example) implements ListIterator interface keeps track of o last node returned by next() o next node to be returned by next() o # of modifications done by the iterator GLinkedList is very similar to Java s LinkedList but LinkedList uses a circular doubly linked list with a head node List Implementations 25 GLinkedList (cont ) After executing: List<Integer> glist = new GLinkedList<Integer>(); Iterator<Integer> itr = glist.iterator(); glist.add(new Integer(5)); glist.add(new Integer(6)); glist.add(new Integer(7)); itr.next(); List Implementations 26

14 GLinkedList (cont d) glist and itr look like: head tail size modcount glist 3 3 lastnode nextnode nextindex ExpectedModCount itr List Implementations 27 Exercises Chapter 20, page 771 Exercises P20.3, P20.5 P20.9 starting from the GlinkedList class provided on the Web site. List Implementations 28

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

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

Linked List Nodes (reminder)

Linked List Nodes (reminder) Outline linked lists reminders: nodes, implementation, invariants circular linked list doubly-linked lists iterators the Java foreach statement iterator implementation the ListIterator interface Linked

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList Implementations I 1 Agenda Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList Stack and queues Array-based implementations

More information

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

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:

More information

Linked Lists. Chapter 4

Linked Lists. Chapter 4 Linked Lists Chapter 4 1 Linked List : Definition Linked List: A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT

More information

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Advanced Linked Lists Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Review The singly linked list: consists of nodes linked in a single direction. access and traversals begin with the first

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

Outline. iterator review iterator implementation the Java foreach statement testing

Outline. iterator review iterator implementation the Java foreach statement testing Outline iterator review iterator implementation the Java foreach statement testing review: Iterator methods a Java iterator only provides two or three operations: E next(), which returns the next element,

More information

CS 310: Array-y vs Linky Lists

CS 310: Array-y vs Linky Lists CS 310: Array-y vs Linky Lists Chris Kauffman Week 4-2 Feedback 1. Something you learned about yourself or your process during coding of HW1. Could be improvements you need to make Could be a success you

More information

CS32 Discussion Week 3

CS32 Discussion Week 3 CS32 Discussion Week 3 Muhao Chen muhaochen@ucla.edu http://yellowstone.cs.ucla.edu/~muhao/ 1 Outline Doubly Linked List Sorted Linked List Reverse a Linked List 2 Doubly Linked List A linked list where

More information

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created. 1. Because an ArrayList is an indexed collection, you can access its elements using a subscript. 2. The actual object type stored in an object of type CollectionClassName is specified when the object

More information

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList! Implementations I 1 Agenda Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList! Stack and queues Array-based implementations

More information

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

Introduction to Computer Science II CS S-20 Linked Lists III Introduction to Computer Science II CS112-2012S-20 Linked Lists III David Galles Department of Computer Science University of San Francisco 20-0: Linked List Previous Practical Example: removeat(int index)

More information

+ Abstract Data Types

+ Abstract Data Types 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

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 26, 2013 Abstract These lecture notes are meant to be looked

More information

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

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes. CS231 - Spring 2017 Linked Lists List o Data structure which stores a fixed-size sequential collection of elements of the same type. o We've already seen two ways that you can store data in lists in Java.

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2 CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 24, 2013 Abstract These lecture notes are meant to be looked

More information

Linked List. ape hen dog cat fox. tail. head. count 5

Linked List. ape hen dog cat fox. tail. head. count 5 Linked Lists Linked List L tail head count 5 ape hen dog cat fox Collection of nodes with a linear ordering Has pointers to the beginning and end nodes Each node points to the next node Final node points

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 26, 2013 Abstract These lecture notes are meant to be looked

More information

List ADT. B/W Confirming Pages

List ADT. B/W Confirming Pages wu3399_ch8.qxd //7 :37 Page 98 8 List ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the List ADT. the List ADT using an array

More information

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one Iterators What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one 9-2 2-2 What is an Iterator? An iterator is an abstract data

More information

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

More information

Class 26: Linked Lists

Class 26: Linked Lists Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward 2 The Java Collection Classes The java.util package contains implementations of many

More information

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: Linked Lists Object References When you declare a variable of a non-primitive type you are really declaring a reference to that object String

More information

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

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #8 Spring 2018 Project 1 due tonight Announcements Project 2 will be out soon Q & A Reminder: For sharing your concern anonymously, you can always go to:

More information

2/22/2013 LISTS & TREES

2/22/2013 LISTS & TREES LISTS & TREES Lecture 9 CS2110 Spring 2013 1 List Overview 2 Purpose Maintain an ordered collection of elements (with possible duplication) Common operations Create a list Access elements of a list sequentially

More information

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam 1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Exam Name: Email Address: TA: Section: You have three hours to complete this exam. For coding questions, you do not need to include

More information

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3. Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item);

More information

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

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr. 12. Dynamic Data Structures & Generics Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch Objectives! Define and use an instance of ArrayList! Describe general idea

More information

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer. C++ - Lesson 2 1. Explain the following declarations: a) int *a(int a[]); This is a function prototype. 'a' is a function that takes an integer array argument and returns an integer pointer. b) const char

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Information Technology and Engineering Iterator (part II) Inner class Implementation: fail-fast Version of March 20, 2011 Abstract These

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

Java Collections Framework reloaded

Java Collections Framework reloaded Java Collections Framework reloaded October 1, 2004 Java Collections - 2004-10-01 p. 1/23 Outline Interfaces Implementations Ordering Java 1.5 Java Collections - 2004-10-01 p. 2/23 Components Interfaces:

More information

CMSC 341. Linked Lists. Textbook Section 3.5

CMSC 341. Linked Lists. Textbook Section 3.5 CMSC 341 Linked Lists Textbook Section 3.5 1 Implementing A Linked List To create a doubly linked list as seen below MyLinkedList class Node class LinkedListIterator class Sentinel nodes at head and tail

More information

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists Abstract Data Type List ADT List Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists A list has first, last, and in between items (the

More information

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering Readings and References Java Collections References» "Collections", Java tutorial» http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Winter 2003 Software Engineering http://www.cs.washington.edu/education/courses/403/03wi/

More information

CS350: Data Structures Linked Lists

CS350: Data Structures Linked Lists Linked Lists James Moscola Department of Physical Sciences York College of Pennsylvania James Moscola Linked Lists Come in a variety of different forms - singly linked lists - doubly linked lists - circular

More information

1. Introduction. Lecture Content

1. Introduction. Lecture Content Lecture 6 Queues 1 Lecture Content 1. Introduction 2. Queue Implementation Using Array 3. Queue Implementation Using Singly Linked List 4. Priority Queue 5. Applications of Queue 2 1. Introduction A queue

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 16 References and linked nodes reading: 16.1 2 Recall: stacks and queues stack: retrieves elements in reverse order as added queue: retrieves elements in same order as added

More information

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

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison Linked lists Comp Sci 1575 Data Structures Outline 1 2 3 4 5 Linked list Linked lists are of a linear collection of data elements, called nodes, each pointing to the next node Each node is composed of

More information

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board) CS112-2012S-20 Linked Lists III 1 20-0: Linked List ious Practical Example: removeat(int index) remove( o) 20-1: removeat First need to get to node before the one we want to remove We can then use the

More information

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct

More information

CMPT 225. Lecture 6 linked list

CMPT 225. Lecture 6 linked list CMPT 225 Lecture 6 linked list 1 Last Lecture Class documentation Linked lists and its operations 2 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data

More information

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016 Winter 2016 COMP-250: Introduction to Computer Science Lecture 6, January 28, 2016 Java Generics element next _, Java Generics Java Generics (Doubly) Linked List (Doubly) Linked List Node element next

More information

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1 Topic #9: Collections CSE 413, Autumn 2004 Programming Languages http://www.cs.washington.edu/education/courses/413/04au/ If S is a subtype of T, what is S permitted to do with the methods of T? Typing

More information

Dynamic Data Structures and Generics

Dynamic Data Structures and Generics Dynamic Data Structures and Generics Chapter 10 Chapter 10 1 Introduction A data structure is a construct used to organize data in a specific way. An array is a static data structure. Dynamic data structures

More information

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

Data structure is an organization of information, usually in memory, for better algorithm efficiency. Lecture 01 Introduction Wednesday, 29 July 2015 12:59 pm Data structure is an organization of information, usually in memory, for better algorithm efficiency. Abstract data types (ADTs) are a model for

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 17.1 Introduction to the

More information

Java Review: Objects

Java Review: Objects Outline Java review Abstract Data Types (ADTs) Interfaces Class Hierarchy, Abstract Classes, Inheritance Invariants Lists ArrayList LinkedList runtime analysis Iterators Java references 1 Exam Preparation

More information

Laboratorio di Algoritmi e Strutture Dati

Laboratorio di Algoritmi e Strutture Dati Laboratorio di Algoritmi e Strutture Dati Guido Fiorino guido.fiorino@unimib.it aa 2013-2014 2 Linked lists (of int) We focus on linked lists for integers; let us start with a class that defines a node

More information

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE PREVIOUS COURSE CONTENT Inheritance Abstract classes Interfaces instanceof operator Nested classes Enumerations COUSE CONTENT Collections List Map Set Aggregate

More information

CE204 Data Structures and Algorithms Part 2

CE204 Data Structures and Algorithms Part 2 CE204 Data Structures and Algorithms Part 2 14/01/2018 CE204 Part 2 1 Abstract Data Types 1 An abstract data type is a type that may be specified completely without the use of any programming language.

More information

Tutorial 2: Linked Lists

Tutorial 2: Linked Lists Tutorial 2: Linked Lists ? 2 Agenda Introduction of Linked List Arrays Vs Linked Lists Types of Linked Lists Singly Linked List Doubly Linked List Circular Linked List Operations Insertion Deletion 3 Data

More information

Introduction to Linked Data Structures

Introduction to Linked Data Structures Introduction to Linked Data Structures A linked data structure consists of capsules of data known as nodes that are connected via links Links can be viewed as arrows and thought of as one way passages

More information

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss The Collections API Mark Allen Weiss Lecture Objectives To learn how to use the Collections package in Java 1.2. To illustrate features of Java that help (and hurt) the design of the Collections API. Tuesday,

More information

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017 COMP 250 Lecture 32 interfaces (Comparable, Iterable & Iterator) Nov. 22/23, 2017 1 Java Comparable interface Suppose you want to define an ordering on objects of some class. Sorted lists, binary search

More information

Today's Agenda. > To give a practical introduction to data structures. > To look specifically at Lists, Sets, and Maps

Today's Agenda. > To give a practical introduction to data structures. > To look specifically at Lists, Sets, and Maps Today's Agenda > To give a practical introduction to data structures > To look specifically at Lists, Sets, and Maps > To talk briefly about Generics in Java > To talk about interfaces in Java Data Structures

More information

Arrays & Linked Lists

Arrays & Linked Lists Arrays & Linked Lists Part 1: Arrays Array Definition An array is a sequenced collection of variables all of the same type. Each variable, or cell, in an array has an index, which uniquely refers to the

More information

Data Structure. Recitation III

Data Structure. Recitation III Data Structure Recitation III Topic Binary Search Abstract Data types Java Interface Linked List Binary search Searching a sorted collection is a common task. A dictionary is a sorted list of word definitions.

More information

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

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

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.

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. Leaning Objective: In this Module you will be learning the following: Circular Linked Lists and it operations Introduction: Circular linked list is a sequence of elements in which every element has link

More information

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data

More information

Linked Lists

Linked Lists Linked Lists 2-17-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? Can you tell me what a linked list is and what

More information

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling

More information

Queues Fall 2018 Margaret Reid-Miller

Queues Fall 2018 Margaret Reid-Miller Queues 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Writing methods various classes that implement Lists. Methods using Lists and Big-O w/ ArrayList or LinkedLists Prove

More information

Java Collections Framework

Java Collections Framework Java Collections Framework Introduction In this article from my free Java 8 course, you will be given a high-level introduction of the Java Collections Framework (JCF). The term Collection has several

More information

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Points off Total off Net Score. CS 314 Final Exam Spring 2017 Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Spring 2017 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points.

More information

doubly linked lists Java LinkedList

doubly linked lists Java LinkedList COMP 250 Lecture 5 doubly linked lists Java LinkedList Sept. 16, 2016 1 Doubly linked lists next prev element Each node has a reference to the next node and to the previous node. head tail 2 class DNode

More information

Introduction to Linked Lists

Introduction to Linked Lists Introduction to Linked Lists In your previous programming course, you organized and processed data items sequentially using an array (or possibly an arraylist, or a vector). You probably performed several

More information

Dynamic Data Structures and Generics

Dynamic Data Structures and Generics Dynamic Data Structures and Generics Reading: Savitch ch. 12 Objectives Introduce Abstract Data Types (ADTs) and review interfaces Introduce Java's ArrayList class Learn about linked lists and inner classes

More information

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1

Abstract Data Types. Data Str. Client Prog. Add. Rem. Find. Show. 01/30/03 Lecture 7 1 Abstract Data Types Add Client Prog Rem Find Data Str. Show 01/30/03 Lecture 7 1 Linear Lists It is an ordered collection of elements. Lists have items, size or length. Elements may have an index. Main

More information

Doubly-Linked Lists

Doubly-Linked Lists Doubly-Linked Lists 4-02-2013 Doubly-linked list Implementation of List ListIterator Reading: Maciel, Chapter 13 HW#4 due: Wednesday, 4/03 (new due date) Quiz on Thursday, 4/04, on nodes & pointers Review

More information

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi Stack and Queue How do we implement a Queue using Array? : A collection of nodes with linear ordering defined on them. Each node holds an element and points to the next node in the order. The first node

More information

Title Description Participants Textbook

Title Description Participants Textbook Podcast Ch21c Title: Hash Table Implementation Description: Overview of implementation; load factors; add() method; rehash() method; remove() method Participants: Barry Kurtz (instructor); John Helfert

More information

Data Structures Brett Bernstein

Data Structures Brett Bernstein Data Structures Brett Bernstein Lecture 8: Iterators, Maps, and Hashtables Exercises 1. Show how to print out the elements of a doubly linked list in reverse order. 2. What are the Java API classes that

More information

Collections Questions

Collections Questions Collections Questions https://www.journaldev.com/1330/java-collections-interview-questions-and-answers https://www.baeldung.com/java-collections-interview-questions https://www.javatpoint.com/java-collections-interview-questions

More information

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists (2018F) Lecture3: Arrays and Linked Lists Daijin Kim CSE, POSTECH dkim@postech.ac.kr Array Definition An array is a sequenced collection of variables all of the same type. Each variable, or cell, in an

More information

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering Readings and References Java Collections "Collections", Java tutorial http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Spring 2004 Software Engineering http://www.cs.washington.edu/education/courses/403/04sp/

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 16 References and linked nodes reading: 16.1 2 Value semantics value semantics: Behavior where values are copied when assigned, passed as parameters, or returned. All primitive

More information

Lists. ordered lists unordered lists indexed lists 9-3

Lists. ordered lists unordered lists indexed lists 9-3 The List ADT Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations Compare list implementations 9-2 Lists A list is a

More information

Class 32: The Java Collections Framework

Class 32: The Java Collections Framework Introduction to Computation and Problem Solving Class 32: The Java Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward Goals To introduce you to the data structure classes that come

More information

DNHI Homework 3 Solutions List, Stacs and Queues

DNHI Homework 3 Solutions List, Stacs and Queues Solutions List, Stacs and Queues Problem 1 Given the IntegerQueue ADT below state the return value and show the content of the, initially empty, queue of Integer objects after each of the following operations.

More information

Model Solutions. COMP 103: Test April, 2013

Model Solutions. COMP 103: Test April, 2013 Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 40 minutes

More information

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper. Performance Evaluation Java Collections Framework TECHNICAL WHITEPAPER Author: Kapil Viren Ahuja Date: October 17, 2008 Table of Contents 1 Introduction...3 1.1 Scope of this document...3 1.2 Intended

More information

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 5 due tonight at midnight -assignment 6 is out -YOU WILL BE SWITCHING PARTNERS! 3 assignment

More information

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017 COMP 250 Lecture 6 doubly linked lists Sept. 20/21, 2017 1 Singly linked list head tail 2 Doubly linked list next prev element Each node has a reference to the next node and to the previous node. head

More information

Model Solutions. COMP 103: Test May, 2013

Model Solutions. COMP 103: Test May, 2013 Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes

More information

CS 314 Final Fall 2012

CS 314 Final Fall 2012 Points off 1 2A 2B 2C 3 4A 4B 5 Total off Net Score CS 314 Final Fall 2012 Your Name_ Your UTEID Instructions: 1. There are 5 questions on this exam. The raw point total on the exam is 110. 2. You have

More information

EECS 2011 M: Fundamentals of Data Structures

EECS 2011 M: Fundamentals of Data Structures EECS 2011 M: Fundamentals of Data Structures Suprakash Datta Office: LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle S. Datta (York Univ.) EECS 2011 W18 1 / 19 Iterators and

More information

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014 Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014 Review List = Organization of Data in a Linear Fashion, where Order is Important Set of actions that can be carried out efficiently on the data. Typical Actions

More information

Dynamic Data Structures and Generics. Chapter 10

Dynamic Data Structures and Generics. Chapter 10 Dynamic Data Structures and Generics 1 Reminders Project 6 due Nov 03 @ 10:30 pm Project 5 grades released (or by tonight): regrade requests due by next Friday Exam 2: handed back next week, solution discussed

More information

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References Reading Readings and References Class Libraries CSE 142, Summer 2002 Computer Programming 1 Other References» The Java tutorial» http://java.sun.com/docs/books/tutorial/ http://www.cs.washington.edu/education/courses/142/02su/

More information

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head Linked Lists An Alternative Collections Data Structure What Is A Linked List? A data structure using memory that expands and shrinks as needed Relies on identical nodes pointing or linked to other nodes

More information