Data Structures and Algorithms Lists



Similar documents
Data Structures and Algorithms

Ordered Lists and Binary Trees

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Data Structures and Algorithms

recursion, O(n), linked lists 6/14

DATA STRUCTURE - STACK

Data Structures and Algorithms Stacks and Queues

Analysis of a Search Algorithm

Circular Linked List. Algorithms and Data Structures

LINKED DATA STRUCTURES

ADTs,, Arrays, Linked Lists

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Unordered Linked Lists

Project 4 DB A Simple database program

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Lecture 12 Doubly Linked Lists (with Recursion)

DATA STRUCTURE - QUEUE

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Programming with Data Structures

Abstract Data Types. Chapter 2

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

STACKS,QUEUES, AND LINKED LISTS

Class 32: The Java Collections Framework

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Iterators. Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.

Chapter 8: Bags and Sets

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Introduction to Object-Oriented Programming

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

D06 PROGRAMMING with JAVA

Cpt S 223. School of EECS, WSU

Data Structures in the Java API

Algorithms and Data Structures

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Universidad Carlos III de Madrid

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Algorithms and Data Structures Written Exam Proposed SOLUTION

Abstraction and Abstract Data Types

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Java Map and Set collections

JAVA COLLECTIONS FRAMEWORK

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Node-Based Structures Linked Lists: Implementation

Section IV.1: Recursive Algorithms and Recursion Trees

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Parallel Programming

Linked List as an ADT (cont d.)

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

The Java Collections Framework

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Singly linked lists (continued...)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CMSC 202H. ArrayList, Multidimensional Arrays

Stacks. Linear data structures

Binary Heap Algorithms

CSE 211: Data Structures Lecture Notes VII

Linked Lists Linked Lists, Queues, and Stacks

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

Recursive Implementation of Recursive Data Structures

Introduction to Data Structures

Sequential Data Structures

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

COMPUTER SCIENCE. Paper 1 (THEORY)

Software Engineering Techniques

Quosal Form Designer Training Documentation

The ADT Binary Search Tree

Practical Session 4 Java Collections

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

6. Standard Algorithms

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

Lecture 12: Abstract data types

Data Structures. Level 6 C Module Descriptor

CmpSci 187: Programming with Data Structures Spring 2015

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Data Structures Using C++

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

7.1 Our Current Model

Lecture Notes on Binary Search Trees

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Transcription:

Data Structures and Algorithms Lists Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/19

5-0: Abstract Data Types An Abstract Data Type is a definition of a type based on the operations that can be performed on it. l is a List because we can insert, remove, clear, etc. Not because of how elements are stored. An ADT is an interface Data in an ADT cannot be manipulated directly only through operations defined in the interface A data structure is an implementation of an ADT Department of Computer Science University of San Francisco p.2/19

5-1: List ADT A List is an ordered collection of elements Each element in the list has a position Element 0, Element 1, Element 2, We can access elements in the list through an iterator Department of Computer Science University of San Francisco p.3/19

5-2: List ADT Operations Make list empty Add an element to the list At the end At the current position Remove an element Get the length of the list Check if the list is empty Print list Get an iterator to traverse the list Department of Computer Science University of San Francisco p.4/19

Think of an iterator as a generic way of accessing the elements in an ADT. Provides a logical index into the ADT. Separates out the functionality og accessing elements from the ADT itself. Some operation on iterators: Move iterator forward in the data structure Move iterator backward in the data structure Insert a new element at the current location Return the element at the current location 5-3: Iterators Department of Computer Science University of San Francisco p.5/19

Set the iterator to the front of the list Move the iterator forward one element Move the iterator back one element Set the iterator to index Get the object at the current position Remove the object at the current position Insert an element right before the current position 5-4: List Iterator Department of Computer Science University of San Francisco p.6/19

A Java interface is a set of methods. 5-5: Java Interfaces Any class that implements an interface must implement all of these methods Department of Computer Science University of San Francisco p.7/19

5-6: Java List Interface Without iterators: public interface List public void clear(); public void append(object item); public Object remove(); public int length(); public void print(); public boolean isempty(); // All the following methods require a user to know about the curr // pointer. public void insert(object item); public void next(); public void prev(); public void setposition(int pos); public void setvalue(object val); public Object currvalue(); Department of Computer Science University of San Francisco p.8/19

5-7: Java List Interface With iterators: public interface List public void clear(); public void append(object elem); public int length(); public void print(); public boolean isempty(); public ListIterator iterator(); Department of Computer Science University of San Francisco p.9/19

The iterator is an example of delegation 5-8: Java List Iterator Interface public interface ListIterator public void first(); public void next(); public void previous(); public boolean inlist(); public Object current(); public void setposition(int position); public void insert(object elem); public Object remove(); public void setcurrent(object value); Department of Computer Science University of San Francisco p.10/19

5-9: Array Implementation Data is stored in an array Iterator stores index of current location To add an element to the current position: Shift other elements to the right To remove and element from the middle of the array: Shift other elements to the left List has a maximum size (unless we use growable arrays) Department of Computer Science University of San Francisco p.11/19

Running Time for each operation: insert append remove setfirst next previous length setposition currentvalue 5-10: Array Implementation Department of Computer Science University of San Francisco p.12/19

Running Time for each operation: insert append remove setfirst next previous length setposition currentvalue 5-11: Array Implementation Department of Computer Science University of San Francisco p.13/19

5-12: Linked-List Implementation Data is stored in a linked list We ll use an auxiliary class called Link to hold elements Maintain a pointer to first and last element in list Iterator maintains a pointer to the current element To find the ith element: Start at the front of the list Skip past i elements How do we insert an element before the current element? How do we remove the current element? Department of Computer Science University of San Francisco p.14/19

5-13: Linked-List Implementation Data is stored in a linked list Maintain a pointer to first and last element in list Iterator maintains a pointer to the element before the current element To find the ith element: Start at the front of the list Skip past (i) elements What should current pointer be when the first element in the list is the current element? Department of Computer Science University of San Francisco p.15/19

5-14: Linked-List Implementation Data is stored in a linked list with a dummy first element Maintain a pointer to first (dummy) and last element in list Iterator maintains a pointer to the element before the current element To find the ith element: Start at the front of the list Skip past (i+1) elements Department of Computer Science University of San Francisco p.16/19

Running Time for each operation: insert append remove setfirst next previous length setposition currentvalue 5-15: Linked List Implementation Department of Computer Science University of San Francisco p.17/19

Running Time for each operation: insert * append remove * setfirst next previous * length setposition * currentvalue * Different from array implementation 5-16: Linked List Implementation Department of Computer Science University of San Francisco p.18/19

5-17: Doubly-Linked Lists Each element in the list has two pointers next and previous Can locate the previous element of any element in the list in time, instead of time More space is required (two pointers for each element, instead of one) Does current still need to point to the element before the current element? Do we still need a dummy element? Department of Computer Science University of San Francisco p.19/19