Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Types Linked Lists. Abstract Data Type(ADT) 4 n ADT--a specification, in abstract terms only, without reference to programming language.

Similar presentations


Presentation on theme: "Abstract Data Types Linked Lists. Abstract Data Type(ADT) 4 n ADT--a specification, in abstract terms only, without reference to programming language."— Presentation transcript:

1 Abstract Data Types Linked Lists

2 Abstract Data Type(ADT) 4 n ADT--a specification, in abstract terms only, without reference to programming language or machine dependent terminology. n ADT--contains two parts: n description of the data together with any internal structure that thedata has n description of the valid operations that can be performed on this data

3 ADT 2 n Because abstract--can not be used directly in a program, but can be implemented into concrete instances, which can be used in programs n To insure conditions that will lead to successful application, ADT should make use of the exception mechanism

4 Ideal properties for ADT List structure 4 n should occupy exactly as much memory as necessary n should be able to grow at any time, and accomplish growth quickly n should be able to shrink at any time and return unused memory to the JVM n should be ordered so that every element has a well-defined location.

5 ADT List--definition 2 n List--an ordered structure of elements such that it is either empty or has a unique first element, every element except for the last one has one immediate successor, and the last element is that one without a successor. n One element in a non-empty List is always designated as the current element.

6 ADT List--operations 4 n Create an empty List n Designate the first element of a non- empty List as the current element n Move the current element to the successor of the current element in a non-empty List, unless the current element is last n Insert an element into the List

7 ADT List--operations (cont.) 5 n Retrieve the element designated as current, unless the List is empty n Remove the element designated as current, unless the List is empty n Replace the element designated as current, by another element unless the List is empty n Check if the List is empty n Check if current element is last

8 List class--definition 3 n A List is either an empty or sequential structure of elements such that one element is always designated as current List supports the following methods: n constructor: creates an empty List containing no elements n public void first(): make first element current, can only be used on a nonempty List

9 List class--definition (cont.) 3 n Public void next(): makes the element after the current one the (new) current one n public void insert (Object o): inserts an element, or object, into the List n public Object retrieve():returns current element, can only be used on a nonempty List

10 List class--definition (cont.) 4 n public void remove (): deletes current element from the List, only on nonempty n public void replace (Object o): replaces current element by another one, can only be used on a nonempty List n public boolean isEmpty():true if List empty, false otherwise n public boolean isLast():true if element last, false otherwise

11 Node 3 n A utility class used to create lists and other list-like sequential structures n Need inside a List--a smart obect that can store data and knows who its immediate successor, if any, is n Hence, Node has two pieces: one piece to store a Java object, and another piece to store a reference to another Node

12 Node (cont.) 3 n A node can return and replace the Object stored n A node can contain a reference to another node called the successor n Implementation: a node can contain two non-private fields, one called data that stores the actual object and another called next that stores either a reference to another node or “null”.

13 Node 2 n GO TO node code. n Draw on board

14 Circular node n (put picture on board) n Node A = new Node(); n Node B = new Node(); n Node C = new Node(); n A.next=B; n B.next=C; n C.next=A;


Download ppt "Abstract Data Types Linked Lists. Abstract Data Type(ADT) 4 n ADT--a specification, in abstract terms only, without reference to programming language."

Similar presentations


Ads by Google