Implementation of a Linked List in Java, as given in CSC 232 on 02/03/2012 by Dr. Richard Martin.
/**
* The methods for access would be very similar
* to those in the ArrayList.
* A LinkedList and an ArrayList are both Lists.
* whatever methods for access for a List should
* be present in both types.
* A poodle and a german shepherd are both dogs.
*/
class XLinkedList
{
private Node head;
private class Node
{
private int data;
private Node link;
Node()
{
data = -999; // bad practice, better would be using MIN_VALUE
link = null;
}
Node(d)
{
data = d;
link = null;
}
}
XLinkedList()
{
head = null;
}
public void clearAll()
{
head = null;
}
/**
* In use, to create a linked list, then add
* some data to it:
*
* XLinkedList mylist = new XLinkedList();
* boolean didit = mylist.add(1);
*
* This would create "head" and then create a
* new node with 1 as data, and have
* would be faster to add onto the front, that
* way, you don't need to traverse the list.
* it really depends on if this is ordered or
* ordered
*
**/
public boolean add(int d)
{
Node temp = new Node(d); // temp -> [ 12 |*]
temp.link = head; //
head = temp; // take temp now, which includes a link to
// a null Node, and assign it to head
return true;
}
/**
* When we find it, since we can't reverse p,
* we want p.link to be set to target.link
* when p.link.data == d
*
* chapter two and the singly linked list
* discusses this problem.
*
**/
public boolean remove(int d)
{
Node p; //capable of holding a REFERENCE to a node
// instead of doing something like head.link.link...
p = head; //tells p to look at the same memory reference
// that head is looking at.
/* broken, first attempt:
// indefinite loop
while( p != null && p.data != d ) //possible that the thing we're looking
{ // for is is not in the list
p = p.link; //now p is looking at head.link
} // (first iteration)
*/
while(p.link!=null && p.data != d)
{
p = p.link;
//...
}
}
/**
* Using an aux node reference, we can traverse
*
**/
public boolean contains(int d)
{
}
/**
* what should we do with the node once it is removed?
* what should be returned? should we return a boolean
* as a confirmation that it was removed, or should we
* return the contents of the data of the note that is
* set to be removed.
*
* There is a singly linked list class in the book.
* This is something to have in my portfolio, my
* little book of personal code snippets.
**/
public boolean remove(int target)
{
link c = head;
link p = head;
// if it is the first node
// we would need to make head point to the second node
//if(head.data==target) //rev a
if(c.data==target) //rev b
{
head = head.link; // that first node is kinda different
return true;
}
//always need a where am I? and where was I
// with singly linked lists, because we can
// never back up in a singly linked list.
//at first, we would have C, P, and head all
// looking at the first node
c = c.link; // now we would ask if current is looking at the target
while(c.link != null)
{
if (c.data == target)
{
p.link = c.link;
return true;
}
else
{
c = c.link;
p = p.link;
}
}
// at the last node, check to see if the data is what we're looking for
if(c.data == target)
{
p.link = null;
return true;
}
else
return false;
}
}