260 likes | 279 Views
CS2006- Data Structures I. Chapter 5 Linked Lists I. Topics. Linked List and Array Object & Reference Reference-Based Linked List. Array Limitations. Arrays Simple Fast, easy to perform search but Difficult to insert and delete items Must specify size at construction time
E N D
CS2006- Data Structures I Chapter 5Linked Lists I
Topics • Linked List and Array • Object & Reference • Reference-Based Linked List
Array Limitations • Arrays • 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?
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.
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
Object & References student • Create a reference to a type 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 ?
student John Smith 40725 3.57 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)
Object & References student = null; • This means that student reference does not “point” to any object student
John Smith 40725 3.57 Jane Jones 58821 3.72 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?)
studentList References as Links • References can be used to create a variety of linked structures, such as a linked list:
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
Linked List Definition • Linked List: • 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.
Head 45 51 84 NULL pointer Node Item Next 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
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?
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) { item = newItem; next = nextNode; } // end constructor
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
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) { item = newItem; next = nextNode; } // end constructor
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) { item = newItem; next = nextNode; } // end constructor
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')); 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')); 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
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
Review • Integer maxNum; maxNum = new Integer (15); ______ is a reference variable. • Integer • maxNum • New • 15
Review • A reference variable declared as a data field within a class has the default value ______. • 0 • -1 • null • empty
Review • If you attempt to use a reference variable before it is instantiated, a(n) ______ will be thrown. • IndexOutOfBoundsException • InstantiationException • IllegalAccessException • NullPointerException
Review • A linked list contains components, called ______, which are linked to one another. • nodes • arrays • vectors • references
Review • According to the principle of information hiding, the data fields of a class must be declared as ______. • public • protected • private • abstract