SE233 Linked Lists
SE233 Linked Lists
&
Algorithm Analysis
Linked List
Stack
Queue
Tree
Network
Graph
Linked List
Head
Finding/searching:
O(n)
Static Implementation
Dynamic Implementation
Static Implementation of List
Node Definition List Definition
public class Node { public class LList {
private double data; private int head;
private int next; private Node[]list;
public Node(){} public LList(int size){
public Node(double list = new Node[size];
data){ head= -1;
this.data = data; }
next = -1; /*getters and Setters go
} here */
/*getters and Setters go }
here */
}
Example: Use of List to maintain
real numbers in ascending order
Example
# Data Next
0
1
Let us see the effect
2
of inserting the
3
following numbers on
4 the List:
5
24, 25.6, -29.4, 21,
6
210.5, -23, 22.3, -
… 26.2
99
Example: Create the list
# Data Next
0
1 At first, List shall be
2 “created” as empty.
3 Hence, head should point
to “null”.
4
5 How shall “null” be
6 represented in the array
… implementation?
head = -1;
99
Example: Inserting a new element
# Data Next
0 24 -1
1
Inserting 24
2
3
4 List[0].data = 24;
5 List[0].next = -1;
6
… head 0
99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 -1
Inserting 25.6
2
3
4 List[1].data = 25.6;
5 List[1].next = -1;
6 List[0].next = 1;
… head 0 (unchanged!)
99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 -1
Inserting -29.4
2 -29.4 0
3
List[2].data = -29.4;
4
5
6 List[2].next = 0;
…
head 2 (changed!)
99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 -1
2 -29.4 3 Inserting 21
3 21 0
List[3].data = 21;
4
List[3].next = 0;
5
6
List[2].next = 3;;
…
head 2 (unchanged!)
99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 4
Inserting 210.5
2 -29.4 3
3 21 0
List[4].data = 210.5;
4 210.5 -1
List[4].next = -1;
5
6
List[1].next = 4;
…
head 2 (unchanged!)
99
Example: Inserting …
# Data Next
0 24 1
1 25.6 4
2 -29.4 7 This is what the List
3 21 6 looks like when all
4 210.5 -1 the data are inserted.
5 -23 3
6 22.3 0 head 2
7 -26.2 5
…
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 4
2 -29.4 7 Deleting -23
3 21 6
4 210.5 -1 List[7].next = 3;
5 -23 3
6 22.3 0 head 2 (unchanged!)
7 -26.2 3
Is there anything that can
… be done to the node “-23”?
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 4
Deleting 22.3
2 -29.4 7
3 21 0 List[3].next = 0;
4 210.5 -1
5 -23 3 head 2 (unchanged!)
6 22.3 0
7 -26.2 3 Is there anything that
can be done to the node
… “22.3”?
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 4
2 -29.4 7 Deleting -29.4
3 21 0
4 210.5 -1 head = 7 (changed!)
5 -23 3
6 22.3 0 Is there anything
7 -26.2 3 that can be done to
… the node “-29.4”?
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 -1
Deleting 210.5
2 -29.4 7
3 21 0 List[1].next = -1;
4 210.5 -1
5 -23 3 head = 7 (unchanged!)
6 22.3 0
7 -26.2 3 Is there anything that
can be done to the node
… “210.5”?
99
Example: Insertion again …
# Data Next
0 24 1
1 25.6 -1
2 -29.4 7 Inserting 23.4
3 21 0
4 210.5 -1 Where shall it be
5 -23 3 inserted?
6 22.3 0
7 -26.2 3
…
99
Insertion again … Two options
# Data Next
0 24 1
1 25.6 -1 Inserting 23.4
2 -29.4 7
3 21 0 1. To insert it at the next
4 210.5 -1 free space (ie. at 8).
5 -23 3
6 22.3 0 2. To insert it at one of
7 -26.2 3 the deleted spaces (ie.
among 2, 4, 5, 6).
…
99
Option 1: Insert at the next free space
# Data Next
0 24 1
1 25.6 -1
2 -29.4 7 This option does not
3 21 0 help us manage the
210. deleted spaces. It
4 -1
5 simply continues to
5 -23 3 provide us free
6 22.3 0 spaces until the
7 -26.2 3 array is exhausted.
…
99
Option 2: Insert at one of the deleted
spaces
# Data Next
0 24 1
1 25.6 -1
In this option, we
2 -29.4 7 need to define a
3 21 0 mechanism to
4 210.5 -1 identify the deleted
5 -23 3 spaces. Besides, we
6 22.3 0 still need to use the
7 -26.2 3 free spaces as well.
…
99
Option 3: Devise a mechanism that manages
free and deletes spaces
# Data Next
0 24 1
1 25.6 -1
One possible mechanism is
2 -29.4 7 to maintain the free spaces
3 21 0 (including the deleted
ones) in a “separate”
4 210.5 -1 linked list inter woven in
5 -23 3 the same array. In other
words, we manage the
6 22.3 0 linked list of floating
7 -26.2 3 numbers and the linked list
… of free spaces (ie. indices)
both in the same array.
99
Option 3: Two linked lists …
# Data Next
0
1
Linked list of floating numbers
2
Floating #
3
4
5 Linked list of free spaces
6
7
…
99
Initially … the linked list of floating numbers
# Data Next
0
1
2 The linked list of floating #s is
3 empty. Hence:
head = -1
4
5
6
7
…
99
Initially … the linked list of free spaces
# Data Next
0 1
1 2
2 3 We can create a linked list of free
3 4 spaces as follows:
for(int i=0; i<=size-2; i++){
4 5
List[i] = new Node();
5 6
List[i].setNext(i+1);
6 7 }
7 8 List[size-1] = new Node();
… List[size-1].setNext(-1);
99 -1 setAvail(0);
setHead(-1);
Static Implementation for a
Linked List
//Code Goes Here
Dynamic Implementation of List
Node Definition List Definition
public class Node { public class LList {