1 / 33

You’re involved, motivated and want to change the world… but can you lead?

You’re involved, motivated and want to change the world… but can you lead? On *March 11th * Queen’s will be hosting/ /*/Impact/*/ /the *ASUS Leadership Conference*. There will be a variety of interactive sessions as well as a keynote address from the *Director of Oxfam

Download Presentation

You’re involved, motivated and want to change the world… but can you lead?

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. CISC121 - Prof. McLeod

  2. You’re involved, motivated and want to change the world… but can you lead? On *March 11th * Queen’s will be hosting/ /*/Impact/*/ /the *ASUS Leadership Conference*. There will be a variety of interactive sessions as well as a keynote address from the *Director of Oxfam Canada Robert Fox*. Tickets will be sold in *Ban Righ and Leonard Cafeterias from* *5:30-7:30pm on February 15th /16th and March 1st /2nd *and in *MacCorry from 11am-2pm from March 6th to 8th *. They are also on sale at *Destination* in the JDUC for only *$10. * If you have any questions email us at leadership@asus.queensu.ca Leaders begin from the basics. CISC121 - Prof. McLeod

  3. More Stuff • Midterm Preparation Page CISC121 - Prof. McLeod

  4. Last Time • Singly linked list, so far: • A separate class for the node definition. • A linked list class to keep track of the nodes, with methods to add nodes to the head and tail. CISC121 - Prof. McLeod

  5. Today • Use of an inner class for the node. • Deleting the head node. • Deleting all nodes. • Searching for, and deleting an inner node. • Deleting the tail node in a singly linked list. • Doubly linked lists (if we have time!) CISC121 - Prof. McLeod

  6. Singly Linked List - Node Class public class IntNode { public int info; // a data value public IntNode next; // a link // constructors public IntNode (int i) { this(i, null); } public IntNode (int i, IntNode n) { info = i; next = n; } } // end IntNode CISC121 - Prof. McLeod

  7. public class IntSLList { // A “singly linked // list with a head and tail” private IntNode head; private IntNode tail; public IntSLList () { head = null; tail = null; } public void addToHead (int aNum) { head = new IntNode(aNum, head); if (tail == null) tail = head; } // more methods to be developed! } // end IntSLList CISC121 - Prof. McLeod

  8. Singly Linked List – Use of Inner Classes • The node object, IntNode, is a public class and its attributes (infoand next) are public. • This is necessary so that the IntSLListclass can use the node class. • This is not good “containment” or “information hiding” practice. • But if the class IntNode and its attributes are declared private, how can they be used by the linked list class? • Re-define IntSLListas: CISC121 - Prof. McLeod

  9. Use of Inner Classes – Cont. public class IntSLList { private IntNode head; private IntNode tail; private class IntNode { private int info; private IntNode next; public IntNode (int i) { this(i, null); } public IntNode (int i, IntNode n) { info = i; next = n; } } // end IntNode // rest of IntSLList methods, constructors } // end IntSLList CISC121 - Prof. McLeod

  10. Use of Inner Classes – Cont. • Note that even though the inner class is privateit can be used by IntSLList because it has been defined inside of this class. • Or, the scope of IntNode is anywhere inside the IntSLList class. • IntNode and its attributes are *not* available outside IntSLList. Much Better! CISC121 - Prof. McLeod

  11. Writing Linked List Methods • Note that each method must work for: • An empty list, • A list with just one node, and • A list with two or more nodes. • We have methods to: • Create a list (the constructor) • Add to the head of the list • Check for an empty list • Add to the tail of the list • What other methods would be useful? CISC121 - Prof. McLeod

  12. Singly Linked List - Other Methods • Deleting a head node. • Deleting all nodes! • Deleting an inner node (not head or tail). • Deleting a tail node. • Others: • Searching for a certain node. • Counting nodes. • Adding a inner node (but why?) CISC121 - Prof. McLeod

  13. Singly Linked List - Deleting Nodes • Note that a deleting method may or may not return the data value or a link to the data Object that it has deleted. We will assume that the deleting method returns the value. • The calling method can choose not to do anything with this value. • Note that these deleting methods will return a value of -1 if the list is empty. What else could we do here? CISC121 - Prof. McLeod

  14. Deleting the Head Node public int removeFromHead () { if (isEmpty()) return -1; int i = head.info; if (head.next == null) { head = null; tail = null; } else head = head.next; return i; } // end removeFromHead CISC121 - Prof. McLeod

  15. Deleting the Head Node, Cont. tail head 15 10 5 null • After head = head.next; tail head Poof! 15 10 5 null • What happens to the node with 15? CISC121 - Prof. McLeod

  16. Deleting All Nodes public void clearList() { if (!isEmpty()) { head = null; tail = null; } // end if } // end clearList • Nodes that are no longer “pointed to” are garbage collected! CISC121 - Prof. McLeod

  17. Deleting the Node that Contains the Value “delNum” • First, locate the node, then delete it. • It is way too big a method to show on this slide!! • See the next one: CISC121 - Prof. McLeod

  18. public void delete (int delNum) { // does not return i if (!isEmpty()) if (head==tail && delNum==head.info) { head = null; tail = null;} // only 1 node else if (delNum == head.info) // delete first node, more nodes in list head = head.next; else { IntNode pred = head; IntNode temp = head.next; while (temp != null && temp.info != delNum) { pred = pred.next; temp = temp.next; } // end while if (temp != null) { pred.next = temp.next; if (tail == temp) tail = pred; } // end if } // end else } // end delete method CISC121 - Prof. McLeod

  19. Deleting a Certain Node, Cont. • How would “we” modify this method to return a value? • Would you return -1? Under what conditions? CISC121 - Prof. McLeod

  20. Deleting an Inner Node - An Example list.delete(10); pred temp head tail 20 15 10 5 -5 null pred temp head tail 20 15 10 5 -5 null pred temp head tail 20 15 10 5 -5 null pred.next = temp.next; CISC121 - Prof. McLeod

  21. Deleting an Inner Node – Cont. • What happens to the node that is pointed to by temp, when the delete method completes? Poof! Gone! (The node is “garbage collected” when execution moves beyond the scope of the variable temp.) CISC121 - Prof. McLeod

  22. Deleting an Inner Node – Iterators • Note the use of the predand tempobjects in the deletemethod: • (“Java jargon”) These are called “iterators” because they are used to move through the list. CISC121 - Prof. McLeod

  23. Deleting a Tail Node • So how is this going to work? • How can the tail pointer be moved up to the preceding node? tail head 20 15 10 5 null CISC121 - Prof. McLeod

  24. Deleting a Tail Node - Cont. • Since there is no link from the tail node to the previous node, the only way is to iterate through the entire list, starting from the head, until the tail is reached. • (How can you tell when you have reached the tail?) • Two iterators must be used (Why?), as in the delete method: CISC121 - Prof. McLeod

  25. public int removeTail () { int i = -1; if (!isEmpty()) { i = tail.info; if (head == tail) { head = null; tail = null;} else { IntNode pred = head; IntNode temp = head.next; while (temp.next != null) { pred = pred.next; temp = temp.next; } // end while tail = pred; tail.next = null; } // end else } // end if return i; } // end removeTail method CISC121 - Prof. McLeod

  26. Deleting a Tail Node - Cont. • That was a lot of work! • Deleting the tail node this way is more time consuming than deleting an inner node. • Would it not be nice if the tail node already had a link pointing to the previous node? • No problem! Create a doubly linked list. CISC121 - Prof. McLeod

  27. Doubly Linked Lists public class IntDLList { private IntDLNode head; private IntDLNode tail; private class IntDLNode { private int info; private IntDLNode next; private IntDLNode prev; // new link! public IntDLNode (int aNum) { this(aNum, null, null); } public IntDLNode (int aNum, IntDLNode n, IntDLNode p) { info = aNum; next = n; prev = p; } } // end IntDLNode // IntDLList constructors and methods } // end IntDLList CISC121 - Prof. McLeod

  28. Doubly Linked List – Cont. • Structure: tail head 20 10 5 next null null prev CISC121 - Prof. McLeod

  29. Doubly Linked List – Cont. • To add a node to the tail of the list: // better add a constructor too! public IntDLList () { head = null; tail = null; } public boolean isEmpty () { return head == null; } public void addToTail (int aNum) { if (!isEmpty()) { tail = new IntDLNode(aNum, null, tail); tail.prev.next = tail; } else { head = new IntDLNode(aNum); tail = head; } } // end addToTail CISC121 - Prof. McLeod

  30. Doubly Linked List – Cont. dLList.addToTail(-10); After “new…”: head tail 20 10 5 -10 null null null head After tail=…; tail 20 10 5 -10 null null null head tail After tail.prev.next = tail; 20 10 5 -10 null null CISC121 - Prof. McLeod

  31. Doubly Linked List – Cont. • To remove the tail node: public int removeFromTail () { int i = -1; if (!isEmpty()) { i = tail.info; if (head == tail) { // one node in list head = null; tail = null; } else { tail = tail.prev; tail.next = null; } } // end if return i; } // end removeFromTail CISC121 - Prof. McLeod

  32. Doubly Linked List – Cont. int temp = dLList.removeFromTail(); head Before tail 20 10 5 -10 null null head tail After tail = tail.prev; 20 10 5 -10 null null head tail After tail.next = null; 20 10 5 -10 null null null CISC121 - Prof. McLeod temp is -10

  33. Doubly Linked List – Cont. • Now the removeFromTail method is much easier. • So, adding or deleting head or tail nodes is “easy” - which operations will require iteration? Any operation that involves a node other than the head or tail! CISC121 - Prof. McLeod

More Related