1 / 46

CSE 326: Data Structures Part Two: Lists

CSE 326: Data Structures Part Two: Lists. Henry Kautz Autumn Quarter 2002. Today. Abstract versus Concrete Data Types List ADT Iterators Comparing implementations Sparse vectors Nested lists Sparse arrays Expressions. Abstract vs. Concrete Data Types. Abstract Data Type (ADT)

jwimberly
Download Presentation

CSE 326: Data Structures Part Two: Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 326: Data StructuresPart Two: Lists Henry Kautz Autumn Quarter 2002

  2. Today • Abstract versus Concrete Data Types • List ADT • Iterators • Comparing implementations • Sparse vectors • Nested lists • Sparse arrays • Expressions

  3. Abstract vs. Concrete Data Types • Abstract Data Type (ADT) • Mathematical description of an object and the set of operations on the object • List, Stack, Tree, Heap, Graph, … • One ADT may specialize another ADT • One ADT may implement another ADT • Concrete Data Type • Implementation of an ADT using some set of primitive data types and operations of known complexity • Primitives: integers, arrays, pointers or references • Object-oriented programming languages (Java, C++) let you explicitly define new concrete data types that correspond to ADT’s.

  4. List ADT ( A1 A2 … An-1 An ) length = n • Mathematical description: a sequence of items • Ai precedes Ai+1 for 1  i < n • Operations • First() = position • Value(position) = item • Next(position) = position • Length() = integer • Insert(item,position) • Delete(position) What other operations might be useful?

  5. List ADT ( A1 A2 … An-1 An ) length = n • Mathematical description: a sequence of items • Ai precedes Ai+1 for 1  i < n • Operations • First() = position • Value(position) = item • Next(position) = position • Length() = integer • Insert(item,position) • Delete(position) • What other operations might be useful? • Kth(integer)=item • SetKth(item,integer) • Find(item)=position

  6. Specialization Hierarchy ListProperty: Sequence First()=pos Value(pos)=item Kth(integer)=itemNext(pos)=pos Length()=integer SetKth(item,integer)Insert(item,pos) Delete(pos) Find(item)=position StackProperty: LIFO Push(item)Pop()=itemIsEmpty()=true/false QueueProperty: FIFO Enqueue(item)Dequeue()=itemIsEmpty()=true/false VectorProperty: random access Kth(int) = itemSetKth(item,integer)

  7. Implementation Hierarchy ListComplexity: Unspecified First()=pos Value(pos)=item Kth(integer)=itemNext(pos)=pos Length()=integer SetKth(item,integer)Insert(item,pos) Delete(pos) Find(item)=position Linked List(1) for: (n) for: Array(1) for: (n) for:

  8. Specialization and Implementation Hierarchies List Stack Queue Vector Sorted Vector Linked List

  9. Concrete Data Types List b c  Linked List What’s an alternative implementation? Linked List using References nodeB.value = “b”;nodeC.value = “c”;list = nodeB;nodeB.next = nodeC

  10. Concrete Data Types List b c  Linked List Linked List using References Linked List using Arrays list = 4; nodeB.value = “b”;nodeC.value = “c”;list = nodeB;nodeB.next = nodeC 1 2 3 4 5

  11. Linked Lists in C a b c  struct node{ Object element; struct node * next; } Everything else is a pointer to a node! typedef stuct node * List; typedef struct node * Position; L

  12. Linked Lists in Java – version 1 • References to objects are implicit pointers class ListNode{ Object element; ListNode next; } class List{ Listnode head; Listnode find(Object item) { Listnode n = head; while (n != null) { if (n.element == item) return n; } return null; }

  13. Data Hiding • Good programming style hides internal details of an object from the rest of the program • Guarantees that data structure always works as expected – cannot easily be corrupted • Here, must make details of ListNode and List public • Type returned by find • For iterating through a list: ListNode n; for (n = mylist.head; n!= null; n = n.next){ v = n.element; do something on each v }

  14. Iterators • Introduce a new public class to explicitly represent a position in a list public class LinkedListItr { ListNode current; public Object retrieve() { return current.element; } public void advance() { current = current.next; } • Then: LinkedListItr i; for (i = mylist.first(); !i.pastEnd(); i.advance){ do something on each v.retrieve() }

  15. Abstract Iterators • Iterators can also be defined for an array implementation of lists: public class ArrayListItr { Object [] data; integer current; public Object retrieve() { return data[current]; } public void advance() { current = current+1; } • We can create an abstract iterator that works for both linked list and array implements of List

  16. Abstract Iterator abstract class ListItr { abstract Object retrieve(); abstract void advance(); … } class LinkedListItr extends ListItr { … } class ArrayListItr extends ListItr { … } • Why do this?

  17. Array Implementation of Linked Lists 1 7 9 2 3 4 5 6 8 10 Data F O A R N R T Next 3 8 6 4 -1 10 5 First = 2 • How do we implement • Delete(position) ? • Insert(element, position)?

  18. Free Cell Management 1 7 9 2 3 4 5 6 8 10 Data F O A R N R T Next 7 9 0 3 8 6 4 -1 10 5 First = 2 Free = 1 When an item is removed from the list, must “reclaim” the unused cell for later use Can use same array to manage a second list of unused cells

  19. Memory Management • Keeping a free cell list is an example of a memory management strategy • How is memory managed in C? • C++? • Java?

  20. Summary: Complexity

  21. To ADT or NOT to ADT? • Issue: when to bypass / expand List ADT? • Using general list (stack) operations: List reverse(List x) { y = new List; while (! x.isEmpty()) y.Push( x.Pop() ) return y; } Disadvantages?

  22. Destructive Method a b c  x Reverse() { ListNode x, last, tmp; x = head; last = null; while (x.next != null){ tmp = x.next; x.next = last; last = x; x = tmp;} head = x; } a  b c x Faster in practice? Asymptotically faster?

  23. Slow Reverse List reverse(List x) { y = new List; for (i=1; i<=x.length(); i++){ y.Push( x.Kth(i) ) } return y; }

  24. ( 5 2 3 ) 5 + 2x + 3x2 ( 7 8 ) 7 + 8x ( 3 0 2 ) 3 + x2 List ADT Polynomial ADT Possible linked list implementation: Ai is the coefficient of the xi-1 term: Problem?

  25. 4 + 3x2001 ( 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 )

  26. 4 0 3 2001 Sparse Vector Data Structure:4 + 3x2001 (<4 0> <2001 3>) class Term { int coeff, power; } class PolyNode extends ListNode { Term data; } class Polynomial extends List { Polynode header; … }

  27. Addition of Two Polynomials? Complexity? 15+10x50+3x1200 p 15 0 10 50 3 1200 5+30x50+4x100 q 5 0 30 50 4 100

  28. Addition of Two Polynomials • One pass down each list: (n+m) 15+10x50+3x1200 p 15 0 10 50 3 1200 5+30x50+4x100 q 5 0 30 50 4 100 r 20 0 40 50 4 100 3 1200

  29. Sparse Matrices • How could we represent this compactly?

  30. Sparse Matrices • How could we represent this compactly? ( (row (column data) (column data) …) (row ( (column data) (column data) …) … ) ( (1 (1 18) (2 33)(4 (4 99))(5 (5 27)) )

  31. Nested Polymorphic Lists • Polymorphic – elements in list may be of different types • Trivial in Java, doable but painful in C++ • Nested – elements of a list is are lists • Nested polymorphic list – elements of list may be of non-list types (e.g. int) or lists

  32. “Universal” Data Structure • NPL’s can be used to implement any of the other linked data structures we’ll cover in the course • Trees, heaps, graphs, disjoint sets • LISP – programming language that uses NPL’s to represent both data and programs • LISP pioneered (in the 1950’s!) many ideas that are the basis of modern programming languages • Recursion, garbage collection, interactive programming environments • JAVA combines the best features of LISP with the best features of C++ (strict type checking, objects and inheritance)

  33. Programs As Lists (progn (setq x 10) (setq y 1) (while (greater-than x 1) (progn (setq y (times y x)) (setq x (minus x 1)))))

  34. -Lisp • Let’s see how easy it is to build our own programming language interpreter… (plus (times (minus 6 2) 14) 8) • Grammar for an expression: expression :: = integer | “(“ symbol {expression}* “)” symbol ::= [a-z]+ repeat 0 or more times repeat 1 or more times

  35. Lists • For these slides: convention that head field of list points directly to first node of list (no dummy node) class Node { Object element; Node next; } class List { Node head; … }

  36. Read/Eval/Print Loop • The top level program: While (not EOF) Print( Eval( ReadExpression()))

  37. Reading Expressions see Weiss for tokenizer ReadExpression() { t = GetToken(); if (t is numeric) return new Integer(t); if (t is “(“)) return ReadList(); if (t is “)”) return null; else return new Symbol(t); }

  38. ReadList ReadList() { e = ReadExpression(); if (e == null) return a new empty List; else return Push( e, ReadList() ); } Why is the recursive call inside the Push?

  39. Evaluating Expressions Eval( e ){ if (e is an Integer) return its value; if (e is a List) f = e.head.element; return Apply(f, EvalList(e.head.next)); else error; }

  40. EvalList EvalList(Node n){ if (n == null) return a new empty list; else return Push( Eval(n.element), EvalList(n.next) ); }

  41. Apply Apply(f, params){ if (f is “plus”) return sum of params else if (f is “minus”) return difference of params else … } params is always a list of ints – it has been fully evaluated!

  42. That’s It! • 33 lines of pseudo-code • About 100 lines of Java • What about variables? If statements? Loops?

  43. Adding Variables • Keep an symbol table list of variable/value pairs ( (a 15) (b 22) (c –2) ) • Add to Eval: • to evaluate a variable, find it in the symbol table and return it’s value – if it is not the table, error. • to set a variable, check for the special form (set symbol expression)

  44. Eval with Variables Eval( e ){ if (e is an Integer) return its value; if (e is a List) f = e.head.element; if (f is “set”){ var = e.head.next.element; val = e.head.next.next.element; Put (var,val) in symbol table; } else return Apply(f, EvalList(e.head.next)) else return e’s value in symbol table; }

  45. Control Flow • Sequence (prog exp1 exp2 …) • In Apply, return value of last parameter • Conditionals (if exp1 exp2 exp3) • In Eval, if exp1 is not 0, evaluate exp2, otherwise exp3 • Loops (loop exp1 exp2) • In Eval, if exp1 is not 0, evaluate exp2, then repeat

  46. Coming Up • Sorting • Weiss Chapter 7 • Know how the following work: • Bubble Sort (selection sort) • Merge Sort • Quicksort

More Related