730 likes | 1k Vues
Exposure Java-AB 2007. Chapter 33 Slides. Linked Lists I. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. ArrayList Warning. Java's ArrayList class allows dynamic resizing during program execution.
E N D
Exposure Java-AB 2007 Chapter 33 Slides Linked Lists I PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram
ArrayList Warning Java's ArrayList class allows dynamic resizing during program execution. An ArrayList object may have some extra capacity for additional new elements. If the capacity is not sufficient to allow adding new elements, a new block of memory is allocated, data is shifted from the old block to the new block, and the old block is returned to available memory for future use. This process can take considerable execution penalty, if the array has many elements, each storing large quantities of data.
The LinkedList Class
// Java3301.java // This program reviews the <LinkedList> class with methods // <addFirst>, <addLast>, <getFirst> and <getLast>. import java.util.LinkedList; public class Java3301 { public static void main(String args[]) { System.out.println("JAVA3301.JAVA\n"); LinkedList family = new LinkedList(); family.addFirst("John"); family.addLast("Greg"); family.addLast("Maria"); family.addLast("Heidi"); System.out.println(family.getFirst()); System.out.println(family.getLast()); System.out.println(); System.out.println("family: " + family); System.out.println(); } }
// Java3302.java // This program creates a linked list with four nodes and displays each node starting with // the first node to the last node using the <getFirst> and <removeFirst> methods. import java.util.LinkedList; public class Java3302 { public static void main(String args[]) { System.out.println("JAVA3302.JAVA\n"); LinkedList family = new LinkedList(); family.addFirst("John"); family.addLast("Greg"); family.addLast("Maria"); family.addLast("Heidi"); for (int k = 1; k <= 4; k++) { System.out.println(family.getFirst()); family.removeFirst(); } System.out.println(); } }
// Java3303.java // This program creates a linked list with four nodes and displays each node starting with // the last node to the first node using the <getLast> and <removeLast> methods. import java.util.LinkedList; public class Java3303 { public static void main(String args[]) { System.out.println("JAVA3303.JAVA\n"); LinkedList family = new LinkedList(); family.addFirst("John"); family.addLast("Greg"); family.addLast("Maria"); family.addLast("Heidi"); for (int k = 1; k <= 4; k++) { System.out.println(family.getLast()); family.removeLast( ); } System.out.println(); } }
The ListIterator Class
// Java3304.java // This program demonstrates how to traverse a linked list using the <next> // method of the <ListIterator> class. import java.util.LinkedList; import java.util.ListIterator; public class Java3304 { public static void main(String args[]) { System.out.println("JAVA3304.JAVA\n"); LinkedList family = new LinkedList(); family.addFirst("John"); family.addLast("Greg"); family.addLast("Maria"); family.addLast("Heidi"); ListIterator iter = family.listIterator(); for (int k = 1; k <= 4; k++) System.out.println(iter.next()); System.out.println(); } }
Constructing aListIterator Object LinkedList family = new LinkedList(); ListIterator iter = family.listIterator(); The listIterator method of the LinkedList class instantiates an iter object of the ListIterator class.
Iterator Method next System.out.print(hAccess.next()+" "); Method next moves the iterator to the next element, and then returns it.
// Java3305.java // This program demonstrates how to traverse a linked list until the // end is found, using the <hasNext> method. import java.util.LinkedList; import java.util.ListIterator; public class Java3305 { public static void main(String args[]) { System.out.println("JAVA3305.JAVA\n"); LinkedList family = new LinkedList(); family.addFirst("John"); family.addLast("Greg"); family.addLast("Maria"); family.addLast("Heidi"); ListIterator iter = family.listIterator(); while (iter.hasNext()) System.out.println(iter.next()); System.out.println(); } }
ListIterator Method hasNext while (iter.hasNext()) Method hasNext returns true if an element remains in the Collection object and returns false otherwise.
ListIterator Method remove iter.remove(); Method remove removes the current item pointed to by the ListIterator.
// Java3306.java // This program demonstrates how to use the <ListIterator> class to access // elements in a <LinkedList> object. The <ListIterator> adds the <add> // and <set> method to the three <Iterator> methods. import java.util.*; public class Java3306 { public static void main(String args[]) { System.out.println("\nJAVA3306.JAVA\n"); LinkedList list = new LinkedList(); for (int k = 1000; k <= 5000; k+= 1000) list.add(new Integer(k)); System.out.println(list); System.out.println(); ListIterator iter = list.listIterator(); while (iter.hasNext()) { iter.next(); iter.set(new Integer(9999)); iter.add(new Integer(1111)); } System.out.println(list); System.out.println(); } }
ListIterator Method add iter.add(new Integer(1111)); Method add inserts a new element between the current item pointed to by the ListIterator and the next element.
ListIterator Method set iter.set(new Integer(9999)); Method set replaces the current item pointed to by the Listiterator with the provided argument.
// Java3307.java // This program reviews implementing a queue with the <ArrayList> class. import java.util.ArrayList; public class Java3307 { public static void main (String args[]) { System.out.println("JAVA3307.JAVA\n"); MyQueue students = new MyQueue(); students.enQueue("Luke Watts"); students.enQueue("Brian Sims"); students.enQueue("Mike Lewis"); students.enQueue("Jamie Singbush"); System.out.println("There are " + students.getSize() + " students stored on the queue"); System.out.println(); if (students.isEmpty()) System.out.println("The queue is empty"); else System.out.println("Queue front contains " + students.peekFront()); System.out.println(); while (!students.isEmpty()) { String student = (String) students.deQueue(); System.out.println(student); } System.out.println(); } }
class MyQueue { private ArrayList data; // stores queue data private final int front; // keeps index of the queue front public MyQueue() // Initializes an empty array object with references of private variable data objects. { data = new ArrayList(); front = 0; } public boolean isEmpty() { return data.size() == 0; } // Returns true if data is empty, false otherwise public void enQueue (Object x) { data.add(x); } // Adds variable x to the back of the queue public Object deQueue() { return data.remove(front); } // Returns and removes the front element from the queue public Object peekFront() { return data.get(front); } // Returns the front element from the queue without removal public int getSize() { return data.size(); } // returns the number of queue elements }
// Java3308.java // This program demonstrates implementing a queue with the <LinkedList> class. import java.util.LinkedList; public class Java3308 { public static void main (String args[]) { System.out.println("JAVA3308.JAVA\n"); MyQueue students = new MyQueue(); students.enQueue("Luke Watts"); students.enQueue("Brian Sims"); students.enQueue("Mike Lewis"); students.enQueue("Jamie Singbush"); System.out.println("There are " + students.getSize() + " students stored on the queue"); System.out.println(); if (students.isEmpty()) System.out.println("The queue is empty"); else System.out.println("Queue front contains " + students.peekFront()); System.out.println(); while (!students.isEmpty()) { String student = (String) students.deQueue(); System.out.println(student); } System.out.println(); } }
class MyQueue { private LinkedList data; // stores queue data public MyQueue() { data = new LinkedList(); } // Initializes an empty array object with references of private variable data objects. public boolean isEmpty() { return data.size() == 0; } // Returns true if data is empty, false otherwise public void enQueue (Object x) { data.addLast(x); } // Adds variable x to the back of the queue public Object deQueue() { return data.removeFirst(); } // Returns and removes the front element from the queue public Object peekFront() { return data.getFirst(); } // Returns the front element from the queue without removal public int getSize() { return data.size(); } // returns the number of queue elements }
Old Fashion Pre-OOP LinkedLists
What is this class ListNode { public Object value; public ListNode next; } This is a "Self-Referencing Declaration". It is used in the creation of Linked Lists.
Self-Referencing Declaration A linked list data structure is made up of self-referential objects. If the object is of ListNode type then one - or more - fields references ListNode, like the example below. class ListNode { public Object value; public ListNode next; }
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all.
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p ?
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p ? temp
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. temp p
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. temp p John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. temp p John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp Greg John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp Greg John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp Greg John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp Greg John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp David Greg John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp David Greg John
// Java3309.java // This program demonstrates creating a linked list in an old-fashioned pre-oop // style that uses a poorly designed <ListNode> class. // Note that the <ListNode> class contains only data and no methods at all. p temp David Greg John
A B Next Reference Rule The next data field is one-directional. This linking reference value is only capable of linking to one other node. In other words, if Node A links to node B, the two nodes are linked but it is not possible to traverse from node B back to node A with the link A B.
Modern OOP LinkedLists
// Java3310.java // This program demonstrates how to create a linked list of four nodes with // a well-designed <ListNode> class. This linked list behaves like a stack. public class Java3310 { public static void main (String args[]) { System.out.println("\nJAVA3310.JAVA\n"); ListNode p = null; ListNode temp = null; System.out.println("Constructing a node with Isolde"); p = new ListNode("Isolde",temp); temp = p; System.out.println("Constructing a node with Maria"); p = new ListNode("Maria",temp); temp = p; System.out.println("Constructing a node with Heidi"); p = new ListNode("Heidi",temp); temp = p; System.out.println("Constructing a node with Diana\n"); p = new ListNode("Diana",temp); temp = p; while (p != null) { System.out.println(p.getValue()); p = p.getNext(); } System.out.println(); } } Diana Heidi Maria Isolde ListNode class on next slide
class ListNode { public ListNode (Object initValue, ListNode initNext) { value = initValue; next = initNext; } public Object getValue () { return value; } public ListNode getNext () { return next; } public void setValue (Object theNewValue) { value = theNewValue; } public void setNext (ListNode theNewNext) { next = theNewNext; } private Object value; private ListNode next; } The same ListNode class will be used for the next few programs.
// Java3311.java // This program demonstrates how to create a linked list of four nodes with // a well-designed <ListNode> class. This linked list behaves like a queue. public class Java3311 { public static void main (String args[]) { System.out.println("\nJAVA3311.JAVA\n"); ListNode p = null; ListNode front = null; ListNode temp = null; System.out.println("Constructing a node with Isolde"); p = new ListNode("Isolde",null); front = p; temp = p; System.out.println("Constructing a node with Maria"); p = new ListNode("Maria",null); temp.setNext(p); temp = p; System.out.println("Constructing a node with Heidi"); p = new ListNode("Heidi",null); temp.setNext(p); temp = p; System.out.println("Constructing a node with Diana\n"); p = new ListNode("Diana",null); temp.setNext(p); temp = p; p = front; while (p != null) { System.out.println(p.getValue()); p = p.getNext(); } System.out.println(); } Isolde Maria Heidi Diana
// Java3312.java // This program demonstrates how to create a linked list of integers. // The linked list is created like a stack. public class Java3312 { public static void main (String args[]) { System.out.println("\nJAVA3312.JAVA\n"); ListNode p = null; ListNode temp = null; for (int k = 100; k < 110; k++) { System.out.println("Constructing a node with " + k); p = new ListNode(new Integer(k),temp); temp = p; } System.out.println(); while (p != null) { System.out.print(p.getValue() + " "); p = p.getNext(); } System.out.println("\n\n"); } } 109 108 107 106 105 104 103 102 101 100
// Java3313.java // This program demonstrates how to create a linked list of integers. // The linked list is created like a queue. public class Java3313 { public static void main (String args[]) { System.out.println("\nJAVA3313.JAVA\n"); ListNode p = null; ListNode temp = null; ListNode front = null; System.out.println("Constructing a node with 100"); front = new ListNode(new Integer(100),null); temp = front; for (int k = 101; k < 110; k++) { System.out.println("Constructing a node with " + k); p = new ListNode(new Integer(k),null); temp.setNext(p); temp = p; } System.out.println(); p = front; while (p != null) { System.out.print(p.getValue() + " "); p = p.getNext(); } System.out.println("\n\n"); } } 100 101 102 103 104 105 106 107 108 109
Deleting From a LinkedLists
Deleting from a Linked ListStep 1 front Meg is the element to be deleted.