You are on page 1of 36

Data Structures - I

“No Bonus” marks for this course anymore

CP
CS
20
4

and say, O my Lord, Give me more knowledge.


(Quran 20 : 114)

1 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Introduction

2 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List
CS
• Abstract Data Type (ADT) or Data Structure
20
4 • A linked list is a sequence of nodes in which
each node is linked to the node following
it.

3 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Array vs Linked List


CS
• Static • Dynamic
20
4 • Contiguous • Non - Contiguous
• Direct Access • No Direct Access
• Homogenous • Homogenous
• Linear • Linear
• Physical • Physical

4 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Array vs Linked List


CS
• Advantages • Advantages
20
o Multiple values o Dynamic
4 o Direct Access o Fast Insertion
• Disadvantages o Fast Deletion
o Static • Disadvantage
o Slow Insertion (shifting) o No Direct Access
o Slow Deletion (shifting) o Traversing is slow

5 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List
CS • A linked list is an ordered collection of data
20 o Each element (generally called nodes) contains the location
4 of the next element in the list
• Each node essentially has two parts:
o The data part
• If this was a list of student records, for example, the data here may
consist of a name, PID, social security number, address, phone, email,
etc.
o The link part
• This link is used to chain the nodes together.
• It simply contains a reference that points to the next node in the linked
list data next
• Variable is often called “next”
6 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Node – Class Declaration data next

CS public class LLnode {


private int data;
20 private LLnode next;

4 // Constructor 1
public LLnode(int i) {
this(i, null);
}

// Constructor 2
public LLnode(int i, LLnode n) {
data = i;
next = n;
}
}

7 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Node - Creation
CS LLnode a = new LLnode(1);
LLnode b = new LLnode(2);
20 LLnode c = new LLnode(3);
4 a b c
1 null 2 null 3 null
data next data next data next

a.next = b;
b.next = c;

8 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Node - Access
CS a b c
20 1 null 2 null 3 null
4 data next data next data next

System.out.print(a.data); is printed 1
System.out.print(a.next.data); is printed 2
System.out.print(a.next.next.data); is printed 3
System.out.print(b.next.data); is printed 3
System.out.print(c.next.data); !ERROR

9 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Node - Structure data next

CS • Not limited to TWO parts only i.e. data & next


20 • It could be 20+ separate fields of information storing name, address,
phone, email, etc.
4 name id gradePoints next
public class LLnode {
private String name;
private String id;
private int gradePoints;
private LLnode next;

// more code here


};

10 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List
CS
• You can think of each node as a record
20 o The first part of the record is all the necessary data
4 o The final part of the record is a field that stores null a
pointer to the next node in the list
10 20 50

o So who knows where is node 50?


• The next of node 20.
o And who knows where is node 20?
• The next of node 10.
11 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List
CS
• Head of the list
20 o Each node of the list is created dynamically and
4 points to the next node
• So from the first node, we can get to the second node
• From the second node, we can get to the third node
• and so on
o You must have a reference variable that simply
points to the first node of the list
• Usually this variable is named head
• This variable has ONE goal in life: point to the first
node!
12 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List
CS • So we will need two Java Classes
20 1. The first class is for the individual nodes of the linked list
4 class Llnode
- We use this class to make individual node objects.
2. The second class is a class that controls the list and
stores access to the actual list
class LinkedList
• This class defines the head/front reference variable
• It can also define a “tail” (or end of list) reference variable
o Depends on your own implementation
• This class also defines many methods that operate on the list
o insertion, deletion, searching, printing nodes, etc.

13 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Class – LinkedList
CS public class LinkedList {
public boolean isEmpty() {
20 private LLnode head;
if (head == null)
4 // Constructor
public LinkedList() {
return true;
else
head = null; return false;
} }

// Example Method to check if list is empty


public boolean isEmpty() {
return head == null;
}

// All methods will be written here


}
14 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Visual Representation- LinkedList class


CS
20 head
4 10 20

insert()
delete()
search() null

Many methods that
operate on the list. 50

15 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Operations

16 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – isEmpty( )


CS
• There is no nodes public class LinkedList {
20 private LLnode head;
in the list // Constructor
4 public LinkedList() {
• Head points to null head = null;
}
null
head public boolean isEmpty() {
return head == null;
An empty linked list }
}

• isFull( )
o Not Needed
17 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
head head head head

CP 10 20 50 null
null

CS • Display the list data


20 • Head points to first node 10 20 50

4 public void printAllNodes() {


while (head != null) {
System.out.print(head.data + “ ”);
head = head.next;
}
}

• The linked list is no more accessible


• Lost the address of first node
Linked List – Traverse
18 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPtr helpPtr helpPtr helpPtr
head
CP 10 20 50 null
null

CS • Display the list data


20 • Head points to first node
4 • Use temporary object “helpPtr”
public void printAllNodes() {
LLnode helpPtr = head;
while (helpPtr != null) {
System.out.print(helpPtr.data + “ ”);
helpPtr = helpPtr.next;
}
}

Linked List – Traverse


19 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPtr helpPtr helpPtr helpPtr
head
CP 20
10 20
30 60
50 null
null

CS • Modify the content of each node


20 • Head points to first node
4 • Use temporary object “helpPtr”
public void printAllNodes() {
LLnode helpPtr = head;
while (helpPtr != null) {
helpPtr.data = helpPtr.data + 10;
helpPtr = helpPtr.next;
}
}

Linked List – Modifying


20 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Operations
(Searching)

21 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore
helpPtr helpPtr helpPtr helpPtr
head
CP 10 20 50 null
null

CS • Searching a key value


20 • Head points to first node Return false
4 public boolean searchNode(int value) {
LLnode helpPtr = head;
while (helpPtr != null) {
if (helpPtr . data == value;
return true;
helpPtr = helpPtr.next;
}
return false;
}

Linked List – Searching 60


22 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
helpPtr helpPtr
head
CP 10 20 50 null

CS • Searching a key value


20 • Head points to first node Return true
4 public boolean searchNode(int value) {
LLnode helpPtr = head;
while (helpPtr != null) {
if (helpPtr . data == value;
return true;
helpPtr = helpPtr.next;
}
return false;
}

Linked List – Searching 20


23 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4
Linked List
Operations
(Insertion)

24 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Insertion


CS
• 4 – Cases
20
o List is empty
4
o Insert as a first node

o Insert as a last node

o Anywhere between two nodes

25 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Insertion– Empty list


CS
• Check list is empty?
20
o head == null
4
• Create new node
o new LLnode(value) OR
o new LLnode(value,null) OR 10 null
o new LLnode(value,head)

• Make head point to new memory


• head =new LLnode(value) OR 10 null
• head =new LLnode(value, head)
26 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Insertion (Empty)


CS public void insertEmptylist(int value) {
20 // Insertion Empty
if (head == null) {

4 head = new LLnode(value, head)


}
}

head

null

10 null

• Value = 10
27 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Insertion– as First node


CS
• Can value be inserted as first? head

20 o head.data > value 10 20 50 null


4
• Create new node
o new LLnode(value, head) 5

• Make head point to new memory


• head =new LLnode(value, head)head
5 10 20 50 null

28 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Insertion (First)


CS public void insertFirst(int value) {
20 // Insertion as First node for non-empty list
if (head.data > value) {

4 head = new LLnode(value, head)


}
}
head

5 10 20 50 null

• Value = 5
29 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Insertion (First & Empty)


CS public void insert(int value) {
20 // Insertion as First node, Empty or non-empty list
if (head == null || head.data > value) {

4 head = new LLnode(value, head)


}

30 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP Insertion– as Last node


CS
• Traverse to the last node helpPtr helpPtr
20 o helpPtr = head head
10 20 50 null
4 o helpPtr.next != null
• Create new node
60 null
o new LLnode(value, helpPtr.next)

• Make helpPtr.next point to new memory


• helpPtr.next =new LLnode(value, helpPtr.next)
head

10 20 50 60 null
31 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
Insertion – as Last node
“No Bonus” marks for this course anymore
60
helpPtr helpPtr helpPtr
head
CP 10 20 50 null 60 null

CS public void insertLast(int value) {


20 list
// Insertion as First node, Empty or non-empty

4 if (head == null || head.data > value) {


head = new LLnode(value, head)
}
// Insertion anywhere else even at last
else {
LLnode helpPtr = head;
while (helpPtr.next != null) {
helpPtr = helpPtr.next;
}
// Now make new node and insert
helpPtr.next = new LLnode(value, helpPtr.next);
}
32 }
Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Insertion – in middle
CS
• Find the location of the new node’s
20
predecessor
4
• Create new node
• Point the new node to the successor
• Point the predecessor node
to the new node

33 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
Insertion – in middle
“No Bonus” marks for this course anymore
30
30
helpPtr helpPtr
head
CP 10 20 50 null 60 null

CS public void insertMiddle(int value) {


20 // Insertion as First node, Empty or non-empty list
if (head == null || head.data > value) {
4 head = new LLnode(value, head)
}
else {
LLnode helpPtr = head;
while (helpPtr.next != null) {
if (helpPtr.next.data > value)
break; // stop traversal
helpPtr = helpPtr.next;
}
helpPtr.next = new LLnode(value, helpPtr.next);
}
}
34 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore

CP Linked List – Insertion (Complete Code)


CS public void insert(int value) {
20 // Insertion as First node, Empty or non-empty list
if (head == null || head.data > value) {

4 head = new LLnode(value, head)


}
// Insertion anywhere else even at last
else {
LLnode helpPtr = head;
while (helpPtr.next != null) {
if (helpPtr.next.data > value)
break; // we found our spot and stop traversal
helpPtr = helpPtr.next;
}
// Now make new node and insert
helpPtr.next = new LLnode(value, helpPtr.next);
}
}

35 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan


Data Structures - I
“No Bonus” marks for this course anymore

CP
CS
20
4

Allah (Alone) is
Sufficient for us, and He
is the Best Disposer of
affairs (for us).(Quran 3 : 173)
36 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan

You might also like