linkedListsFall10

linkedListsFall10 - Data Structures Static vs. Dynamic...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Data Structures Static vs. Dynamic Structures -A static data structure has a fixed size – example: arrays (Note, this meaning is different from those associated with static modifier) -A dynamic data structure grows and shrinks as required by the information it contains example: linked lists An object reference is a variable that stores the address of an object. A reference is also called a pointer . Object references can be used to create links between objects Linked Lists Consider an object that contains a reference to another object of the same type: class Node { String name; Node pointer; } student John Smith 40725 3.58 This can construct a linked list A variable “students” stores the address of the object containing information of “John Smith”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Deleting an entry from a linked list: Inserting an entry into a linked list:
Background image of page 2
Using Iterator in the ArrayList We can use the iterator of an ArrayList object to traverse it (without using any index). ArrayList arraylist1 = new ArrayList(); ….. Iterator iterator1 = arraylist1.iterator(); while (iterator1.hasNext()) { System.out.println(iterator.next()); } Here, we use iterator.next() to access each element in the array list. Since a linked list does not have any index to trace each element, we need to use an iterator such as above. Note that the above loop is equivalent to: for (int i=0; i<arraylist1.size(); i++) { System.out.println(arraylist1.get(i)); } We can also use “for each loop” as follows: for (Object obj : arraylist1) { System.out.println(obj); }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Some method implementation of Linked List Note: You need to be careful in boundary cases such as operations at the beginning and the end of a list. //This Linked List class shows how some of its commonly //used methods are implemented. // A linked list is a sequence of nodes with efficient // element insertion and removal. // This class contains a subset of the methods of the // standard java.util.LinkedList class. import java.util.NoSuchElementException; public class LinkedList { //nested class to represent a node private class Node { public Object data; public Node next; } //only instance variable that points to the first node. private Node first; // Constructs an empty linked list. public LinkedList() { first = null; } // Returns the first element in the linked list. public Object getFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else return first.data; } // Removes the first element in the linked list. public Object removeFirst()
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

linkedListsFall10 - Data Structures Static vs. Dynamic...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online