next element next next next element element element Baltimore Rome Seattle Toronto link: The next reference inside a node is a link or pointer to another node... head next element next
Trang 1Singly Linked Lists
Trang 2Definition: A linked list is a colleciton of nodes that
together form a linear ordering
node: A compound object that stores a reference to an
element and a reference, called next, to another node
Reference to another node
Reference to an element
next Element Node
Trang 3next
element
next next next
element element element
Baltimore Rome Seattle Toronto
link: The next reference inside a node is a link or pointer to
another node
Trang 4We can start from a given node, and move from it to the next
and so on This is called link hopping or pointer hopping
head
next
element
next next next
element element element
Baltimore Rome Seattle Toronto
Trang 5head: The first node of a linked list
tail: The last node of a linked list - it has a null next reference
head
next
element
next next next
element element element
Baltimore Rome Seattle Toronto
Such a linked list is called a singly linked list
tail
Trang 6pointer to anext node
pointer to
an elementnode
Illustration of a linked list in memory:
Trang 7pointer to anext node
pointer to
an elementnode
Trang 8pointer to anext node
pointer to
an elementnode
Trang 9pointer to anext node
pointer to
an elementnode
Trang 11Singly Linked Lists and Arrays
Trang 12Class Node
Here is an implementation of nodes in Java:
public class Node {
private Object element;
private Node next;
Trang 13Object getElement() {
return element }
Trang 14Insertion of an Element at the
Head
Before the insertion:
head
Trang 15Have a new node:
head
next element
The following statement is not correct:
x.element = new String(“Baltimore”));
Trang 16After the insertion:
head
next element
Trang 17Deleting an Element at the Head
Before the deletion:
head
next element
next element element elementBaltimore Rome Seattle Toronto
Trang 18Remove the node from the list:
head
next element
Trang 19After the deletion:
head
Trang 20Insertion of an Element at the
Tail
Before the insertion:
head
tail
Trang 21Have a new node:
head
next
element
next next next
element element element
Baltimore Rome Seattle Toronto
tail
Node x = new Node( );
x.setElement(new String(“Baltimore”));x.setNext(null);
tail.setNext(x);
tail = x;
Trang 22After the insertion:
head
next
element
Baltimore Rome Seattle Toronto
tail
Trang 23How to keep “head” and “tail”?
public class Head_and_Tail {
Node head;
Node tail;
Head_and_Tail(Node x, Node y) {
head = x;
tail = y;
}}
Trang 24How to keep “head” and “tail”?
public class GeneratingList {
Node head = null;
Node tail = null;
Public Head_and_Tail linked_list () {
Node x = null;
for (int i = 0; i < 10; i++)
{x = new Node(); x.setElement(new Integer(i));
if (i == 0 ) {x.setNext(null); tail = x;} else x.setNext(head);
head = x;
}
return new Head_and_Tail(head, tail);}
}
Trang 25Deleting an Element at the Tail
Deletion of an element at the tail of a singly linked list takes more effort
The difficulty is related with the fact that the last node does not have a link to the previous node which will become the new tail of the list
Trang 26Scott: Who is McFee?
Gary: I don’t know
Trang 27Before the deletion:
head
next
element
Baltimore Rome Seattle Toronto
tail
Trang 28Remove the node: How can we find the new tail?
next
element
next next next
element element element
Baltimore Rome Seattle Toronto
should be removed
Trang 29How to insert a new node in the middle of a singly linkedlist?
How to remove a node which in the middle of a singlylinked list?
Trang 30Data Structure Exercises 5.1
Write a Java program to create a linked list as shown below
Φ
… …