0% found this document useful (0 votes)
1 views

Lec7A_LinkedList

The document compares unordered and ordered arrays, highlighting their advantages and disadvantages in terms of insertion, deletion, and search operations. It introduces linked lists as a dynamic data structure that overcomes some limitations of arrays, such as fixed size and costly insertions/deletions. While linked lists allow for flexible sizing and easier management of elements, they do not provide direct access to individual elements, requiring traversal from the head to access specific nodes.

Uploaded by

mike
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lec7A_LinkedList

The document compares unordered and ordered arrays, highlighting their advantages and disadvantages in terms of insertion, deletion, and search operations. It introduces linked lists as a dynamic data structure that overcomes some limitations of arrays, such as fixed size and costly insertions/deletions. While linked lists allow for flexible sizing and easier management of elements, they do not provide direct access to individual elements, requiring traversal from the head to access specific nodes.

Uploaded by

mike
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Advantages Disadvantages

Unordered array Insertion - O(1) Search – O(n)


list Deletion – O(n)
Fixed array size
Ordered array list search –O(log2 n) Insertion - O(n)
Deletion - O(n)
Fixed array size

Better performed when searches are more than insert


 Disadvantages
 arrays are static structures and therefore cannot be
easily extended or reduced to fit the data set.
 arrays are also expensive to maintain new insertions
and deletions.

 In this presentation we consider another data


structure called Linked Lists that addresses
some of the limitations of arrays.
 A linked list is a linear data structure where
each element is a separate object.

 Each element (we will call it a node) of a list is


comprising of two items
 the data and a reference to the next node
• list elements are stored, in memory, in an
arbitrary order

• explicit information (called a link) is used


to go from one element to the next
Layout of L = (a,b,c,d,e) using an array representation.

a b c d e

A linked representation uses an arbitrary layout.


c a e d b
List L = (a,b,c,d,e)

c a e d b

firstNode

pointer (or link) in e is null

use a variable firstNode to get to the


first element a
firstNode

null
a b c d e

link or pointer field of node

data field of node


firstNode

null
a b c d e

•In a linked list each node represents one element.


•There is a link or pointer from one element to the
next.
• The last node has a null pointer.
class Node
{
// data members
Object data; // data field
next
Node next; // link field
data

// constructors come here


}
null next
Node() {}
null data

null next
Node(Object element)
element
{this.data= element;} data

Node(Object element, Node addr) addr next


{this.data = element; element data
this.next = addr;}
firstNode

next null

data a b c d e

checkIndex(0);
return firstNode.data; // gets you to first node
OR
checkIndex(0);
desiredNode = firstNode;
return desiredNode.data;
firstNode

next null

data a b c d e

checkIndex(1);
desiredNode = firstNode.next; // gets you to second node
return desiredNode.data;
firstNode

null
a b c d e

checkIndex(2);
desiredNode = firstNode.next.next; // gets you to third
node
return desiredNode.data;
firstNode

null
a b c d e

checkIndex(5); // throws exception


desiredNode = firstNode.next.next.next.next.next;
// desiredNode = null
return desiredNode.data; // null.data
firstNode

null
a b c d e

desiredNode =
firstNode.next.next.next.next.next.next;
// gets the computer mad
// you get a NullPointerException
firstNode

null
a b c d e

remove(0)

firstNode = firstNode.next;
firstNode

null
c
a b c d e

beforeNode
first get to node just before node to be removed

beforeNode = firstNode.next;
firstNode

null
a b c d e

beforeNode
now change pointer in beforeNode

beforeNode.next = beforeNode.next.next;
firstNode

null
f a b c d e

newNode

Step 1: get a new node, set its data and link fields

Node newNode =
new Node(new Character(‘f’), firstNode);
firstNode

null
f a b c d e

newNode

Step 2: update firstNode

firstNode = newNode;
firstNode

null
f a b c d e

newNode

firstNode = new Node(new Character(‘f’),


firstNode);
firstNode newNode
f

null
a b c d e

beforeNode
• first find node whose index is 2
• next create a node and set its data and link fields
Node newNode = new Node(new Character(‘f’), beforeNode.next);

• finally link beforeNode to newNode


beforeNode.next = newNode;
firstNode newNode
f

null
a b c d e

beforeNode

beforeNode = firstNode.next.next;
beforeNode.next = new Node(new Character(‘f’),
beforeNode.next);
 A linked list is a dynamic data structure. The
number of nodes in a list is not fixed and can grow
and shrink on demand.
 Any application which has to deal with an
unknown number of objects will need to use a
linked list.
 One disadvantage against an array is that
 it does not allow direct access to the individual elements.
If you want to access a particular item then you have to
start at the head and follow the references until you get to
that item.

You might also like