Linked List - Introduction - Traversing
Linked List - Introduction - Traversing
I
T
2
0 Queue Data Structure
Introduction
8
Lecture
1 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 Linked List
0
8
Introduction
Linked List
• Abstract Data Type (ADT) or Data Structure
I oA linked list is a sequence of nodes in which
T each node is linked to the node following it.
2
0
8
Linked List
• A linked list is:
o An ordered collection of data or
I o Series of connected nodes
o Each element (generally called nodes) contains the
T location of the next element(node) in the list
2 • Each node essentially has two parts:
oThe data part
0 • The data part contains the data item in a node.
oThe link(next) part
8 • Link contains a reference that points to the next
node in the linked list
Null
• Variable is often called “next”
data next
• Head: pointer to the first node
4 • The last node points to NULL Dr. Belal Murshed
Data Structures and Algorithms
Differences between Array and
Linked List
I
T
2
Direct access No Direct access
0
8
Similarities between Array and Linked List
1. Physical 1. Physical
2. Linear 2. Linear
3. Homogenous 3. Homogenous
5 Dr. Belal Murshed
Data Structures and Algorithms
Single-linked list
• In a singly linked list,
oOne way change
I oEach node contains a reference to the next
T node in the sequence.
2 oTraversing a singly linked list is done in a
forward direction (One way change)
0
8
Double-linked list
• In a doubly linked list,
oTwo way change
I o Each node contains references to both the
T next and previous nodes.
2 oThis allows for traversal in both forward and
backward directions, but it requires additional
0 memory for the backward reference
8
Circular-linked list
• In a circular linked list,
oThe last node points back to the head node,
I creating a circular structure.
T oCircular linked list can be either singly or
doubly linked.
2
o Doubly Circular linked list
0 o Singly Circular linked list
8
𝐷1 next
0
𝐷2 Address Address
8 𝐷2 next
𝐷3 null Address
𝐷3 next
Circular-linked list
• In a circular linked list,
oTwo way change
I oThe last node points back to the head node,
T creating a circular structure.
2 oCircular linked list can be either singly or
doubly linked.
0 o Doubly Circular linked list
8 o Singly Circular linked list
Node - Creation
a = new LLnode(1);
b = new LLnode(2);
I c = new LLnode(3);
T a b c
1 null 2 null 3
2 data next data next data next
0
a.next = b;
8 b.next = c; null
Node - Access
a b c
I 1 null 2 null 3 null
data next data next data next
T
2 print(a.data); 1 is printed
print(a.next.data); 2 is printed
0 print(a.next.next.data); 3 is printed
print(b.next.data); 3 is printed
8 print(c.next.data); ERROR!
Linked List
• You can think of each node as a record
oThe first part of the record is all the necessary
I data
T oThe final part of the record is a field that stores a
pointer to the next node in the list null
2
10 20 50
0
8 oSo who knows where is node 50?
• The next of node 20.
oAnd who knows where is node 20?
• The next of node 10.
18 Dr. Belal Murshed
Data Structures and Algorithms
Linked List
• Head of the list
I oEach node of the list is created dynamically and
points to the next node
T • So from the first node, we can get to the second node
2 • From the second node, we can get to the third node
• and so on
0 oYou must have a reference variable that simply
8 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!
19 Dr. Belal Murshed
Data Structures and Algorithms
Linked List
• So we will need two Java Classes
1. The first class is for the individual nodes of the linked
I list
class Llnode
T - We use this class to make individual node objects.
2 2. The second class is a class that controls the list and
stores access to the actual list
0 class LinkedList
• This class defines the head/front reference variable
8 • 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.
I
T
2 Linked List
0
8
Operations
Operations
• Is Empty
I • Traversing
T • Searching
2 • Insertion
0 • Deletion
8 • Merging
oNot Needed
23 Dr. Belal Murshed
Data Structures and Algorithms
method
T while (head != null)
cout<<head.data << “ ”
2 head = head.next;
whileend
0 Algend
I
• Design an algorithm to insert an element
T
2 into single linked list (SLL) such that the
0 insert element become the first element
8 in the list
T Algorithm insert_first_SLL
Input:
2 e, element to be inserted
Head address of first node
Output:
0 Head updated(list updated)
Method
8 LLnode *NewNode;
NewNode = new LLnode;
NewNode -> data = e;
NewNode -> next= head;
head = NewNode;
28 Algorithmend Dr. Belal Murshed
Data Structures and Algorithms
I
• Design an algorithm to insert an element
T
2 into single linked list (SLL) such that the
0 insert element become the last element in
8 the list
Algorithm insert_first_SLL
Input:
e, element to be inserted
I Head address of first node
Output:
T Head updated(list updated)
Method
LLnode *NewNode;
2 NewNode = new LLnode;
NewNode-> data = e;
0 NewNode-> next= null;
temp=head
8 While(temp->.next != null)
temp=temp-> next
temp-> next= NewNode
Algorithmend
I
T • Design an algorithm to delete the last
2 node of the given singly linked list (SLL)
0
8
Algorithm Delete_first_SLL
Input:
head, address of first node
Output:
head, updated(list updated)
Method
I If head == null then
print “no delete, list is empty”
T else head->.next == null then
T=head
2 Head = null
Dispose (T)
0 else
While(T->.next.next != null)
8 T = T-> next
whileend
T1=T-> next
T-> next=null
Dispose (helpPtr)
34 ifend Dr. Belal Murshed
Algorithmend
Data Structures and Algorithms
I
T
2
0
8