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.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

© 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.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
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.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 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
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
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,
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.
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.
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Linked List, Stacks Queues
Sorted Maps © 2014 Goodrich, Tamassia, Goldwasser Skip Lists.
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,
Skip Lists 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Lecture 5 of Computer Science II
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Data Structure By Amee Trivedi.
Ch7. List and Iterator ADTs
Lectures linked lists Chapter 6 of textbook
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,
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CE 221 Data Structures and Algorithms
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Chapter 4 Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
LINKED LISTS CSCD Linked Lists.
Ch7. List and Iterator ADTs
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/22/2018 3:25 AM Doubly-Linked Lists Doubly-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
Array-Based Sequences
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Doubly Linked Lists or Two-way Linked Lists
CS212D: Data Structures Week 5-6 Linked List.
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.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
Linked Lists.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Linked Lists.
Arrays © 2014 Goodrich, Tamassia, Goldwasser Arrays Vectors
Chapter 9 Linked Lists.
Problem Understanding
Presentation transcript:

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 M. H. Goldwasser, Wiley, 2014 Arrays © 2014 Goodrich, Tamassia, Goldwasser 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 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. A 1 2 i n © 2014 Goodrich, Tamassia, Goldwasser Arrays

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, and so on, up through a.length−1, and the cell with index k can be accessed with syntax a[k]. a 1 2 k n © 2014 Goodrich, Tamassia, Goldwasser Arrays

Declaring Arrays (first way) The first way to create an array is to use an assignment to a literal form when initially declaring the array, using a syntax as: The elementType can be any Java base type or class name, and arrayName can be any valid Java identifier. The initial values must be of the same type as the array. © 2014 Goodrich, Tamassia, Goldwasser Arrays

Declaring Arrays (second way) The second way to create an array is to use the new operator. 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. © 2014 Goodrich, Tamassia, Goldwasser Arrays

Arrays of Characters or Object References An array can store primitive elements, such as characters. An array can also store references to objects. © 2014 Goodrich, Tamassia, Goldwasser Arrays

Java Example: Game Entries A game entry stores the name of a player and her best score so far in a game © 2014 Goodrich, Tamassia, Goldwasser Arrays

Java Example: Scoreboard Keep track of players and their best scores in an array, board The elements of board are objects of class GameEntry Array board is sorted by score © 2014 Goodrich, Tamassia, Goldwasser Arrays

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] board 1 2 i n board 1 2 i n board e 1 2 i n © 2014 Goodrich, Tamassia, Goldwasser Arrays

Java Example © 2014 Goodrich, Tamassia, Goldwasser Arrays

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] board e 1 2 i n board 1 2 i n board 1 2 i n © 2014 Goodrich, Tamassia, Goldwasser Arrays

Java Example © 2014 Goodrich, Tamassia, Goldwasser Arrays

Doubly Linked Lists 5/31/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Doubly Linked Lists © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Doubly Linked List A doubly linked list can be traversed forward and backward Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next element node trailer header nodes/positions elements © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Insertion p A B C p q A B C X p q A B X C Insert a new node, q, between p and its successor. p A B C p q A B C X p q A B X C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Deletion A B C D p A B C p D A B C Remove a node, p, from a doubly linked list. A B C D p A B C p D A B C © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Doubly-Linked List in Java © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Doubly-Linked List in Java, 2 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Doubly-Linked List in Java, 3 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Doubly-Linked List in Java, 4 © 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists

Sequences 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 M. H. Goldwasser, Wiley, 2014 Singly Linked Lists Singly Linked Lists

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 element link to the next node next node element head  A B C D Singly Linked Lists

A Nested Node Class Singly Linked Lists

Accessor Methods Singly Linked Lists

Inserting at the Head Allocate new node Insert new element Have new node point to old head Update head to point to new node Singly Linked Lists

Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node Singly Linked Lists

Java Methods Singly Linked Lists

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node Singly Linked Lists

Java Method Singly Linked Lists

Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node Singly Linked Lists