Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• Representation
• Space Analysis
• Traversal
• Search
• Deletion
1
What is a Singly-linked list?
• A singly linked list is a dynamic data structure consisting of a
sequence of nodes, forming a linear ordering.
Node:
Singly-linked list:
2
Why linked lists?
• Linked lists are used to implement many important data structures
such as stacks, queues, graphs, hash tables, etc.
3
Singly-linked lists vs. 1D-arrays
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
No memory waste if the array is full or almost Extra storage needed for references; however
full; otherwise may result in much memory uses exactly as much memory as it needs
waste.
Sequential access is faster because of greater Sequential access is slow because of low locality
locality of references [Reason: Elements in of references [Reason: Elements not in
contiguous memory locations] contiguous memory locations]
4
Representation
• We are using a representation in which a linked list has both head
and tail references:
public class MyLinkedList{
protected Element head;
protected Element tail;
public final class Element{
Object data;
Element next;
Element(Object obj, Element element){
data = obj;
next = element;
}
public Object getData(){return data;}
public Element getNext(){return next;}
}
// . . .
}
5
Representation: Space Analysis
• Assume:
– There are n nodes in the list
– All data references are null
• Once created, elements can be inserted into the list using either the
append or prepend methods:
for (int k = 0; k < 10; k++)
list.append(new Integer(k));
8
Insertion at the beginning (Prepend)
public void prepend(Object obj) {
Element element = new Element(obj, head);
if(head == null)
tail = element; Complexity is O(1)
head = element;
}
9
Traversal
Begin at the first node, then follow each next reference until the
traversal condition is satisfied or until you come to the end.
e = e.next;
e.next = element;
element
y
e
e.next
x
• If the new node was inserted at the end of the list, update the tail reference:
if(element.next == null) tail = element;
12
Insertion after an element
• The insertAfter method of the Element class is invoked as:
MyLinkedList.Element e = list.find(obj1);
if(e != null)
e.insertAfter(obj2); // insert obj2 after obj1
else
System.out.println("Element to insert before not found");
Complexity is O(n)
• Note: The total complexity of the insert after operation is O(n) because find is O(n)
13
Insertion before an element
• To insert an object y before a node x:
• Move a reference previous from the beginning of the list to the node before
node x:
Element e = head, previous;
if(e == null) throw new IllegalArgumentException(“not found”);
while(e != null && ! e.data.equals(x)){
previous = e;
previous e
e = e.next;
} x
if(e == null) throw new IllegalArgumentException(“not found”);
• Create a new node containing y as data and let its next reference refer to the
element
y
node x: e
previous
Element element = new Element(y, e); x
• Make the next reference of the node before node x refer to node y:
if(e == head)
head = element; element
y
else
previous e
previous.next = element;
x
14
Insertion before an element
• The insertBefore method of the Element class is invoked as:
MyLinkedList.Element e = list.find(obj1); Complexity is O(n)
if(e != null)
e.insertBefore(obj2); // insert obj2 before obj1
else
System.out.println("Element to insert before not found");
if(element == null)
throw new InvalidOperationException(“Not found”);
element.next = next;
}
if(this == tail)
tail = element; Complexity is …
}
19
Time Complexity: Singly-linked lists vs. 1D-arrays
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
20
Exercises
• Using the Element extract method is less efficient than using the
MyLinkedList extract method. Why?
21
Doubly Linked Lists
• Representation
• Space Analysis
• Traversal
• Deletion
22
What is a Doubly-linked list?
• A doubly linked list is a dynamic data structure consisting of a
sequence of nodes, forming a linear ordering.
Doubly-linked list:
23
Representation
public class DoublyLinkedList{
protected Element head, tail;
//. . .
public class Element {
Object data;
Element next, previous;
24
Doubly-Linked Lists: Space Analysis
• Assume:
– There are n nodes in the list
– All data references are null
26
List Creation and Insertion
27
Insertion at the end (append)
28
Insertion at the beginning (prepend)
29
Traversal
For DoublyLinked list, traversal can be done in either direction.
Forward, starting from the head, or backward starting from the tail.
30
Traversal (cont’d)
Example: The following computes the sum of the last n
nodes:
31
Insertion before an element
• Inserting before the current node (this) that is neither the first nor the
last node:
Element element = new Element(obj, this, this.previous);
this.previous.next = element;
this.previous = element;
Complexity is …
32
Deletion
• To delete an element, we use either the extract method of
DoublyLinkedList or that of the Element inner class.
public void extract(Object obj){
Element element = head;
while((element != null) && (!element.data.equals(obj)))
element = element.next;
33
Exercises
34