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

33
Winter 2006 CISC121 - Prof. McLeod 1

Upload: valentine-petty

Post on 30-Dec-2015

18 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Winter 2006 CISC121 - Prof. McLeod 1

Winter 2006 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 [email protected] Leaders begin from the basics.

Winter 2006 CISC121 - Prof. McLeod 3

More Stuff

• Midterm Preparation Page

Winter 2006 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.

Winter 2006 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!)

Winter 2006 CISC121 - Prof. McLeod 6

Singly Linked List - Node Class

public class IntNode {

public int info; // a data valuepublic IntNode next; // a link

// constructorspublic IntNode (int i) { this(i, null); }public IntNode (int i, IntNode n) {

info = i;next = n;

}} // end IntNode

Winter 2006 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

Winter 2006 CISC121 - Prof. McLeod 8

Singly Linked List – Use of Inner Classes

• The node object, IntNode, is a public class and its attributes (info and next) are public.

• This is necessary so that the IntSLList class 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 IntSLList as:

Winter 2006 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

Winter 2006 CISC121 - Prof. McLeod 10

Use of Inner Classes – Cont.

• Note that even though the inner class is private it 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!

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

Winter 2006 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?)

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

Winter 2006 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

Winter 2006 CISC121 - Prof. McLeod 15

Deleting the Head Node, Cont.

15

headtail

10 5null

15

head tail

10 5null

• After head = head.next;

• What happens to the node with 15?

Winter 2006 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!

Winter 2006 CISC121 - Prof. McLeod 17

• First, locate the node, then delete it.

• It is way too big a method to show on this slide!!• See the next one:

Deleting the Node that Contains the Value “delNum”

Winter 2006 CISC121 - Prof. McLeod 18

public void delete (int delNum) { // does not return iif (!isEmpty())

if (head==tail && delNum==head.info) { head = null; tail = null;} // only 1 nodeelse 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

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

Winter 2006 CISC121 - Prof. McLeod 20

Deleting an Inner Node - An Example

list.delete(10);

15

head

10 520tail

-5null

15head

10 520tail

-5null

15head

10 520tail

-5null

pred temp

pred temp

pred temp

pred.next = temp.next;

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

(The node is “garbage collected” when execution moves beyond the scope of the variable temp.)

Winter 2006 CISC121 - Prof. McLeod 22

Deleting an Inner Node – Iterators

• Note the use of the pred and temp objects in the delete method:– (“Java jargon”) These are called “iterators”

because they are used to move through the list.

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

15

head

10 5null

20

tail

Winter 2006 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:

Winter 2006 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 ifreturn i;

} // end removeTail method

Winter 2006 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.

Winter 2006 CISC121 - Prof. McLeod 27

Doubly Linked Listspublic 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

Winter 2006 CISC121 - Prof. McLeod 28

Doubly Linked List – Cont.

• Structure:

head20

tail

null

10 5nullnext

prev

Winter 2006 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

Winter 2006 CISC121 - Prof. McLeod 30

Doubly Linked List – Cont.

dLList.addToTail(-10);head

20tail

null

10 5null

head

20

tail

null

10 5null

-10null

head

20

tail

null

10 5 -10null

-10null

After “new…”:

After tail.prev.next = tail;

After tail=…;

Winter 2006 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 ifreturn i;

} // end removeFromTail

Winter 2006 CISC121 - Prof. McLeod 32

Doubly Linked List – Cont.

int temp = dLList.removeFromTail();head

20

tail

null

10 5 -10null

After tail = tail.prev;

Before

head

20

tail

null

10 5 -10null

After tail.next = null;head

20

tail

null

10 5null

-10null

temp is -10

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