You are on page 1of 56

CC103 - Data Structure and Algorithm

Week 4:
Link ed Lists

CC103 - Data Structure and Algorithm


LESSON 4: Linked Lists

OBJECTIVES:
At the end of the topic session the students are
expected to:

1. Define Linked Lists.


2. Explore the insertion and deletion operations on
Linked Lists.
3. Differentiate Singly-Linked List from Doubly-Linked
List.
4. Explain linked lists with header and trailer nodes.

CC103 - Data Structure and Algorithm


3
LESSON 4: Linked Lists

Linked Lists
Only a certain class of list may employ arrays:

• Lists that are fixed in length


Arrays cannot dynamically alter their size once they have been defined

• Lists that do not require a considerable amount of insertion and deletion operations
When the elements are sorted, inserting a new element at a specific point in the array would
cause all succeeding elements to move

Consider : A one dimensional array, size 10.


Insert an element at position 1.
Result:
 ALL nine elements MUST be moved one position higher.

 n - i additional operations are required.

 If the array has 1000 elements, considerable amount of time wasted.

The alternative solution to representing this kind of linear lists is the linked list.

CC103 - Data Structure and Algorithm


4
LESSON 4: Linked Lists

Singly Linked Lists

Each node is divided into two parts: the data field and the pointer field.
The data field is used to contain the value of the element. The pointer field
contains the address of the next node in the list.
 If a node is the last one in the list, the pointer field would contain the value NULL.

 If a node is the "next node" in the list, it is called the SUCCESSOR.

CC103 - Data Structure and Algorithm


5
LESSON 4: Linked Lists

Example

• the node in the list where the value NULL is stored in the pointer field signifies
the end of the list

Singly Linked Lists containing Four Nodes

 c Unlike arrays, the nodes in a linked list are not physically adjacent in memory.

 c There is no formula to determine the location of the next node.

CC103 - Data Structure and Algorithm


6
LESSON 4: Linked Lists

Adding a Node
To add a node to a linked list:

1. Create a new node.

2. Set the data field of the new node to the value to be inserted into the list.

3. Assign the address of the new node to the pointer field of the current last
node in the list.

4. Set the pointer field of the new node to NULL.

CC103 - Data Structure and Algorithm


7
LESSON 4: Linked Lists

Linked Lists Operations


Basic operations performed on Singly-Linked Lists:

1. Finding the length of the list, n.


2. Retrieving the ith node in the list, where i = n.
3. Storing a new value into the ith position, where i= n.
4. Inserting a new node at position i, where i = n.
5. Deleting the ith element, where i = n.
6. Copying a list.
7. Sorting the nodes in the list.
8. Merging two or more lists.
9. Splitting a list into several sublists

CC103 - Data Structure and Algorithm


8
LESSON 4: Linked Lists

Finding the Length of the Lists

CC103 - Data Structure and Algorithm


9
LESSON 4: Linked Lists

Pseudocode Read_Lists
Read_List (Head)
{
If (Head = NULL) then
/* the list is empty
*/
{
Print “The list is empty.”
Exit
}

Set Num_Nodes to 1
Set Next_Node to Head c Node.Pointer /* get the
address
of the next node*/

Print Head c Node.Data

While (Next_Node is not NULL)


{
Increment Num_Nodes
Print Next_Node c Node.Data

/* Print contents of
Data field */
Set Next_Node to Next_Node c Node.Pointer

}
Print “ “
Print “The number of nodes in the list is ” Num_Nodes
}

CC103 - Data Structure and Algorithm


10
LESSON 4: Linked Lists

Simulation of Read_list

CC103 - Data Structure and Algorithm


11
LESSON 4: Linked Lists

Retrieving the ith Element


in the List
This pseudocode accepts the address of the Head node and the number of the
element to be retrieved as input.
Retrieve_Node (Head, Node_Num)
{
If (Head = NULL) then /* the list is empty
*/
{
Print “Error: The list is empty.”
Exit
}

Set I to 1
Set Next_Node to Head
While ((Next_Node is not NULL) AND ( I < Node_Num))
{
Increment I
Set Next_Node to Next_Node c Node.Pointer
}
If (Next_Node = NULL) then
Print “Error: Element number exceeds length of
list”
Else
Print Next_Node c Node.Data
}

CC103 - Data Structure and Algorithm


12
LESSON 4: Linked Lists

Storing a New Value into an Element


Store_Value (Head, Node_Num, New_Value)
{
If (Head = NULL) then /* the list is empty */
{
Print “Error: The list is empty.”
Exit
}

Set I to 1
Set Next_Node to Head

While ((Next_Node is not NULL) AND ( I < Node_Num))


{
Increment I
Set Next_Node to Next_Node c Node.Pointer
}
If (Next_Node = NULL) then
Print “Error: Element number exceeds length of list”
Else
Set Next_Node c Node.Data to New_Value
}
CC103 - Data Structure and Algorithm
13
LESSON 4: Linked Lists

Retrieving the ith Element


in the List
This pseudocode accepts the address of the Head node and the number of the
element to be retrieved as input.
Retrieve_Node (Head, Node_Num)
{
If (Head = NULL) then /* the list is empty */
{
Print “Error: The list is empty.”
Exit
}

Set I to 1
Set Next_Node to Head
While ((Next_Node is not NULL) AND ( I < Node_Num))
{
Increment I
Set Next_Node to Next_Node c Node.Pointer
}
If (Next_Node = NULL) then
Print “Error: Element number exceeds length of
list”
Else
Print Next_Node c Node.Data
}

CC103 - Data Structure and Algorithm


14
LESSON 4: Linked Lists

Inserting a new Node at


Position i
Steps needed to insert a new node in a singly linked list:

1. Create a new node for the element.

2. Set the data field of the new node to the value to be inserted.

3. Insert the node.

Three locations in which a node may be inserted:

1. At the start of the list (i = 1).

2. At the end of the list (i > length of the list).

3. At position i(1< i < length of list).

CC103 - Data Structure and Algorithm


15
LESSON 4: Linked Lists

Inserting a new Node at


Position i
Steps needed to insert a new node in a singly linked list:

1. Create a new node for the element.

2. Set the data field of the new node to the value to be inserted.

3. Insert the node.

Three locations in which a node may be inserted:

1. At the start of the list (i = 1).

2. At the end of the list (i > length of the list).

3. At position i(1< i < length of list).

CC103 - Data Structure and Algorithm


16
LESSON 4: Linked Lists

Inserting at the Start of the List


• Set the pointer field of the new node to the value of HEAD.
• Recall the HEAD points to the current first node in the list.
• Set HEAD to the address of the new node.

Example:
Insert “Voltes V” at the beginning of the list:

• Create a new node for the element.


• Set the data field to “Voltes V”.

• Set the pointer field of the node “Voltes V” to the address contained in the
variable HEAD.

Set Address of New Node c NodePointer to HEAD

CC103 - Data Structure and Algorithm


17
LESSON 4: Linked Lists

Inserting at the Start of the List

• Set the variable HEAD to the address of the newly created “Voltes V” node.
Set HEAD to Address of New Node

The final linked list will be:

CC103 - Data Structure and Algorithm


18
LESSON 4: Linked Lists

Inserting at the End of the List


1. Set the pointer field of the current last node to the address
of the new node.

Note : the pointer field of the current last node contains


NULL.

2. Set the pointer field of the new node to NULL.

CC103 - Data Structure and Algorithm


19
LESSON 4: Linked Lists

Inserting at the End of the List


 Example:
Insert “Voltes V” at the end of the list.

1. Set the pointer field of the node “Voltron” to


the address of the new node.
Set Address of “Voltron” Node. Pointer to Address of New Node

2. Set the pointer field of “Voltes V” to NULL.


Set Address of New Node c Node. Pointer to NULL

The final linked list will be:

CC103 - Data Structure and Algorithm


20
LESSON 4: Linked Lists

Inserting at Position i
(1 < i < Length of List)

A. Locate the node at position i - 1.

B. Set the pointer field of the new node to the


value of the pointer field of node i - 1.

C. Set the pointer field of node i - 1 to the


address of the new node.

CC103 - Data Structure and Algorithm


21
LESSON 4: Linked Lists

Inserting at Position i
Example
Make “Voltes V” the 3rd node in the list.

1. Locate the node at position i - 1.


2. Set the pointer field of “Voltes V” to the contents of
the pointer field of “Mazinger Z”.
Set Address of New Node  Node.Pointer to Address of “Mazinger Z” 
Node.Pointer

3. Set the pointer field of “Mazinger Z” to the address of “Voltes V”.


Set Address of “Mazinger Z” c Node.Pointer to Address of New Node
The final Linked List will be:

CC103 - Data Structure and Algorithm


22
LESSON 4: Linked Lists

Inserting at Position i
 Example: Specify i = 4List has only 3 nodes.

1. Locate the node at position i - 1. Last node at the list


2. Set the pointer field.
Set Address of New Node c Node.Pointer to Address of
“Voltron” Node.Pointer

Set Address of “Voltron”Node.Pointer to Address of New Node

The procedure inserted "Voltes V" at the end of the list.

CC103 - Data Structure and Algorithm


23
LESSON 4: Linked Lists

Pseudocode
Representation

CC103 - Data Structure and Algorithm


24
LESSON 4: Linked Lists

Adding a New Node

1. Create a new node for the element.


2. Set the data field of the new node to the value to be
inserted.
3. Set the pointer field of the new node to the value of NULL.
4. Set the pointer field of the node referenced by TAIL to the
address of the new node.
5. Set TAIL to the address of the new node.

CC103 - Data Structure and Algorithm


25
LESSON 4: Linked Lists

Deleting a Node
Two ways to identify a node to be deleted:

1. By Position - The position of the node from the start of the list is specified.

2. By Value - The contents of the data field of the node is identified.


- Used only if the contents of the nodes are unique.

CC103 - Data Structure and Algorithm


26
LESSON 4: Linked Lists

Deleting a Node by Position


1. Locate the node.

2. Delete the node.

3. Release the node from memory.

Deleting a node:
(based on the node's location in the list)

1. If node i is at the HEAD of the list (i = 1)

2. If node i is not the HEAD of the list (1 < i = n)

3. If node i is at the end of the list (1 = n)

CC103 - Data Structure and Algorithm


27
LESSON 4: Linked Lists

The node i is at the Head of the List (i = 1)


• Set the variable HEAD to the address in the pointer field of the node to be
deleted.

Example: Given

Delete “Daimos” from the list.

Set HEAD to HEADc Node.Pointer

The new list would correspond to:

CC103 - Data Structure and Algorithm


28
LESSON 4: Linked Lists

The node i is not the Head of the List


(1 < i ≤ n)
a. Locate the preceding node (i-1).
b. Set the pointer field of the preceding node (i-1) to
the value in the pointer field of the node to be
deleted.

Delete the node “Mazinger Z” which is at position 2.


1. Locate the node at position 1 (i - 1).
2. Set the pointer field.
Set Address of “Daimos”  Node.Pointer to Address of “Mazinger Z”  Node.Pointer
The new list would contain the nodes:

CC103 - Data Structure and Algorithm


29
LESSON 4: Linked Lists

The node i is at the End of the List

CC103 - Data Structure and Algorithm


30
LESSON 4: Linked Lists

Release the Node from Memory


Ø Deleting a node does not only mean that we should remove it from the list.
Ø Every time a node is created, memory is allocated for the node according to its
size.
Ø When we “unlink” a node from a list, it simply becomes “invisible” to the other
nodes but it is still present in memory.

FREE (ADDR) - Frees the node whose address is ADDR.

CC103 - Data Structure and Algorithm


31
LESSON 4: Linked Lists

Releasing the Node from Memory

CC103 - Data Structure and Algorithm


32
LESSON 4: Linked Lists

Pseudocode Representation
Delete_Node (Head, I,n)
{
If (I > n) then
{
Print “Error: Position to be deleted exceeds length of
list”
Exit
}
If (I = 1) then /* Delete node at start of list */
{
Set Node_Address to Head
Set Head to Head c Node.Pointer
}
Else
{
Decrement I
Set Ctr to 1
Set Next_Node to Head While (Ctr < I)
/* Locate node i - 1 */
{
Increment Ctr
Set Next_Node to Next_Node c
Node.Pointer
}
Set Node_Address to Next_Node c Node.Pointer
Set Next_Node c Node.Pointer to
Node_Address c Node.Pointer
}
FREE (Node_Address)
}
CC103 - Data Structure and Algorithm
33
LESSON 4: Linked Lists

Doubly-Linked Lists
Singly-Linked List
• traversing is only from left to right
• computer time is wasted in traversing the list

Doubly-Linked List
• reading a list from both directions

CC103 - Data Structure and Algorithm


34
LESSON 4: Linked Lists

Example

CC103 - Data Structure and Algorithm


35
LESSON 4: Linked Lists

Doubly-Linked Lists Operations

CC103 - Data Structure and Algorithm


36
LESSON 4: Linked Lists

Finding the Length of the List

CC103 - Data Structure and Algorithm


37
LESSON 4: Linked Lists

Reading the List from Left to Right

CC103 - Data Structure and Algorithm


38
LESSON 4: Linked Lists

Reading the List from Right to Left

CC103 - Data Structure and Algorithm


39
LESSON 4: Linked Lists

Retrieving a Node from the List

CC103 - Data Structure and Algorithm


40
LESSON 4: Linked Lists

Fast Retrieving

CC103 - Data Structure and Algorithm


41
LESSON 4: Linked Lists

Fast Retrieving

CC103 - Data Structure and Algorithm


42
LESSON 4: Linked Lists

Fast Retrieving

CC103 - Data Structure and Algorithm


43
LESSON 4: Linked Lists

Fast Retrieving

CC103 - Data Structure and Algorithm


44
LESSON 4: Linked Lists

Pseudocode Simulation

CC103 - Data Structure and Algorithm


45
LESSON 4: Linked Lists

Inserting a Node into the List

CC103 - Data Structure and Algorithm


46
LESSON 4: Linked Lists

Inserting the Node at the Head of the List


Insert the node “A” into the list

The first two steps will generate the node

A) Set the left pointer field to NULL

B) Set the right pointer field to HEAD.

CC103 - Data Structure and Algorithm


47
LESSON 4: Linked Lists

Inserting the Node at the Head of the List


C) Set the left pointer field of the current HEAD node in the list to the address of
the new node.

D) Set the variable HEAD to the address of the new node.

CC103 - Data Structure and Algorithm


48
LESSON 4: Linked Lists

Pseudocode
Representation

CC103 - Data Structure and Algorithm


49
LESSON 4: Linked Lists

Pseudocode
Representation

CC103 - Data Structure and Algorithm


50
LESSON 4: Linked Lists

Deleting a Node from the List


• Steps in deleting a node from a doubly-linked list

CC103 - Data Structure and Algorithm


51
LESSON 4: Linked Lists

Deleting the Node at the Head of the List

 Delete node “B” from the list

a) Set the variable HEAD to the address of the second


node in the list, node “D”.

b) Set the left pointer field of the new HEAD


node, node
“D”, to NULL.

CC103 - Data Structure and Algorithm


52
LESSON 4: Linked Lists

Pseudocode
Representation

CC103 - Data Structure and Algorithm


53
LESSON 4: Linked Lists

Pseudocode
Representation

CC103 - Data Structure and Algorithm


54
LESSON 4: Linked Lists

Exercise
Write an algorithm that would perform the following:
1. Accept a series of 12 strings as input.
2. Create a doubly-linked list based from the input.
3. The nodes in the linked list must be sorted in
descending order.
Input:

(“Garnet”, “Amethyst”, “Aquamarine”, “Diamond”,


“Emerald”, “Pearl”, “Ruby”, “Peridot”, “Sapphire”, “Opal”, “Topaz”, “Turquoise”)
Output:

CC103 - Data Structure and Algorithm


55
LESSON 1: Orientation and Course Introduction

THE END

CC103 - Data Structure and Algorithm

You might also like