Lec 12 Chapter 12 Dynamic Data Structures Jiang
















- Slides: 16
Lec. 12 (Chapter 12) Dynamic Data Structures Jiang (Jen) ZHENG June 13 th, 2005 CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12
Outline n Preview of Data Structure n How to us those data structures n How to implement those data structures n What tools to know and use in order to implement those data structures n What Java provide to us and we can use the features n Dynamic Data Structure n Basic Data Structure in Java n List: Array. List, Linked. List n Stack n More on CS 0445 … CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 2
Preview of Data Structure n The way to store and retrieve information. n Basic Data Structures: n Array n Linked List n Stack n Queue n Tree n… CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 3
Preview of Data Structures n In Data Structures, we want to learn, understand be able to utilize many of the data structures that are fundamental to computer science Data structures such as vectors, stacks, queues, linked-lists and trees are used throughout computer science n We should understand these from a user's point of view: n n What are these data structures and how do I use them in my programs? CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 4
Preview of Data Structures n We also want to understand implementation issues related to these data structures, and to see how they can be implemented in the Java programming language Data structures can be implemented in various ways, each of which has implications (ex: runtime differences, code complexity, modifiability) n We should understand these data structures from an implementer's point of view: n n How can these data structures be effectively implemented? CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 5
Preview of Data Structures n We also want to understand utilize programming ideas and techniques utilized in data structure implementation n Object-oriented programming, dynamic memory utilization, recursion and other principles must be understood in order to effectively implement data structures n What tools must I know and be able to use in order to implement data structures? CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 6
Preview of Data Structures n We also want to learn more of the Java programming language and its features, and to become more proficient at programming with it Java is a very large language with extensive capabilities n As your programming skills improve, you can utilize more of these capabilities effectively n n Since I am working with Java, how well can I learn and use the language and its features? CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 7
Dynamic Data Structure n The data structure can expand contract during computation Linked list n Queue n Stack n Vector n… n CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 8
List In Java n Example: Consider the idea of a List: n Ordered (by position), indexable collection of data n In Java this is an interface – with some methods as shown: n void add(int index, Object element) § Add a new object at the specified index n int index. Of(Object o) § Find the object and return its index n Object get(int index) § Return the object at the specified index n Object remove(int index) § Remove (and return) the object at the specified index CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 9
List in Java n Classes Implement the List interface n Array. List: Resizable-array implementation of the List interface n Linked. List: Use a linked list of nodes as the underlying Head data structure to implementation of the List interface n n Vector: The Vector class implements a growable array of objects See Java API. CS 401/COE 401 Summer 2005. Department of 10 Computer Science. University of Pittsburgh. Lecture 12
List in Java n Each implementation has advantages and disadvantages n Ex: Advantage of Array. List § get(i) can be done in one step, since we just go to that index in the array § In a Linked. List we must follow references down the list n Ex: Advantage of Linked. List § add(i, obj) requires only creating a new object and linking it correctly § In an Array. List we must shift all of the items from i down a spot in order to make "room" at index i CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 11
List in Java n Ex: See ex 27. java n n Note that the operations work for both Array. List and Linked. List, using the List interface However, the implementations and efficiencies of the operations differ § More in CS 0445! CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 12
Stack in Java n Stack class is a subclass of Vector n Methods implemented n Boolean empty() n Object peek() n Object pop() n Object push(Object item) n int search(Object o) n Preview of Stack if needed n See API CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 13
More on CS 0445 n In CS 0445 you will see these and other implementation ideas in detail CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 14
Garbage Collection n In C, C++: memory is freed explicitly by the programmer when certain data structures are no longer needed. n In Java: the computer automatically figures out when an object is no longer needed and removes it – Garbage Collection. Reference Counting: when the count goes to zero, the object can be collected. n Mark and sweep garbage collection: any object not marked is garbage and can be recycled. n CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 15
Creating and Using Package n Creating Package n Add a package statement to the top of each class in the package. Ex. package tio; n Compile the java files. Ex. javac tio/java. * n Using Package n import tio. *; to import all the classes from a package. n import tio. Concole; to import the specific class. CS 401/COE 401 Summer 2005. Department of Computer Science. University of Pittsburgh. Lecture 12 16
















