Download presentation
Presentation is loading. Please wait.
Published byElvin Goodman Modified over 9 years ago
1
CSE 326: Data Structures Part 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 Mathematical description: a sequence of items –A i precedes A i+1 for 1 i < n Operations –First() = position –Value(position) = item –Next(position) = position –Length() = integer –Insert(item,position) –Delete(position) ( A 1 A 2 … A n-1 A n ) length = n What other operations might be useful?
5
List ADT Mathematical description: a sequence of items –A i precedes A i+1 for 1 i < n Operations –First() = position –Value(position) = item –Next(position) = position –Length() = integer –Insert(item,position) –Delete(position) ( A 1 A 2 … A n-1 A n ) length = n What other operations might be useful? Kth(integer)=item SetKth(item,integer) Find(item)=position
6
Specialization Hierarchy List Property: Sequence First()=posValue(pos)=itemKth(integer)=item Next(pos)=posLength()=integerSetKth(item,integer) Insert(item,pos)Delete(pos)Find(item)=position Stack Property: LIFO Push(item) Pop()=item IsEmpty()=true/false Queue Property: FIFO Enqueue(item) Dequeue()=item IsEmpty()=true/false Vector Property: random access Kth(int) = item SetKth(item,integer)
7
Implementation Hierarchy List Complexity: Unspecified First()=posValue(pos)=itemKth(integer)=item Next(pos)=posLength()=integerSetKth(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 StackQueueVector Linked List Sorted Vector
9
Concrete Data Types List Linked List Linked List using References What’s an alternative implementation? nodeB.value = “b”; nodeC.value = “c”; list = nodeB; nodeB.next = nodeC bc
10
Concrete Data Types List Linked List Linked List using References nodeB.value = “b”; nodeC.value = “c”; list = nodeB; nodeB.next = nodeC bc Linked List using Arrays “c”“b” 02 list = 4; 1 2 3 4 5
11
Linked Lists in C struct node{ Object element; struct node * next; } Everything else is a pointer to a node! typedef stuct node * List; typedef struct node * Position; abc 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 How do we implement Delete(position) ? Insert(element, position)? FOARNRT 3864105 Data Next 179 23456810 First = 2
18
Free Cell Management 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 FOARNRT 790 3864105 Data Next 179 23456810 First = 2Free = 1
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 Linked listArraySorted array Kth(int) Find(e) Insert(e,pos) Next(pos) InsertAnywhere(e)
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 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; } abc x a bc 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
Polynomial ADT Possible linked list implementation: A i is the coefficient of the x i-1 term: 5 + 2x + 3x 2 ( 5 2 3 ) 7 + 8x ( 7 8 ) 3 + x 2 ( 3 0 2 ) Problem? List ADT
25
4 + 3x 2001 ( 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 3 )
26
Sparse Vector Data Structure: 4 + 3x 2001 4040 3 2001 ( ) class Term { int coeff, power; } class PolyNode extends ListNode { Term data; } class Polynomial extends List { Polynode header; … }
27
Addition of Two Polynomials? Complexity? 10 50 3 1200 15+10x 50 +3x 1200 15 0 p 30 50 4 100 5+30x 50 +4x 100 5050 q
28
Addition of Two Polynomials One pass down each list: (n+m) 10 50 3 1200 15+10x 50 +3x 1200 15 0 p 30 50 4 100 5+30x 50 +4x 100 5050 q 4 100 3 1200 40 50 20 0 r
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 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); } see Weiss for tokenizer
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.