








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Unknown 1989;
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









a b c d e
c a e d b
c a e d b
firstNode
package dataStructures;
class ChainNode { // package visible data members Object element; ChainNode next;
// constructors come here }
next element
ChainNode() {}
null null
null element
next element
ChainNode(Object element) {this.element = element;}
ChainNode(Object element, ChainNode next) {this.element = element; this.next = next;}
get(0)
checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode.element;
a b c d e
null
firstNode
get(1)
checkIndex(1); desiredNode = firstNode.next; // gets you to second node return desiredNode.element;
a b c d e
null
firstNode
NullPointerException
desiredNode = firstNode.next.next.next.next.next.next; // gets the computer mad // you get a NullPointerException
a b c d e
null
firstNode
Remove An Element
remove(0)
a b c d e
null
firstNode
firstNode = firstNode.next;
a b d e
null
firstNode
c
remove(2)
first get to node just before node to be removed
cc
beforeNode = firstNode.next;
b
beforeNode
remove(2)
now change pointer in beforeNode
beforeNode.next = beforeNode.next.next;
beforeNode
a b c d e
null
firstNode
One-Step add(0,’f’)
a b c d e
null
firstNode
f
newNode
firstNode = new ChainNode( new Character(‘f’), firstNode);
add(3,’f’)
a b c d e
null
firstNode f
newNode
beforeNode
c
ChainNode newNode = new ChainNode(new Character(‘f’),
beforeNode.next);
Two-Step add(3,’f’)
a b c d e
null
firstNode f
newNode
beforeNode
c