Linked Data Representations B Ramamurthy 10282021 B Ramamurthy
Linked Data Representations B. Ramamurthy 10/28/2021 B. Ramamurthy 1
Introduction z. Linked data representation and algorithms that manipulate them are an important component of study in CS. z. Linked representations are used when it is difficult to predict the size and shape of the data structures needed. 10/28/2021 B. Ramamurthy 2
Topics for Discussion z Vector Class z Java pointers(? !) z Node of a linked list z Linear linked list yclass definition yimplementation of methods ytesting yusage z JDK 1. 2 Linked List 10/28/2021 B. Ramamurthy 3
Steps in Designing a Class (/ADT) z. Interface /Class definition z. Implementation z. Testing z. Application z. Examining Extensilbility 10/28/2021 B. Ramamurthy 4
The Interface for Vector z. Constructors : default and initializing z. Service methods: add. Element, remove. Element z. Set/get methods: get. Element. At, set. Element. At z. Utility : size z. Predicate methods: is. Empty 10/28/2021 B. Ramamurthy 5
Organize Your Analysis: Class Diagrams class Rectangle Data Declarations Methods 10/28/2021 length width color area perimeter display B. Ramamurthy 6
Class Diagram for Vector 10/28/2021 //methods: public Vector() public Vector(int initial. Capacity) //service methods public boolean add. Element(Object obj) public boolean insert. Elem. At(Object obj, int index) public boolean remove. Element(Object obj) public boolean remove. Elem. At(int index) //set and get methods public Object get. Elem(int index) public void set. Elem(Object obj, int index) //utility methods public int size() public boolean is. Empty() // predicate method // Data: private Object element. Data[]; private int elem. Count; 7
Sample Program z. On the overhead projector 10/28/2021 B. Ramamurthy 8
Pointer in Java (? !) z. Any object reference in Java is an implicit pointer. z. An object reference is nothing but address of the memory location(s) assigned to the array or an object. z. Since any data falls under either an object or a primitive value, there is no need to explicitly distinguish between pointer and non-pointer data B. Ramamurthy as in C++ 10/28/2021 9
A linked list z Typically pointers are used to link items. z Each item in a linked list contains both the data value and a pointer to the next item. z Since each item must contain at least two pieces of information class is a natural choice for implementing an item of the linked list. z This item of linked list is commonly referred to as node of the linked list. 10/28/2021 B. Ramamurthy 10
A node of linked list public class List. Node { Item. Type item; List. Node link; } 10/28/2021 B. Ramamurthy 11
Linked List class Linked. List // data int length; List. Node first. Node; // methods public int size() public bool insert. Node(Item. Type it) public bool insert. Last(Item. Type it) public bool delete. Last() public List. Node search(Item. Type it) public bool equals(Linked. List ll) public String to. String() 10/28/2021 B. Ramamurthy 12
JDK 1. 2 Linked List Object Abstract. Collection Abstract. List Abstract. Sequential. List Linked. List 10/28/2021 B. Ramamurthy List, java. io. Serializable, java. lang. Cloneable 13
Linked. List’s inheritance z. Most of the direct linked list functionality is defined in the interface List. z. Serializable defines the disk archiving functionality. z. Cloenable defined the cloning ability. z. Linked. List class defines the variables needed for storing the header and size. z. A List. Iterator class offers the Iterator 10/28/2021 B. Ramamurthy 14 facility.
Linked. List methods z. During Lecture we discussed method sin List interface and how Linked. List implements them. Look at /util/lang/jdk 1. 2/docs/index. html for more details. 10/28/2021 B. Ramamurthy 15
List. Iterator Usage: Linked. List my. List = new Linked. List(); // fill it up with data List. Iterator k = my. List. list. Iterator(); while(k. has. Next()) { // process the elements my. Process(k. next()); System. out. println(k. next()); } 10/28/2021 B. Ramamurthy 16
HWK 2 Write an application that keeps the “airport name” and “airport code” as given in Fig. 3. 28 into a linked list. How? You need to define a class to represent airport data. Call this class Airport. Create a linked list for objects of Airport type. Write a user defined search method in the application class that searches for code given the airport name. See www. cse. buffalo. edu/~bina/cse 116 for more details on hwk 2 10/28/2021 B. Ramamurthy 17
Summary z. We discussed the traditional view of the linked list ADT. z. We also looked at how JDK 1. 2 defines linked list. z. We had a first look at what iterators are and how they are used. 10/28/2021 B. Ramamurthy 18
- Slides: 18