Professional Documents
Culture Documents
CPCS204-04-Linked List - Introduction-Traversal-Searching
CPCS204-04-Linked List - Introduction-Traversal-Searching
CPCS204-04-Linked List - Introduction-Traversal-Searching
CP
CS
20
4
CP
CS
20
4
Linked List
Introduction
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.
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
4 // Constructor 1
public LLnode(int i) {
this(i, null);
}
// Constructor 2
public LLnode(int i, LLnode n) {
data = i;
next = n;
}
}
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;
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
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
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.
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;
} }
insert()
delete()
search() null
…
Many methods that
operate on the list. 50
CP
CS
20
4
Linked List
Operations
• 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
CP
CS
20
4
Linked List
Operations
(Searching)
CP
CS
20
4
Linked List
Operations
(Insertion)
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
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
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
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
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