Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
Chapter 24 Lists, Stacks, and Queues
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Review Learn about linked lists
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Summary of lectures (1 to 11)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Lecture 5 of Computer Science II Arrays Instructor: Mr.Ahmed Al Astal.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Data Structures Using Java1 Chapter 4 Linked Lists.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
LINKED LISTS CSCD Linked Lists.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
" A list is only as strong as its weakest link. " - Donald Knuth
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS2013 Lecture 4 John Hurley Cal State LA.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
Linked Lists & Iterators
CS210- Lecture 6 Jun 13, 2005 Announcements
Arrays © 2014 Goodrich, Tamassia, Goldwasser Arrays Vectors
Problem Understanding
Presentation transcript:

Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1

Part 1: Arrays Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 2

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 value stored in that cell. The cells of an array, A, are numbered 0, 1, 2, and so on. Each value stored in an array is often called an element of that array. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 3 A 012n i

Array Length and Capacity Since the length of an array determines the maximum number of things that can be stored in the array, we will sometimes refer to the length of an array as its capacity. In Java, the length of an array named a can be accessed using the syntax a.length. Thus, the cells of an array, a, are numbered 0, 1, 2, , a.length−1, and the cell with index k can be accessed with syntax a[k]. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 4 a 012n k

Declaring Arrays (first way) Use an assignment to a literal form when initially declaring the array, using a syntax as: The elementType can be any Java primitive type or class name, and arrayName can be any valid Java identifier. The initial values must be of the same type as the array. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 5

Declaring Arrays (second way) Use the new operator. o However, because an array is not an instance of a class, we do not use a typical constructor. Instead we use the syntax: new elementType[length] length is a positive integer denoting the length of the new array. The new operator returns a reference to the new array, and typically this would be assigned to an array variable. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 6

Arrays of Characters or Object References An array can store primitive elements, such as characters. An array can also store references to objects. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 7

Java Example: Game Entries A game entry stores the name of a player and her best score so far in a game Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 8

Java Example: Scoreboard Keep track of players and their best scores in an array, board o The elements of board are objects of class GameEntry o Array board is sorted by score Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 9

Adding an Entry To add an entry e into array board at index i, we need to make room for it by shifting forward the n - i entries board[i], …, board[n – 1] Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 10 board 012n i 012n i 012n e i

Java Example Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 11

Removing an Entry To remove the entry e at index i, we need to fill the hole left by e by shifting backward the n - i - 1 elements board[i + 1], …, board[n – 1] Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists n i 012n e i 012n i board

Java Example Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 13

Multidimensional arrays Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 14 int[ ][ ] data = new int [2][3]; data = { {6, 13, 17}, {45, 67, 82} }; data[0][2] = 3; // 17 replaced by 3 String[][] irregular2DArray = { {“Friends:”, “Merry”, “Bob”, “Chris”}, {“Games:”, “baseball”, “soccer”}, {“Places:”, “Toronto”, “Boston”, “Barcelona”, “Beijing”} };

Example: Pascal Triangle Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 15

Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 16 Example: Pascal Triangle output

Some Built-in Methods in java.util.Arrays Class Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 17

Part 1: Summary Arrays: o Definition o Declaration o Element types o Java examples o Adding & removing entries o Multidimensional arrays o Built-in Methods in java.util.Arrays Class Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 18

Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 19

Part 2: Singly Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 20

Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes, starting from a head pointer Each node stores o element o link to the next node EECS2011: Arrays & Linked Lists 21 next element node ABCD  head Last Update: Aug 21, 2014

A Nested Node Class EECS2011: Arrays & Linked Lists 22 Last Update: Aug 21, 2014

Accessor Methods EECS2011: Arrays & Linked Lists 23 Last Update: Aug 21, 2014

Inserting at the Head a)Current list b)Allocate new node Insert new element Have new node point to old head c)Update head to point to new node EECS2011: Arrays & Linked Lists 24 Last Update: Aug 21, 2014

Inserting at the Tail a)Current list b)Allocate a new node Insert new element Have new node point to null c)Have old last node point to new node Update tail to point to new node EECS2011: Arrays & Linked Lists 25 Last Update: Aug 21, 2014

Java Methods EECS2011: Arrays & Linked Lists 26 Last Update: Aug 21, 2014

Removing at the Head a)Current list b)Update head to point to next node in the list c)Allow garbage collector to reclaim the former first node EECS2011: Arrays & Linked Lists 27 Last Update: Aug 21, 2014

Java Method EECS2011: Arrays & Linked Lists 28 Last Update: Aug 21, 2014

Removing at the Tail Removing at the tail of a singly linked list is not efficient! It’s impossible in constant-time to access the predecessor of the tail node. You need linear-time to scan through the list from head to tail. EECS2011: Arrays & Linked Lists 29 Last Update: Aug 21, 2014

Part 2: Summary Singly linked list structure A nested node class Accessor methods Insertion: o at the tail o at the head Removing o at the head o at the tail - inefficient EECS2011: Arrays & Linked Lists 30 Last Update: Aug 21, 2014

EECS2011: Arrays & Linked Lists 31 Last Update: Aug 21, 2014

Part 3: Doubly Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 32

Doubly Linked List A doubly linked list can be traversed forward and backward Nodes store: o element o link to the previous node o link to the next node Special trailer and header nodes Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 33 trailer header nodes/positions elements prevnext element node

Insertion Insert a new node, q, between p and its successor. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 34 ABXC ABC p ABC p X q pq

Deletion Remove a node, p, from a doubly linked list. Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists 35 ABCD p ABC D p ABC

Doubly-Linked List in Java Last Update: Aug 21, 2014 EECS2011: Arrays & Linked Lists 36

Doubly-Linked List in Java, 2 Last Update: Aug 21, 2014 EECS2011: Arrays & Linked Lists 37

Doubly-Linked List in Java, 3 Last Update: Aug 21, 2014 EECS2011: Arrays & Linked Lists 38

Doubly-Linked List in Java, 4 Last Update: Aug 21, 2014 EECS2011: Arrays & Linked Lists 39

Circular Linked Lists We can form a circular linked list by identifying the header and trailer sentinel nodes. Varieties of Circular Linked Lists: – Singly Linked Circular Lists (SLCL). – Doubly Linked Circular Lists (DLCL). With DLCL it is possible to perform the following operations in constant time: – Inserting an item at any given position in the list. – Deleting an item from any given position in the list. – Concatenating two lists of same type (by interlacing). EECS2011: Arrays & Linked Lists 40 Last Update: Aug 21, 2014

Part 3: Summary Doubly linked list structure A nested node class Accessor methods Insertion at any given position Removal of any given node Singly & Doubly Linked Circular Lists EECS2011: Arrays & Linked Lists 41 Last Update: Aug 21, 2014

EECS2011: Arrays & Linked Lists 42