Lecture - 3 - Linked List in Data Structure
Lecture - 3 - Linked List in Data Structure
Muhammad Umer
Department of Computer Science,
KFUEIT
Link
List
Linked List
Linked list is a linear data structure. It contains nodes. Each node contains two
parts, i.e. DATA part and LINK part.
• The data contains elements and
• Link contains address of another node.
23
23
Each node has a link or pointer to the next element of the list,
the next node.
Singly Linked List
23
23
23 3
23 3 9
23 3 9 42
23 3 9 42
next ( )
23 3 9 42
23 3 9 42
23 3 9 42
- to the tail.
Singly Linked List
23 3 9 42
23 3 9 42
to the element 9
Singly Linked List
23 3 9 42
23 3 9 42
next ( )
23 3 9 42
23 3 9 42
next ( )
23 3 9 42
If the list contains a lot of elements, this may take some time.
Singly Linked List
23 3 9 42
23 3 9 42
17
23 3 9 42
17
You link to the new element „17“ from the current element
„9”.
Singly Linked List
23 3 9 42
17
23 3 9 17 42
23 3 9 42
23 3 9 42
23 3 9 42
23
23
23
23
23
so a Linked List that contains nodes that provide a link to the next
and the previous node is called a “Doubly Linked
List”.
Doubly Linked List
23 3
23 3 9
23 3 9 42
23 3 9 42
23 3 9 42
Here we will add the element “17” before the current element
“9”.
Doubly Linked List
17
23 3 9 42
23 3 9 42
23 3 9 42
From the new element “17” we link to the next element “9”.
Doubly Linked List
17
23 3 9 42
23 3 9 42
23 3 17 9 42
public E item() {
return item;
}
public Node(Node<E>
previous, E element,
Node<E> next) {
this.item = element;
this.next = next;
this.previous = previous;
}
public E item() {
return item;
}
public Node<E> previous()
{ return previous;
}
public Node<E> next() {
return next;
}
[…]
}
The Node of a Doubly Linked List is very similar,
but a bit more complex.
Additionally, you have a reference to the previous
Node.
LinkedList
public class LinkedList<E> {
private Node<E>
currentNode;
private Node<E>
head;
[…]
}
A LinkedList usually
contains a link to the
head of the List.
LinkedList
public class LinkedList<E> {
private Node<E>
currentNode;
private Node<E>
head;