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

Linked List - Introduction - Traversing

The document discusses different types of linked lists including single, double, and circular linked lists. It describes the basic structure of linked list nodes and how they are connected to form a linked list. The key aspects covered include nodes containing data and a pointer to the next node, singly linked lists only traversing in one direction while doubly linked lists allow bidirectional traversal.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Linked List - Introduction - Traversing

The document discusses different types of linked lists including single, double, and circular linked lists. It describes the basic structure of linked list nodes and how they are connected to form a linked list. The key aspects covered include nodes containing data and a pointer to the next node, singly linked lists only traversing in one direction while doubly linked lists allow bidirectional traversal.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Data Structures and Algorithms

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

2 Dr. Belal Murshed


Data Structures and Algorithms

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

3 Dr. Belal Murshed


Data Structures and Algorithms

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

Array vs Linked List


• Advantages • Advantages
I oMultiple values oDynamic
T oDirect Access oFast Insertion
2 • Disadvantages oFast Deletion
0 oStatic • Disadvantage
oSlow Insertion (shifting) oNo Direct Access
8
oSlow Deletion (shifting) oAccessing is slow

6 Dr. Belal Murshed


Data Structures and Algorithms

Types of Linked List


• There are mainly three types of linked lists:
I 1. Single-linked list
T 2. Double linked list
2 3. Circular linked list
0
8

7 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

8 Dr. Belal Murshed


Data Structures and Algorithms

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

9 Dr. Belal Murshed


Data Structures and Algorithms

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

10 Dr. Belal Murshed


Data Structures and Algorithms

Two types of Linked List


Single Linked List Double Linked List
oOne way change oTwo Way Change
I o𝐷1 𝐿𝑖𝑛𝑘𝑒𝑑 𝑡𝑜 𝐷2 o𝐷1 𝐿𝑖𝑛𝑘𝑒𝑑 𝑡𝑜 𝐷2
T obut 𝐷2 𝑑𝑜𝑛𝑡 𝑙𝑖𝑛𝑘𝑒𝑑𝐷1 o𝐷2 𝑑𝑜𝑛𝑡 𝑙𝑖𝑛𝑘𝑒𝑑𝐷1
2 𝐷1 Address Null

𝐷1 next
0
𝐷2 Address Address

8 𝐷2 next

𝐷3 null Address

𝐷3 next

11 Dr. Belal Murshed


Data Structures and Algorithms

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

12 Dr. Belal Murshed


Data Structures and Algorithms

Declarations to create a node


• First you must declare a data structure that will be
used for the nodes.
• For example, the following struct could be used to
I create a list where each node holds a data:
T struct LLNode
{
2 int data; data next
LLNode *next;
0 };
8 Declaration
• The Second step is to declare a pointer to serve as the list head,
as shown below.
o LLNode *head;
o The variable Head has been declared as pointer to a node. data next
13 o The pointer is pointing to the first node in the linked list. Dr. Belal Murshed
Data Structures and Algorithms

Create a node for the List


The pseudocode is shown below. The C++ code follows.
• Create a node called(Newnode) variable from the node
structure
I
• Allocate memory for the Newnode
T • Store data e in the newnode.
2 • Placing address of the first in address field of newnode
0 • Update head to newnode
struct LLNode LLNode *newnode.
data next
8 { Newnode= new LLNode ()
newnode.data=e
int data;
Newnode.next=null
LLNode *next; Head=newnode
}; data next
14 Dr. Belal Murshed
Data Structures and Algorithms

Node - Structure data next

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


I • It could be 20+ separate fields of information storing name, address,
phone, email, etc.
name id gradePoints next
T
Struct LLNode
2 {
String name;
0 String id;
int gradePoints;
8 LLNode *next;

// more code here


};

15 Dr. Belal Murshed


Data Structures and Algorithms

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

16 Dr. Belal Murshed


Data Structures and Algorithms

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!

17 Dr. Belal Murshed


Data Structures and Algorithms

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.

20 Dr. Belal Murshed


Data Structures and Algorithms

I
T
2 Linked List
0
8
Operations

21 Dr. Belal Murshed


Data Structures and Algorithms

Operations
• Is Empty
I • Traversing
T • Searching
2 • Insertion
0 • Deletion
8 • Merging

22 Dr. Belal Murshed


Data Structures and Algorithms

Linked List – isEmpty( )


• There is no nodes Llnode *head
I in the list head=null

T • Head points to null Algorithm isEmpty()


method
2 null If head == null then
head return true
0 Else
An empty linked list return false
8 ifend
• isFull( ) algend

oNot Needed
23 Dr. Belal Murshed
Data Structures and Algorithms

head head head head


null
10 20 50 null

• Display the list data


• Head points to first node
I Algorithm printAllNodes()
10 20 50

method
T while (head != null)
cout<<head.data << “ ”
2 head = head.next;
whileend
0 Algend

8 • The linked list is no more accessible


• Lost the address of first node

Linked List – Traverse


24 Dr. Belal Murshed
Data Structures and Algorithms
Linked List – Traverse
helpPtr helpPtr helpPtr helpPtr
head
null
10 20 50 null

I • Display the list data


• Head points to first node
T
• Use temporary object “helpPtr”
2 Algorithm printAllNodes()
method
0 Llnode *helpPtr
helpPtr=head
8 while (helpPtr!= null)
cout<<helpPtr.data << “ ”
helpPtr = helpPtr.next;
whileend
Algend
25 Dr. Belal Murshed
Linked List – Traverse
Data Structures and Algorithms

Linked List – Modifying


helpPtr helpPtr helpPtr helpPtr
head
20
10 30 6050 null
I 20 null

• Modify the content of each node


T
• Head points to first node
2 • Use temporary object “helpPtr”
0 Algorithm printAllNodes()
method
8 LLnode *helpPtr
helpPtr = head;
while (helpPtr != null) {
helpPtr.data = helpPtr.data + 10;
helpPtr = helpPtr.next;
26 whileend Dr. Belal Murshed
Algend
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 first element
8 in the list

27 Dr. Belal Murshed


Data Structures and Algorithms
Inserting element at the beginning
Steps to insert element at the beginningdata next
1. Create a node called NewNode
2. Store e in the node (NewNode)
3. Placing address of first in address field of NewNode
I 4. Update head to NewNode

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

29 Dr. Belal Murshed


Data Structures and Algorithms
Inserting element at the last

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

30 Dr. Belal Murshed


Data Structures and Algorithms
Inserting element at the last
Algorithm insert_first_SLL
Input:
e, element to be inserted
Head address of first node
Output:
I Head updated(list updated)
Method data next
T LLnode *NewNod,*helpPtr
NewNode = new LLnode;
2 NewNode-> data = e;
NewNode-> next= null;
0 If head == null then
head = newnode
8 else
helpPtr=head
While(helpPtr->.next != null)
helpPtr=helpPtr-> next
helpPtr-> next= NewNode
31 ifend Dr. Belal Murshed
Algorithmend
Data Structures and Algorithms

• Design an algorithm to delete the first


node of the given singly linked list (SLL)
I Algorithm Delete_first_SLL
data next
T Input:
head, address of first node
Output:
2 head, updated(list updated)
Method
0 If head == null then
print “no delete, list is empty”
8 else
helpPtr=head
head = head > next
Dispose (helpPtr)
ifend
32 Algorithmend Dr. Belal Murshed
Data Structures and Algorithms

I
T • Design an algorithm to delete the last
2 node of the given singly linked list (SLL)
0
8

33 Dr. Belal Murshed


Data Structures and Algorithms

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

35 Dr. Belal Murshed

You might also like