Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
© 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.
© 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.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
© 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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Linear Data Structures
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
Lecture4: Arrays Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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)
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
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.
Parasol Lab, Dept. CSE, Texas A&M University
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
Linked List, Stacks Queues
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.
Ch7. List and Iterator ADTs
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
COMP9024: Data Structures and Algorithms
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Data Structures Linked list.
LINKED LISTS CSCD Linked Lists.
Array Lists, Node Lists & Sequences
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,
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Stacks.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010

Abstract List Data Structures Array Lists, Linked Lists, and Doubly Linked Lists

Array List  The Array List ADT extends the notion of array by storing a sequence of arbitrary objects  An element can be accessed, inserted or removed by specifying its index (number of elements preceding it)  An exception is thrown if an incorrect index is given (e.g., a negative index)

Array List  Main methods:  get(integer i): returns the element at index i without removing it  set(integer i, object o): replace the element at index i with o and return the old element  add(integer i, object o): insert a new element o to have index i  remove(integer i): removes and returns the element at index i  Additional methods:  size()  isEmpty()

Array-based Implementation  Use an array A of size N  A variable n keeps track of the size of the array list (number of elements stored)  Operation get ( i ) is implemented in O(1) time by returning A[i]  Operation set ( i,o ) is implemented in O(1) time by performing t = A[i], A[i] = o, and returning t.

Insertion  In operation add ( i, o ), we need to make room for the new element by shifting forward the n  i elements A[i], …, A[n  1]  In the worst case ( i  0 ), this takes O(n) time A 012n i A 012n i A 012n o i

Element Removal  In operation remove (i), we need to fill the hole left by the removed element by shifting backward the n  i  1 elements A[i  1], …, A[n  1]  In the worst case ( i  0 ), this takes O(n) time A 012n i A 012n o i A 012n i

Performance  In the array based implementation of an array list:  The space used by the data structure is O(n)  size, isEmpty, get and set run in O(1) time  add and remove run in O(n) time in worst case  In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

Growable Array-based Array List  In an add(o) operation (without an index), we always add at the end  When the array is full, we replace the array with a larger one  How large should the new array be?  Incremental strategy: increase the size by a constant c  Doubling strategy: double the size

Growable Array-based Array List Algorithm add(o) if t = S.length  1 then A  new array of size … for i  0 to n  1 do A[i]  S[i] S  A n  n + 1 S[n  1]  o //create a new array A (larger than S) //copy the elements in S to A //increase the size by 1 //add o as the last element //Replace S with A

Comparison of the Strategies  We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n add(o) operations  We assume that we start with an empty stack represented by an array of size 1  We call amortized time of an add operation the average time taken by an add over the series of operations, i.e., T(n)/n

Incremental Strategy  We replace the array k = n / c times  The total time T(n) of a series of n add operations is proportional to n + c + 2c + 3c + 4c + … + kc  Since c is a constant, T(n) is O(n + k 2 ), i.e., O(n 2 )  The amortized time of an add operation is O(n)

Doubling Strategy  We replace the array k = log 2 n times  The total time T(n) of a series of n add operations is proportional to n …+ 2 k = 3n  1  T(n) is O(n)  The amortized time of an add operation is O(1) geometric series

Singly Linked List  A concrete data structure consisting of a sequence of nodes  Each node stores  element  link to the next node next elem node ABCD 

The Node Class for Singly Linked List public class Node{ private Object element; private Node next; public Node(){ this ( null, null ); } public Node(Object e, Node n) { element = e; next = n; } public Object getElement() { return element; } public Node getNext() { return next; } public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; } // Instance variables // Creates a node with null // references to its element and next node. // Creates a node with the given element // and next node. // Accessor methods // Modifier methods

Inserting at the Head 1.Allocate a new node 2.Insert new element 3.Have new node point to old head 4.Update head to point to new node

Removing at the Head 1.Update head to point to next node in the list 2.Allow garbage collector to reclaim the former first node

Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node

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

Stack as a Linked List  We can implement a stack with a singly linked list  The top element is stored at the first node of the list  The space used is O(n) and each operation of the Stack ADT takes O(1) time  t nodes elements

Queue as a Linked List  We can implement a queue with a singly linked list  The front element is stored at the first node  The rear element is stored at the last node  The space used is O(n) and each operation of the Queue ADT takes O(1) time 21 f r  nodes elements

Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored  It gives a unified view of diverse ways of storing data, such as  a cell of an array  a node of a linked list  Just one method:  object element(): returns the element stored at the position

Node List ADT  The Node List ADT models a sequence of positions storing arbitrary objects  It establishes a before/after relation between positions Generic methods: size(), isEmpty() Accessor methods: first(), last() prev(p), next(p) Update methods: set(p, e) addBefore(p, e), addAfter(p, e), addFirst(e), addLast(e) remove(p)

Doubly Linked List  A doubly linked list provides a natural implementation of the Node List ADT  Nodes implement Position and store:  element  link to the previous node  link to the next node  Special trailer and header nodes prevnext elem node

Doubly Linked List trailer header nodes/positions elements

Insertion  We visualize operation insertAfter(p, X), which returns position q ABXC ABC ABC p X q pq

Insertion Algorithm Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p)//link v to its predecessor v.setNext(p.getNext())//link v to its successor (p.getNext()).setPrev(v)//link p’s old successor to v p.setNext(v)//link p to its new successor, v return v//the position for the element e

Deletion  We visualize remove(p), where p = last() ABCD p ABC D p ABC

Deletion Algorithm Algorithm remove(p): t = p.element//a temporary variable to hold the return value (p.getPrev()).setNext(p.getNext())//linking out p (p.getNext()).setPrev(p.getPrev()) p.setPrev( null )//invalidating the position p p.setNext( null ) return t

HW4 (Due on 10/14) Maintain an ordered keyword list.  A keyword is a pair [String name, Integer count]  Keep the list in order by its count while adding or deleting elements  For the list structure, you can  Use java.util.ArrayList, or  java.util.LinkedList, or  Develop it by yourself  Given a sequence of operations in a txt file, parse the txt file and execute each operation accordingly

Add and Output operationsdescription add(Keyword k)Insert k to the list in order outputIndex(int i)Output the ith keyword in the list outputCount(int c)Output all keywords whose count is equal to c outputHas(string s)Output all keywords whose name contains s outputName(string s)Output all keywords whose name is equal to s outputFirstN(int n)Output the first n keywords outputAll()Output the whole list

Add in order add Fang 3 add Yu 5 add NCCU 3 add UCSB 2 … Fang, 3  Yu, 5Fang, 3  Yu, 5NCCU, 3Fang, 3 

Output operations Yu, 5NCCU, 3Fang, 3UCSB, 2  … outputCount 3 outputName Yu outputHas U outputIndex 1 outputFirstN 2 outputAll … [NCCU, 3] [Fang, 3] [Yu, 5] [NCCU, 3] [UCSB, 2] [Yu, 5] [Yu, 5] [NCCU,3] [Yu, 5] [NCCU, 3] [Fang, 3] [UCSB, 2]

Delete operationsdescription deleteIndex(int i)Delete the ith keyword in the list deleteCount(int c)Delete all keywords whose count is equal to c deleteHas(string s)Delete all keywords whose name contains s deleteName(string s)Delete all keywords whose name is equal to s deleteFirst(int n)Delete the first n keywords

Delete operations Yu, 5NCCU, 3Fang, 3UCSB, 2  deleteCount 3 deleteName Yu deleteHas U deleteIndex 1 deleteFirstN 2 deleteAll

An input file add Fang 3 add Yu 5 add NCCU 3 add UCSB 2 add MIS 2 add Badminton 4 add Food 3 add Data 3 add Structure 4 outputAll deleteCount 3 outputCount 2 outputName Yu deleteName Yu outputHas a deleteHas a outputIndex 2 deleteIndex 4 deleteFirstN 1 outputFirstN 3 deleteAll 1. You need to read the sequence of operations from a txt file 2. The format is firm 3. Raise an exception if the input does not match the format