Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies

Similar presentations


Presentation on theme: "Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies"— Presentation transcript:

1 Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies razdan@asu.edu http://i3dea.asu.edu/razdan

2 Razdan et al. - CST 2302 Data Structures and ADTs A Data Structure refers to the organization of data (and its storage allocations in a computer) An Abstract Data Type (ADT) is a type whose internal form is hidden behind a set of access functions. we can implement an ADT (using a class in java) that represents a data structure.

3 Razdan et al. - CST 2303 Data Structures Typical Data Structure operations: –insertion –access to data –deletion of data Common data structures: –linked list, stack, queue, priority queue, tree, binary search tree, hash table

4 Razdan et al. - CST 2304 Linked List ADT A Linked List is an ordered “chain” of data elements. Example: 12 14 -4 9 Head – the first element in the list Tail – the last element in the list

5 Razdan et al. - CST 2305 Linked List Operations Potential Linked List operations: –insert element into the list (arbitrary position) –insert element in list (specific position) –delete element from the list –find an element in the list –list concatenation –report list length –list copy –sublist copy

6 Razdan et al. - CST 2306 Nodes A list is composed of several Nodes. Nodes contain two instance variables: –data –reference (“link”) to next Node (or null ref) Example: 9 19null

7 Razdan et al. - CST 2307 Node class

8 Razdan et al. - CST 2308 Book implementation discussion –Constructors –addNodeAfter –getData –getLink –listCopy –listCopyWithTail –listLength –listPart –listPosition –listSearch –removeNodeAfter –setData –setLink The book implements a Node class with the following methods: We are NOT going implement things this way! Why not?

9 Razdan et al. - CST 2309 List Class

10 Razdan et al. - CST 23010 We will implement the following List methods Constructor (no parameters) add remove getLength display Constructor (List parameter) clone equals concat Note: we will assume a List of integers for now…

11 Razdan et al. - CST 23011 equals method All objects get a default “equals” method from the “Object” class The default checks to see if the instance variables are the same. What are the instance variables for a List? What do we want “equals” to mean for a List?

12 Razdan et al. - CST 23012 Generic equals method The equals method should always follow something similar to the following format (see page 80 of your textbook): public boolean equals( Object obj ){ if( obj is actually of the desired type ){ cast obj to desired type check if obj is equal to this object } else return false; }

13 Razdan et al. - CST 23013 List equals For the List class: public boolean equals( Object obj ){ boolean same = false; if( obj instanceof List){ List candidate = (List) obj; // add code to check for same } return same; }

14 Razdan et al. - CST 23014 List clone method public Object clone(){ List answer; try{ answer = (List) super.clone(); } catch( CloneNotSupportedException e){ throw new RuntimeException ("This class does not implement cloneable"); } // do we need to add anything or will super.clone // do the job??? return answer; }

15 Razdan et al. - CST 23015 Section 4.6 Many ADTs could be implemented using an Array or a linked list. –List, Sequence –Bag, Set –Others… How do we choose the best implementation? Section 4.6 gives some guidelines

16 Razdan et al. - CST 23016 Arrays vs. Linked Lists Arrays –Better at random access –Space efficient if we know the exact size of the ADT because we don’t have to store a “next” link (the next is assumed to be the next array element) Linked List –Better at additions/removals at a “cursor” –Better at resizing (dynamic sizing)


Download ppt "Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies"

Similar presentations


Ads by Google