Professional Documents
Culture Documents
Week 4:
Link ed Lists
OBJECTIVES:
At the end of the topic session the students are
expected to:
Linked Lists
Only a certain class of list may employ arrays:
• 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
The alternative solution to representing this kind of linear lists is the linked list.
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.
Example
• the node in the list where the value NULL is stored in the pointer field signifies
the end of the list
c Unlike arrays, the nodes in a linked list are not physically adjacent in memory.
Adding a Node
To add a node to a linked list:
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.
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 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
}
Simulation of Read_list
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
}
Set I to 1
Set Next_Node to Head
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
}
2. Set the data field of the new node to the value to be inserted.
2. Set the data field of the new node to the value to be inserted.
Example:
Insert “Voltes V” at the beginning of the list:
• Set the pointer field of the node “Voltes V” to the address contained in the
variable HEAD.
• Set the variable HEAD to the address of the newly created “Voltes V” node.
Set HEAD to Address of New Node
Inserting at Position i
(1 < i < Length of List)
Inserting at Position i
Example
Make “Voltes V” the 3rd node in the list.
Inserting at Position i
Example: Specify i = 4List has only 3 nodes.
Pseudocode
Representation
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.
Deleting a node:
(based on the node's location in the list)
Example: Given
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
Example
Fast Retrieving
Fast Retrieving
Fast Retrieving
Fast Retrieving
Pseudocode Simulation
Pseudocode
Representation
Pseudocode
Representation
Pseudocode
Representation
Pseudocode
Representation
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:
THE END