Linked List
A Linked List is a list of elements, or nodes, that are linked to one another such that each element references the next. Linked lists and arrays are similar since they both store collections of data, however an array has a fixed size, at least in most commonly used languages; where as a linked list as an arbitrary length, which allows the list to grow and shrink as needed by the client.
Another advantage is that a linked list is a flexible data structure: nodes (items) may be added to it or deleted from it at will. However, a linked list does not allow random access, which is the ability to access an arbitrary element of a sequence in equal time. With that said a programmer does not need to worry about how many items a program will have to contain when using a link list. This ability allows programmers to write larger programs, which requires much less maintenance and memory. A common source of problems in program maintenance is the need to increase the capacity of a program to handle ever increasing collections.
Linked lists can be implemented in many different programming languages, but it is used must commonly in OOD languages such as C++ and Java. They also come in four different types: Singly, Doubly, Circularly Double-Linked, and Circular Doubly-Linked List
Types of Linked Lists

/**
* This interface defines the methods required by any object that can be
* linked into a linked list.
*/
public interface Linkable {
public Linkable getNext(); // Returns the next element in the list
public void setNext(Linkable node); // Sets the next element in the list
}
// This class has a default constructor: public LinkedList() {}
/** This is the only field of the class. It holds the head of the list */
Linkable head;
/** Return the first node in the list */
public synchronized Linkable getHead() {
return head;
}
/** Insert a node at the beginning of the list */
public synchronized void insertAtHead(Linkable node) {
node.setNext(head);
head = node;
}
/** Insert a node at the end of the list */
public synchronized void insertAtTail(Linkable node) {
if (head == null)
head = node;
else {
Linkable p, q;
for (p = head; (q = p.getNext()) != null; p = q)
/* no body */;
p.setNext(node);
}
}
/** Remove and return the node at the head of the list */
public synchronized Linkable removeFromHead() {
Linkable node = head;
if (node != null) {
head = node.getNext();
node.setNext(null);
}
return node;
}
/** Remove and return the node at the end of the list */
public synchronized Linkable removeFromTail() {
if (head == null)
return null;
Linkable p = head, q = null, next = head.getNext();
if (next == null) {
head = null;
return p;
}
while ((next = p.getNext()) != null) {
q = p;
p = next;
}
q.setNext(null);
return p;
}
/**
* Remove a node matching the specified node from the list. Use equals()
* instead of == to test for a matched node.
*/
public synchronized void remove(Linkable node) {
if (head == null)
return;
if (node.equals(head)) {
head = head.getNext();
return;
}
Linkable p = head, q = null;
while ((q = p.getNext()) != null) {
if (node.equals(q)) {
p.setNext(q.getNext());
return;
}
p = q;
}
}
/**
* This is a test class that implements the Linkable interface
*/
static class LinkableInteger implements Linkable {
int i; // The data contained in the node
Linkable next; // A reference to the next node in the list
public LinkableInteger(int i) {
this.i = i;
} // Constructor
public Linkable getNext() {
return next;
} // Part of Linkable
public void setNext(Linkable node) {
next = node;
} // Linkable
public String toString() {
return i + "";
} // For easy printing
public boolean equals(Object o) { // For comparison
if (this == o)
return true;
if (!(o instanceof LinkableInteger))
return false;
if (((LinkableInteger) o).i == this.i)
return true;
return false;
}
}
/**
* The test program. Insert some nodes, remove some nodes, then print
* out all elements in the list. It should print out the numbers 4, 6,
* 3, 1, and 5
*/
public static void main(String[] args) {
LinkedList ll = new LinkedList(); // Create a list
ll.insertAtHead(new LinkableInteger(1)); // Insert some stuff
ll.insertAtHead(new LinkableInteger(2));
ll.insertAtHead(new LinkableInteger(3));
ll.insertAtHead(new LinkableInteger(4));
ll.insertAtTail(new LinkableInteger(5));
ll.insertAtTail(new LinkableInteger(6));
System.out.println(ll.removeFromHead()); // Remove and print a node
System.out.println(ll.removeFromTail()); // Remove and print again
ll.remove(new LinkableInteger(2)); // Remove another one
// Now print out the contents of the list.
for (Linkable l = ll.getHead(); l != null; l = l.getNext())
System.out.println(l);
}
}
References
- Wikipedia. Nov. 10, 2008. Accessed Nov.10,2008. < http://en.wikipedia.
org/wiki/Linked_list >
Wiki - Carrano, Frank M. and Prichard, Janet. J Data Abstraction & Problem Solving With Java. 2nd Edition. Addison Wesley.
- Model Engineering College. Resources. Accessed Nov. 10, 2008. < http://www.mec.ac.in
/resources/notes/not es/ds/ll_files/image 018.jpg>
http://www.mec.ac.in/resources/notes/not es/ds/ll_files/image 018.jpg - Linked List example. java2s.com. Accessed Nov. 10,2008. < http://www.java2s.co
m/Code/Java/Collecti ons-Data-Structure/L inkedListexample.htm >
Source Code









Comments
Write New Comment ▼
Write New Comment
Sorry! This knol's owner(s) have blocked you from editing, making suggestions, or commenting here.