1 / 26

CS2006- Data Structures I

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

lhinojosa
Download Presentation

CS2006- Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2006- Data Structures I Chapter 5Linked Lists I

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

  3. 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?

  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 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 ?

  7. 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)

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

  9. 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?)

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

  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 • 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.

  13. 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

  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) { item = newItem; next = nextNode; } // end constructor

  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) { item = newItem; next = nextNode; } // end constructor

  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) { item = newItem; next = nextNode; } // end constructor

  19. 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

  20. 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

  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 ______. • 0 • -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

More Related