Вы находитесь на странице: 1из 18

Linked Lists

Definition of a linked list


• A linked list is a linear data structure where
each element is a separate object. Each element
(generally called as node) of a list is comprising of
two items – the data and a reference to the next
node. The last node has a reference to null. The
entry point into a linked list is called the head of
the list.

2
Anatomy of a linked list
• A linked list consists of:
– A sequence of nodes

myList

a b c d

Each node contains a value


and a link (pointer or reference) to some other node
The last node contains a null link
The list may have a header

3
More terminology

• A node’s successor is the next node in the sequence


– The last node has no successor
• A node’s predecessor is the previous node in the
sequence
– The first node has no predecessor
• A list’s length is the number of elements in it
– A list may be empty (contain no elements)

4
Pointers and references
• In C and C++ we have “pointers,” while in Java
we have “references”
– These are essentially the same thing
• The difference is that C and C++ allow you to modify pointers
in arbitrary ways, and to point to anything
– In Java, a reference is more of a “black box,” or ADT
• Available operations are:
– dereference (“follow”)
– copy
– compare for equality
• There are constraints on what kind of thing is referenced: for
example, a reference to an array of int can only refer to an
array of int
5
Creating references
• The keyword new creates a new object, but also
returns a reference to that object
• For example, Person p = new Person("John")
– new Person("John") creates the object and returns a
reference to it
– We can assign this reference to p, or use it in other
ways

6
Creating links in Java
myList:

44 97 23 17

class Cell { int value;


Cell next;
Cell (int v, Cell n) { // constructor
value = v;
next = n;
}
}
Cell temp = new Cell(17, null);
temp = new Cell(23, temp);
temp = new Cell(97, temp);
Cell myList = new Cell(44, temp);
7
Singly-linked lists
• Here is a singly-linked list (SLL):

myList

a b c d

• Each node contains a value and a link to its


successor (the last node has no successor)
• The header points to the first node in the list (or
contains the null link if the list is empty)

8
Singly-linked lists in Java
public class SLL {
• This class actually
private SLLNode first; describes the header of a
singly-linked list
public SLL() {
this.first = null; • However, the entire list is
} accessible from this header
• Users can think of the SLL
// methods...
as being the list
}
– Users shouldn’t have to
worry about the actual
implementation

9
SLL nodes in Java
public class SLLNode {
protected Object element;
protected SLLNode succ;

protected SLLNode(Object elem,


SLLNode succ) {
this.element = elem;
this.succ = succ;
}
}

10
Creating a simple list
• To create the list ("one", "two", "three"):
• SLL numerals = new SLL();
• numerals.first =
new SLLNode("one",
new SLLNode("two",
new SLLNode("three", null)));

numerals

one two three

11
Traversing a SLL
• The following method traverses a list (and
prints its elements):
public void printFirstToLast() {
for (SLLNode curr = first;
curr != null;
curr = curr.succ) {
System.out.print(curr.element + " ");
}
}
• You would write this as an instance method
of the SLL class
12
Implementation of linklist
• Insertion
• Traversal
• Deletion

13
Doubly-linked lists
• Here is a doubly-linked list (DLL):
myDLL

a b c

• Each node contains a value, a link to its successor (if any),


and a link to its predecessor (if any)
• The header points to the first node in the list and to the last
node in the list (or contains null links if the list is empty)

14
DLLs compared to SLLs
• Advantages: • Disadvantages:
– Can be traversed in either – Requires more space
direction (may be essential – List manipulations are
for some programs) slower (because more links
– Some operations, such as must be changed)
deletion and inserting – Greater chance of having
before a node, become bugs (because more links
easier must be manipulated)

15
Deleting a node from a DLL
• Node deletion from a DLL involves changing two links

myDLL

a b c

• Deletion of the first node or the last node is a special case


• Garbage collection will take care of deleted nodes

16
Advantages of Linklist

• Dynamic Data Structure


Linked list is a dynamic data structure so it can grow and shrink at runtime by
allocating and deallocating memeory. So there is no need to give initial size
of linked list.
• Insertion and Deletion
Insertion and deletion of nodes are really easier. Unlike array here we don’t
have to shift elements after insertion or deletion of an element. In linked list
we just have to update the address present in next pointer of a node.
• No Memory Wastage
As size of linked list can increase or decrease at run time so there is no
memory wastage. In case of array there is lot of memory wastage, like if we
declare an array of size 10 and store only 6 elements in it then space of 4
elements are wasted. There is no such problem in linked list as memory is
allocated only when required.
• Implementation
Data structures such as stack and queues can be easily implemented using
linked list.
17
Disadvantages of Linklist

• Memory Usage
More memory is required to store elements in linked list as compared to
array. Because in linked list each node contains a pointer and it requires extra
memory for itself.
• Traversal
Elements or nodes traversal is difficult in linked list. We can not randomly
access any element as we do in array by index. For example if we want to
access a node at position n then we have to traverse all the nodes before it.
So, time required to access a node is large.
• Reverse Traversing
In linked list reverse traversing is really difficult. In case of doubly linked
list its easier but extra memory is required for back pointer hence wastage of
memory.

18

Вам также может понравиться