CSE 326: Data Structures Lecture #5 Alon Halevy Spring Quarter 2001
Implementations of Linked Lists abc (a b c) (optional header) L W1ISES Array: Linked list: HAY Can we apply binary search to an array representation?
Linked List vs. Array Find(item) = position Find_Kth(integer)=item Find_Kth(1)=item Insert(item, position) Insert(item) Delete(position) Next(position) = position linked listarraysorted array
Tradeoffs For what kinds of applications is a linked list best? Examples for an unsorted array? Examples for a sorted array?
Implementing in C++ Create separate classes for –Node –List (contains a pointer to the first node) –List Iterator (specifies a position in a list; basically, just a pointer to a node) Pro: syntactically distinguishes uses of node pointers Con: a lot of verbage! Also, is a position in a list really distinct from a list? abc (a b c) (optional header) L
Other Data Structures for Lists Doubly Linked List Circular List cdef
Implementing Linked Lists Using Arrays “Cursor implementation” Ch Often useful in any language Can use same array to manage a second list of unused cells FOARNRT Data Next First = 2
Application: Polynomial ADT A i is the coefficient of the x n-i term: 3x 2 + 2x + 5 ( ) 8x + 7 ( 8 7 ) x ( ) Problem?
3x ( )
Sparse List Data Structure: 3x ( )
Addition of Two Polynomials Similar to merging two sorted lists – O(n+m) x 50 +3x p x 50 +4x q r
Multiple Linked Lists Many ADTS such as graphs, relations, sparse matrices, multivariate polynomials use multiple linked lists Several options –array of lists –lists of lists –multi lists General principle throughout the course: use one ADT to implement a more complicated one.
Array of Linked Lists: Adjacency List for Graphs G Array G of unordered linked lists Each list entry corresponds to an edge in the graph
Reachability by Marking Suppose we want to mark all the nodes in the graph which are reachable from a given node k. –Let G[1..n] be the adjacency list rep. of the graph –Let M[1..n] be the mark array, initially all false s. mark(int i){ M[i] = true; x = G[i] while (x != NULL) { if (M[x->node] == false) mark(G[x->node]) x = x->next }
Multi-Lists Suppose we have a set of movies and cinemas, and we want a structure that stores which movies are playing where.
More on Multi-Lists What if we also want to store the playing times of movies?