You are on page 1of 18

DATA STRUCTURES AND

ALGORITHM
Linked List
LINKED LISTS

 The linked list consists of a series of nodes, which are


not necessarily adjacent in memory.
 This enables us to avoid the cost of insertion and
deletion.
LINKED LISTS - IMPLEMENTATION IDEA
 Each node in a list contains the element and a link to a
node containing its successor.

 Each node by itself is an ADT.


LINKED LISTS - NODE
IMPLEMENTATION
LINKED LISTS - BASIC
IMPLEMENTATION
 Next, lets implement actual list composed of the nodes

 Note that we maintain a reference to the last item in the


list. This makes insertion at the end more efficient.
LINKED LISTS - BASIC OPERATIONS
   addition to storing just the head and tail, the ADT
In
must implement basic operations:
 Insertionand Deletion
 Find item in list - similar to writing a[k] in an array
 Search

 These operations are common to all lists


 Insertion and deletion at the front and tail of a list are
frequently performed.
 We will give special attention to both.
LINKED LISTS - INSERTION AT THE
FRONT
 The figure shows the steps necessary to insert the
number ”6” at the beginning of the list
LINKED LISTS - INSERTION AT THE
FRONT(CODE)
LINKED LISTS - INSERTION AT THE TAIL
 The figure shows the steps necessary to insert the
number ”10” at the end of the list
LINKED LISTS - INSERTION IN THE
MIDDLE
LINKED LISTS - DELETION AT THE
FRONT

 Q: is it OK to leave the node for ”6” in memory without


deleting it?
 A: In Java it’s garbage collected since there is no
reference to it. (In
 languages like C++ you have to delete it manually.)
LINKED LISTS - DELETION AT THE TAIL
LINKED LISTS - DELETION IN THE
MIDDLE
MOTIVATION FOR DOUBLY LINKED
LISTS
 Deletion from tail indicates a problem inherent to singly
linked lists
 We have no direct access to the node that precedes the
tail.
 We have to scan the entire list and stop right in front of
tail to delete it
 If we frequently delete from the end of the list, this will
be a problem
 If a node contains a reference both to its successor and
predecessor, deletion from the tail will be more efficient.
 We can access the predecessor of the tail node
DOUBLY LINKED LISTS-
REPRESENTATION
 Each node in the list has two reference fields, one to the
successor and one to the predecessor.

 Much of the ideas we have discussed to singly linked


lists applied here
DOUBLY LINKED LISTS - DELETION
FROM THE TAIL
ARRAYS VS LINKED LISTS
  Accessing the item:
 Array:O(1)
 Linked List: O(i )

 Insertion:
 Array: You might need to re-allocate an array with larger size.
(To add one element to an array of size ∼ 1GB, you an
additional ∼ 1GB)
 Linked List: No need to recreate the list, in-place insertion

 Deletion:
 Array: We need to shift the items
 Linked List: We just need to ”re-wire” references
 ARRAYS LINKED LISTS(CONTD)
 Use an array if:
 The size of the list remains fairly static once loaded into
memory.
 You need to access random location in the list quickly

 Use a linked list if:


 The list is likely to undergo extensive insertion and deletion
once loaded into memory.

You might also like