Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2006- Data Structures I Chapter 5 Linked Lists I.

Similar presentations


Presentation on theme: "CS2006- Data Structures I Chapter 5 Linked Lists I."— Presentation transcript:

1 CS2006- Data Structures I Chapter 5 Linked Lists I

2 Topics Linked List and Array Object & Reference
Reference-Based Linked List

3 Array Limitations Arrays Is there any other way to implement the list?
Simple Fast, easy to perform search but Difficult to insert and delete items Must specify size at construction time Is there any other way to implement the list?

4 Linked Lists Fortunately, we can use a structure called a linked list to overcome this limitation. The linked list is a very flexible dynamic data structure: items may be added to it or deleted from it at will. A linked list allows as many elements as a programmer needs requiring much less maintenance.

5 Object & References class Student { }
String name; int ID; double GPA; Address address; } class variables (instance variable) are initialized by compiler automatically. Name = null; ID=0; GPA=0.0; address=null; //object references are initialized with null

6 Object & References Create a reference to a type student
Student student; student is called a reference variable (or reference) of type Student it contains the address of an object or null; the object needs to be created using new ?

7 Object & References A reference can also be called a pointer (to an object in memory) and they are often depicted graphically: student = new Student (“John Smith”, 40725, 3.57) student John Smith 40725 3.57

8 Object & References student = null; This means that student reference does not “point” to any object student

9 References as Links Object references can be used to create links between objects Suppose a Student class contained a reference to another Student object (code?) John Smith 40725 3.57 Jane Jones 58821 3.72

10 References as Links References can be used to create a variety of linked structures, such as a linked list: studentList

11 Objects Two objects of this class can be instantiated and chained together having the next reference of one Node object refer to the other. The second object’s next reference can refer to a third Node object, and so on, creating a linked list of Node objects. Each node will contain some data as specified by the programmer. This constitutes a linked list

12 Linked List Definition
A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to its data value(s), a reference to an object of the same type.

13 Linked Lists List: An external reference usually referred to as the "head" of the list contains the address of the first node object. Diagram of a sample linked list containing int data: Node NULL pointer 45 51 84 Head Node Item Next

14 A Linked List Node Class
First attempt at a class for a linked list of integers: public class IntegerNode { public int item; public IntegerNode next; } Problem?

15 Final IntegerNode Class
public class IntegerNode { private int item; private IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { next = nextNode;

16 Final IntegerNode Class (2)
public void setItem(int newItem) { item = newItem; } // end setItem public int getItem() { return item; } // end getitem public void setNext(IntegerNode nextNode) { next = nextNode; } // end setNext public IntegerNode getNext() { return next; } // end getNext } // end class IntegerNode

17 A Polymorphic Linked List Node
public class Node { private Object item; private Node next; public Node(Object newItem) { item = newItem; next = null; } // end constructor public Node(Object newItem, Node nextNode) { next = nextNode;

18 A Polymorphic Linked List Node
public class Node { private Object item; private Node next; public Node(Object newItem) { item = newItem; next = null; } // end constructor public Node(Object newItem, Node nextNode) { next = nextNode;

19 A Polymorphic Linked List Node
public void setItem(Object newItem) { item = newItem; } // end setItem public Object getItem() { return item; } // end getitem public void setNext(Node nextNode) { next = nextNode; } // end setNext public Node getNext() { return next; } // end getNext } // end class Node To instantiate a Node containing an Integer: Node n = new Node(new Integer(6)); or containing a character: Node n = new Node(new Character('A'));

20 A Polymorphic Linked List Node
public void setItem(Object newItem) { item = newItem; } // end setItem public Object getItem() { return item; } // end getitem public void setNext(Node nextNode) { next = nextNode; } // end setNext public Node getNext() { return next; } // end getNext } // end class Node To instantiate a Node containing an Integer: Node n = new Node(new Integer(6)); or containing a character: Node n = new Node(new Character('A'));

21 Review When you declare a variable that refers to an object of a given class, you are creating a(n) ______ to the object. interface reference method ADT

22 Review Integer maxNum; maxNum = new Integer (15);
______ is a reference variable. Integer maxNum New 15

23 Review A reference variable declared as a data field within a class has the default value ______. -1 null empty

24 Review If you attempt to use a reference variable before it is instantiated, a(n) ______ will be thrown. IndexOutOfBoundsException InstantiationException IllegalAccessException NullPointerException

25 Review A linked list contains components, called ______, which are linked to one another. nodes arrays vectors references

26 Review According to the principle of information hiding, the data fields of a class must be declared as ______. public protected private abstract


Download ppt "CS2006- Data Structures I Chapter 5 Linked Lists I."

Similar presentations


Ads by Google