ECE 242 Data Structures and Algorithms. Linked List I. Lecture 10. Prof.

Similar documents
Introduction. Reference

Stacks and Queues III

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

Mid-Term- ECE 242 Fall 2016 Closed book/notes- no calculator- no phone- no computer

ECE 242 Data Structures and Algorithms. Hash Tables I. Lecture 24. Prof.

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

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CS171 Midterm Exam. October 29, Name:

1. Introduction. Lecture Content

Standard ADTs. Lecture 19 CS2110 Summer 2009

ITI Introduction to Computing II

About this exam review

1. Stack Implementation Using 1D Array

CS W3134: Data Structures in Java

cs Java: lecture #6

CS24 Week 4 Lecture 2

Java Memory Management

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Announcements Queues. Queues

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

ECE 242 Fall 13 Exam I Profs. Wolf and Tessier

ECE 242 Data Structures and Algorithms. Advanced Sorting I. Lecture 16. Prof.

Final- ECE 242 Fall 2016 Closed book/notes- no calculator- no phone- no computer

Linked Structures. See Section 3.2 of the text.

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

Object Oriented Programming COP3330 / CGS5409

CS 206 Introduction to Computer Science II

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Basic Data Structures

ECE 242 Data Structures and Algorithms. Arrays. Lecture 1. Prof. Eric Polizzi

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Name CPTR246 Spring '17 (100 total points) Exam 3

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

Basic Data Structures 1 / 24

CE 221 Data Structures and Algorithms

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

A queue is a linear collection whose elements are added on one end and removed from the other

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

WITH SOLUTIONS!!! WARNING: THIS COPY CONTAINS SOLUTIONS!!! COMP103 Introduction to Data Structures and Algorithms

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

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)

Week 6. Data structures

Data Abstraction and Specification of ADTs

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

Model Solutions. COMP 103: Test April, 2013

CSEN 301 Data Structures and Algorithms

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


Abstract Data Types. Abstract Data Types

COS 226 Algorithms and Data Structures Fall Midterm

CMSC 132: Object-Oriented Programming II. Stack and Queue

CSE 214 Computer Science II Heaps and Priority Queues

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Stack and Queue. Stack:

Name Section Number. CS210 Exam #2 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Data Structures and Algorithms. Roberto Sebastiani

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

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

Data Structures G5029

cs2010: algorithms and data structures

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

Introduction to Linked Data Structures

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

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

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

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

ECE 242 Data Structures and Algorithms. Hash Tables II. Lecture 25. Prof.

Data Structures and Algorithms Winter Semester

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

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

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

Priority Queue. How to implement a Priority Queue? queue. priority queue

Please note that if you write the mid term in pencil, you will not be allowed to submit a remark request.

CS 455 MT 2 Sample Questions

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

STANDARD ADTS Lecture 17 CS2110 Spring 2013

Abstract Data Type: Stack

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination T09:00

Interfaces & Generics

ECE Fall st Midterm Examination (120 Minutes, closed book)

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

More Data Structures (Part 1) Stacks

Midterm Exam (REGULAR SECTION)

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

Summer Final Exam Review Session August 5, 2009

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

COMP 103 : Test. 2018, Sept 12//(Corrected)

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Model Solutions. COMP 103: Test May, 2013

Programming Languages and Techniques (CIS120)

PRIORITY QUEUES AND HEAPS

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Complexity, General. Standard approach: count the number of primitive operations executed.

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Data Structures and Algorithms Winter term 2012

CS2012 Programming Techniques II

COMP 103 Introduction to Data Structures and Algorithms

16-Dec-10. Collections & Data Structures ABSTRACTION VS. IMPLEMENTATION. Abstraction vs. Implementation

Transcription:

ECE 242 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece242/ Linked List I Lecture 10 Prof. Eric Polizzi

Logistics ECE242 Recommendations The goal of projects is to help you practice and master the concepts that we see in class. Projects aim at providing a rewarding and useful experience. However, focusing your efforts only on projects will not be enough to succeed in the 242 exams (mid term, and final). Exams will be heavily based on your understanding of the lecture notes, textbook, and HW practices. In order to become more comfortable with the class materials, I encourage you to: Practice the HW before discussion Ask questions to TA (by emails, in discussion, in Office hours) Get a complementary look at the textbook 2

The big picture... so far DATA STRUCTURES ALGORITHMS Array Linked List Unordered array (unsorted list) Ordered array Ordered Array (sorted list) Stacks Queues Priority Q insert, delete, linear search, shuffle, sorting insert, delete, shuffle, binary search push, pop, peek, etc. enqueue, dequeue, peek, etc. 3

Arrays Several advantages: Easy to access a particular item... array[i] Insertion is fast in unordered array Searching is fast in ordered array Data locality in memory (good for performance in scientific computing) Main disadvantages: Some algorithms require large number of shifting/moves... costly The size of an array cannot be changed once it is created... lack of flexibility If the size is too large... waste of memory If the size is too small, it cannot be expanded dynamically. 4

Linked list Versatile mechanism that can be used in place of an array based implementation for various data structures (list, stack, queues, etc.) Take as much memory as needed (allocate memory dynamically if needed) No need to move items around In a linked list each item is embedded in a link (sometimes called a node), and each link contains a reference to the link. We obtain a chain of objects that are connected link link link a b c a b c : Items/objects : reference 5

Linked list Succession of links from first link to the last link that points to reference The class Link contains some data and a reference to the link The class Linked List contains the reference to the first link and all major methods that operate on the list (ex: add, remove, display, search, etc.) 6

Linked list: Class Link Remark: in Java (2 steps to define an object) MyObject o; // 1- Creation of reference/pointer to object o= new MyObject();// 2- Creation of the actual object 'new' Definition of Class Link (example with two data items: String and int) class Link { public String name; // data public int age; // data public Link ; // reference to link // Constructor public Link(String name,int age){ this.name=name; this.age=age; = ; // optional This class is self referential since it contains a field () of the same type as itself 7

Linked list: Example with 2 links Link 1stlink = new Link(name1,age1); data name1 age1 Link 2ndlink = new Link(name2,age2); name2 age2 1stlink. = 2ndlink; name1 age1 name2 age2 reference to link 8

Linked list: Consequences Move from one link to another by references Number of links can be expanded dynamically The objects/items of the link can be anywhere in memory (no data locality) Insertion/removal does not require moving items a b c e d To do: Test Java applet LinkList.html 9

Linked list: Class LinkList Creation of a Linked list for the objects data defined in the class Link class LinkList { // variables // constructors // methods private Link first;// Reference to the first // link on the list public LinkList() { first = ; // no item in the list yet public boolean isempty() { return(first==);//true if list is empty //////////// other methods such as: public void insertfirst(string name,int age){ // insert new link at the beginning public Link deletefirst() {// to complete public void displaylist() {// to complete 10

Linked list: insertfirst method Before insertion LinkList first Han 40 Luke 20 Obiwan 55 After Insertion LinkList first Han 40 Luke 20 Obiwan 55 2 Yoda 400 1 11

Linked list: insertfirst method // insert at the beginning of the list public void insertfirst(string name,int age) { Link newlink = new Link(name, age); // create new link newlink. = first; // newlink old first (step 1) first = newlink; // first newlink (step 2) LinkList first Han 40 Luke 20 Obiwan 55 2 Yoda 400 1 12

Linked list: deletefirst method Before deletion (at the beginning of the list) LinkList first Han 40 Luke 20 Obiwan 55 After deletion LinkList first Luke 20 Obiwan 55 Remark: the object removed from the list, still exists in memory somewhere, but it is not the user responsibility to deal with it (garbage collection process) 13

Linked list: deletefirst method // delete at the beginning of the list and return first Link public Link deletefirst(){ if (!isempty()){ // check that the list is not empty Link temp = first; // save reference of first link first = first.; // first old (remove link) return temp; // return deleted link else return ; After deletion LinkList first Luke 20 Obiwan 55 Remark: the object removed from the list, still exists in memory somewhere, but it is not the user responsibility to deal with it (garbage collection process) 14

Linked list: displaylist method 'current' probe moves from link to link using current=current.; LinkL ist first Han 40 Luke 20 Obiwan 55 LinkL ist first Han 40 Luke 20 Obiwan 55 current current public void displaylist() { Link current = first; // start at the beginning of the list while (current!=) // until the end of the list { System.out.println(current.name+"'s age is "+current.age); current = current.; // move to Link 15

Linked List Application Example class LinkListApp { public static void main(string[] args) { LinkList mylist = new LinkList(); mylist.insertfirst("obiwan",55); mylist.insertfirst("luke",20); mylist.insertfirst("han",40); mylist.displaylist(); while(!mylist.isempty()){ Link del = mylist.deletefirst(); System.out.println("link of "+del.name+" deleted"); Han's age is 40 Luke's age is 20 Obiwan's age is 55 link of Han deleted link of Luke deleted link of Obiwan deleted 16