10.1 - CS 367 Announcements Thursday October 1 2015 Homework h3 due by 10 pm tomorrow October 2nd make sure your file is a pdf but not pdf scan of

10.1 - CS 367 Announcements Thursday October 1 2015...

This preview shows page 1 out of 10 pages.

You've reached the end of your free preview.

Want to read all 10 pages?

Unformatted text preview: CS 367 Announcements Thursday, October 1, 2015 Homework h3 due by 10 pm tomorrow, October 2nd • make sure your file is a pdf but not pdf scan of written work or pdf of a screen shot • make sure you use the name h3.pdf • submit to your in handin directory • remember homeworks are to be done individually • remember that late work is not accepted Homework h4 assigned Sunday Program p2 due 10 pm Friday, October 23rd Last Time Chains of Linked Nodes • practice Today Java Visibility Modifiers (repeats) LinkedList Class (repeats) LinkedListIterator Class Linked List Variations • tail reference • header node • double linking • circular linking Next Time Read: finish Linked Lists, start Complexity Linked List Variations (finish) Complexity • Intro • big-O notation • analyzing algorithms practice • analyzing Java code • analyzing Java code practice Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 1 Recall Chain of Linked Nodes Data Structure Listnode class class Listnode<E> { private E data; private Listnode<E> next; public public public public public public Listnode(E d) { Listnode(E d, Listnode<E> n){ E getData() { Listnode<E> getNext() { void setData(E d) { void setNext(Listnode<E> n) { . . . } . . . } return data; } return next; } data = d; } next = n; } } Show how the memory diagrams change as a result of executing the code beneath each: head head.setNext(head.getNext().getNext().getNext().getNext()); head head.getNext().getNext().setNext(head); // creates a circular linked list head curr n1 n1.setNext(curr.getNext()); curr.setNext(n1); Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 2 Java Visibility Modifiers public public class ArrayList<E> - public is accessible to any code private private -Object items - accessible only to code within the same module (e.g. class) protected protected String name - Accesible only to code within the same class or any of its subclasses package class Listnode<E> - if there is no visibility modifier ie means it ha sa package visibility an dis accesible to code only within the same package (AKA folder/directory) GOAL: hide data structure from others STEPS: 1. create a folder ie linkedlist 2. put in the folder: listnode.java - in package , linkedList.java, linkedListIterator.java - should be public 3. User must import linkedlist.* Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 3 LinkedList - Implementing ListADT using a Chain of Nodes public class LinkedList<E> implements ListADT<E> { private Listnode<E> head; private int numItems; public LinkedList() { head = null; numItems = 0; } public void add(E item) { // add a listnode at the end of the linked list if(item == null) throw new IllegalArgumentException(); Listnode<E> newNode = new Listnode<E>(item); //special case if(head == null){ head = newNode; } //General CAse: //traverse to last node in the chain Listnode<e> curr = head; while(curr.getNext() != null){ curr = curr.getNext(); } //link in the new node curr.setNext(newNode); numItems ++; Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 4 LinkedList (cont.) public class LinkedList<E> implements ListADT<E> { private Listnode<E> head; private int numItems; public LinkedList() { ... } public void add(E item) { ... } public E get(int pos) { // check for invalid position if(pos < 0 || pos > numItems){ throw new IndexOutOfBoundsException(); } // traverse to pos Listnode<E> curr = head; for(int i = 0; i < pos; i++){ curr = curr.getNext(); } return curr.getData(); Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 5 Implementing LinkedListIterator Should an indirect or a direct iterator implementation be used with a LinkedList? head numItems import java.util.*; public class LinkedListIterator<E> implements Iterator<E> { private Listnode<E> curr; LinkedListIterator(Listnode<E> head curr = head; ) { } public boolean hasNext() { return curr != null; } public E next() { if ( curr == null ) throw new NoSuchElementException(); E item = curr.getData(); curr = curr.getNext(); return item; } public void remove() { throw new UnsupportedOperationException(); } } Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 6 Making LinkedList Iterable public class LinkedList<E> implements ListADT<E> , Iterable<E>{ private Listnode<E> head; private int numItems; public LinkedList() { ... } public void add(E item) { ... } public E get(int pos) { ... } . . . public Iterator<E> iterator() { return new LinkedListIterator<E>(head); } } Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 7 Tail Reference Concept empty non-empty ... head Code Example public class LinkedList<E> implements ListADT<E> { private Listnode<E> tail; private Listnode<E> head; private int numItems; public LinkedList() { head = null; tail = null; numItems = 0; } public void add(E item) { if (item == null) throw new IllegalArgumentExceptions(); Listnode<E> newnode = new Listnode<E>(item); //Special Case: empty list if (head == null) { head = newnode; tail = newNode; } //General Case: non-empty list else { Listnode<E> curr = head; while (curr.getNext() != null) curr = curr.getNext(); curr.setNext(newnode); } <-- get rid of this tail.setNext(newNode); tail = newNode; numItems++; } Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 8 Header Node Concept // used to eliminate special case code empty non-empty ... head // header node is first node ad data is blank Code Example public class LinkedList<E> implements ListADT<E> { private Listnode<E> head; private int numItems; public LinkedList() { head = null; ------------------------- head = new Listnode<E>(null) numItems = 0; } public void add(E item) { if (item == null) throw new IllegalArgumentExceptions(); Listnode<E> newnode = new Listnode<E>(item); //Special Case: empty list if (head == null) { head = newnode; } //General Case: non-empty list else { Listnode<E> curr = head; while (curr.getNext() != null) curr = curr.getNext(); curr.setNext(newnode); } numItems++; } Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 9 Double and Circular Linking Doubly-Linked Chains of Nodes *commonly combined with tail reference and header node Circular Singly-Linked Chains of Nodes ... last node loops back to first node if the first and last positions don't matter then just use a curr reference otherwise, use a tail ref to identify the last node (which links to the first node) Circular Doubly-Linked Chains of Nodes ... if first and last positions don't matter then just use a curr reference which holds the current node otherwise just use a head reference pointing to first node in the chain Copyright 2014-2015 Jim Skrentny CS 367 (F15): L9 - 10 ...
View Full Document

  • Spring '08
  • MarvinSolomon
  • Data Structures, private int numitems, ListNode, Jim Skrentny

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture