Вы находитесь на странице: 1из 20

CHAPTER 3 LINKED LIST

Linked list is an ordered collection of data in which each element contains the location of the next element [RICH98]. The elements in a linked list are called nodes. A node in a linked list is a structure consisting of 2 fields: Data and Link. There are two types of linked list namely: Singly-Linked list and Doubly-Linked list.

SINGLY-LINKED LIST Singly-linked list is a simple linked list structure in which each node has a pointer that identifies the next element in the list. Singly-linked list contains only one link to a single successor [RICH98]. Link field: contains the address of the next node in the list (successor)

DATA Data field: contains the value of the node or element

LINK

Example: 19H Renbert 05H Romel 15H Lolita TAIL (TOP)

05H

15H

Null

HEAD (BOTTOM)

In the above example, we have illustrated a singly-linked list with three nodes. The Head, which is the first node in the list consist Romel. The Link field of the Head points to the address of the next node. So, the successor of Romel is Renbert and the successor of Renbert is Maryli. Since Maryli" is the last node in the list, called the TAIL, the value of its link field is set to NULL. T his signifies the end of the list. Both arrays and singly-linked list performs the same operations such as: 1. Reading the list from left to right 2. Retrieving the ith node in the list (where i n)

3. 4. 5. 6. 7. 8. 9.

Storing a new value into the ith position (where i n) Inserting a new node at position i (where i n) Deleting the ith element (where i n) Copying a list Sorting the nodes in the list Merging two or more lists Splitting a list into several sublist READING THE SINGLY-LINKED LIST FROM LEFT TO RIGHT

Read_List(Head) { If (Head Null) { Set Node_Count to 1 Set Next_Node to Head -> Node.Link Print Head -> Node.Data While(Next_Node is not NULL) { Increment Node_Count Print Next_Node -> Node.Data Set Next_Node to Next_Node -> Node.Link } Print Total Number of Nodes: Node_Count } else Print The list is Empty } Example:

RETRIEVING THE ith NODE IN A SINGLY-INKED LSIT

Retrieve_Node(Head, Node_Pos) { If (Head NULL) { Set Count to 1 Set Next_Node to Head While(( Count < Node_Pos) and (Next_Node is not NULL) { Increment Count Set Next_Node to Next_Node -> Node.Link } If (Next_Node NULL) then Print Next_Node -> Node.Data Else Print Sorry the Node Position is more than the length of list } else Print The list is Empty } Example:

STORING A NEW VALUE INTO THE ith NODE Store_Value(Head, Node_Pos, New_Value) { If (Head Null) { Set Count to 1 Set Next_Node to Head While(( Count < Node_Pos) and (Next_Node is not NULL) { Increment Count Set Next_Node to Next_Node -> Node.Link } If (Next_Node NULL) then Set Next_Node -> Node.Data to New_Value Else Print Sorry the Node Position is more than the length of list } else Print The list is Empty

Example:

INSERTING A NODE IN A SINGLY-LINKED LIST General procedure for inserting a new node a t a certain position in a singlylinked 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 INSERTING A NODE AT THE HEAD OF A SINGLY-LINKED LIST 1. 2. 3. 4. Example: 07H Armin 03H Shan 19H Renbert TAIL (TOP) Create a new node for the element Set the data field of the new node to the value to be inserted Set the link field of the new node to the value of HEAD. Set the HEAD to the address of the new node

03H

19H

Null

HEAD (BOTTOM)

Suppose we want to insert Isay at the start of the list. Following the procedure set above: 1) Create a new node for the element Isay 08H <Data field> 2) Set the data field to Isay 08H Isay <Link field>

3) Set the link field to the value of Head. Since the address of Head is 07H, it will be assigned to the link field of the new node. 08H Isay 07H Armin HEAD (BOTTOM) 4) Set the HEAD to the address of the new node. 08H Isay 07H Armin 03H Shan 19H 19H Renbert Null TAIL (TOP) 03H Shan 19H 19H Renbert Null TAIL (TOP)

07H

03H

07H

03H

HEAD (BOTTOM)

This will signify that the start of the list is the new created node Isay. Insert_Node_At_Head(Head, New_Value) { Create New Node Set Address of New Node -> Node.Data to New_Value Set Address of New Node -> Node.Link to Head Set Head to Address of New Node }

INSERTING A NODE AT THE TAIL OF A SINGLY-LINKED LIST 1. 2. 3. 4. Example: 15H Lita HEAD 02H Bobby 09H Che TAIL Create a new node for the element Set the data field of the new node to the value to be inserted Set the link field of the current last node to the address of the new node. Set the link field of the new node to NULL

02H

09H

Null

Suppose we want to insert Mel at the end of the list. Following the procedure set above: 1. Create a new node for the element Mel 05H <Data field> 2. Set the data field to Mel 05H Mel 3. Set the link field of the current last node to the address of the new node. 15H Lita 02H Bobby 09H Che TAIL (TOP) <Link field>

02H

09H

05H

HEAD (BOTTOM) 3. Set the link field of the new node to NULL Mel NULL

So, the final list would be: 15H Lita 02H 02H Bobby 09H 09H Che 05H 05H Mel NULL

HEAD (BOTTOM) Insert_Node_At_Tail(Tail, New_Value) { Create New Node Set Address of New Node -> Node.Data to New_Value Set Address of New Node -> Node.Link to NULL Set Tail -> Node.Link to Address of New Node Set Tail to Address of New Node }

TAIL (TOP)

DELETING THE NODE AT THE HEAD OF THE SINGLY-LINKED LIST When we delete a node, we simply remove the link of that node to the rest of the nodes. In this way, we logically deletes the node in the list. To physically delete the node, we must FREE the node every time it is deleted. To delete a node at the head of the list, set the variable HEAD to the address stored in the link field of the node to be deleted. Example. 12H Ros 20H 20H Luisa 31H 31H Robert 16H 16H Mina NULL

HEAD (BOTTOM)

TAIL (TOP)

Since we would like to delete Ros we would assign 20H to be the HEAD. HEAD = HEAD -> Node.Link HEAD = 12H -.> 20H

Therefore the value of the HEAD is 20H and the list would be: 12H Ros 20H 20H Luisa 31H 31H Robert 16H 16H Mina NULL

HEAD

TAIL

As you have noticed, the linked between Ros and Luisa was removed but the node Ros is still there. If we want to physically delete Ros in the list, we must release the node through using the function FREE(). After freeing the node, the list would be: TAIL 20H Luisa 31H 31H Robert 16H 16H Mina NULL

HEAD Delete_Head(Head) { Set Node_Address to HEAD Set HEAD to HEAD -> Node.Link FREE(Node_Address) }

DELETING A NODE AT POSITION i To delete a node at position i (where 1 i n ), we must first determine the preceding node (that is the node i-1) then we set the link field of the preceding node (i-1) to the value in the link field of the node to be deleted.

Delete_At_Position_i(Head,i) { Set i to i - 1 Set Ctr to 1 Set Next_Node to HEAD While (Ctr < i ) { Increment ctr Set Next_Node to Next_Node -> Node.Link } Set Node_Address to Next_Node -> Node.Link Set Next_Node -> Node.Link to Node_Address -> Node.Link FREE(Node_Address) } Example. 12H Ros 20H (1) HEAD Position denoted by i 20H Luisa 31H (2) 31H Robert 16H (3) 16H Mina NULL (4) TAIL

If i=2, then after executing the pseudocode (except FREE(Node_Address), the list would be: 12H Ros 31H 20H Luisa 31H 31H Robert 16H 16H Mina NULL

HEAD

TAIL

As you have noticed, the linked between Ros and Luisa, Luisa and Robert was removed but the node Luisa is still there. If we want to physically delete Luisa in the list, we must release the node through using the function FREE(). After freeing the node, the list would be: TAIL 12H HEAD Ros 31H 31H Robert 16H 16H Mina NULL

So, the node Luisa was removed physically in the memory.

DOUBLY-LINKED LIST Doubly-linked list is a linked list structure in which each node has a pointer to both its successor and predecessor[RICH98]. Right Link field: contains the address of the next node in the list (successor)

LEFT LINK

DATA

RIGHT LINK

Left Link field: contains the address of the previous node in the list (predecessor)

Data field: contains the value of the node or element

Example: 15H Null Lita 09H 09H 15H Che 19H 09H 19H Renbert Null

HEAD

TAIL

In the above example, we have illustrated a doubly-linked list with three nodes. The Head, which is the first node in the list consists Lita. The right link field of the Head points to the address of the next node. So, the successor of Lita is Che. The left link of the Head is set to NULL. This means that the node Lita has no predecessor. The successor of Che is Renbert and the predecessor is Lita. Since Renbert is the last node in the list, called the TAIL, the value of its right link field is set to NULL. This signifies the end of the list. Operations: 1. Reading the list from left to right 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 sublist READING THE LIST FROM LEFT TO RIGHT Read_Left_Right(Head) { If (Head Null) { Set Node_Count to 1 Set Next_Node to Head -> Node.RightLink Print Head -> Node.Data While(Next_Node is not NULL) { Increment Node_Count Print Next_Node -> Node.Data Set Next_Node to Next_Node -> Node.RightLink } Print Total Number of Nodes: Node_Count } else Print The list is Empty }

Example:

READING THE LIST FROM RIGHT TO LEFT Read_Right_Left(Tail) { If (Tail Null) { Set Node_Count to 1 Set Predecessor to Tail -> Node.LeftLink Print Tail -> Node.Data While(Predecessor is not NULL) { Increment Node_Count Print Predecessor -> Node.Data Set Predecessor to Predecessor -> Node.LeftLink } Print Total Number of Nodes: Node_Count } else Print The list is Empty } Example:

RETRIEVING A NODE FROM THE LIST

Retrieve_Node(Head, Node_Pos) { If (Head NULL) { Set Count to 1 Set Next_Node to Head While(( Count < Node_Pos) and (Next_Node is not NULL) { Increment Count Set Next_Node to Next_Node -> Node.RightLink } If (Next_Node NULL) then Print Next_Node -> Node.Data Else Print Sorry the Node Position is more than the length of list } else Print The list is Empty } Example:

INSERTING A NODE IN A DOUBLY-LINKED LIST General procedure for inserting a new node a t a certain position in a singlylinked 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. Determine the position of the node in the list based on its value. 4. Insert the node INSERTING A NODE AT THE HEAD OF A DOUBLY-LINKED LIST 1. Create a new node for the element

2. 3. 4. 5.

Set the data field of the new node to the value to be inserted Set the left link field of the new node to NULL. Set the right link field of the new node to address of HEAD Set the left link field of the current HEAD node in the list to the address of the new node.. 6. Set the HEAD to the address of the new node Example: 15H Null Lita 09H 09H 15H Che 19H 09H 19H Renbert Null

HEAD

TAIL

Suppose we want to insert Romel at the start of the list. Following the procedure set above: 1. Create a new node for the element Romel 05H <Left Link> <Data> <Right Link>

2. Set the data field to Romel 05H Romel 3. Set the left link field of the new node to NULL. 05H NULL

Romel

4 . Set the right link field of the new node to address of the HEAD Since the address of the HEAD is 15H, it will be placed to the right link pointer of the new node. 05H NULL Romel 15H

5. Set the left link field of the current HEAD node in the list to the address of the new node. 15H Address of the new node 09H 05H Lita 09H 15H Che 19H 19H 09H Renbert Null

HEAD 6. Set the HEAD to the address of the new node 05H
NULL Romel 15H

TAIL

09H 15H Che 19H 15H 19H 09H 09H Renbert Null

HEAD

05

Lita

TAIL This will signify that the start of the list is the new created node Romel. Insert_Node_At_Head(Head, New_Value) { Create New Node Set Address of New Node -> Node.Data to New_Value Set Address of New Node -> Node.RightLink to Head Set Head->Node.LeftLink to Address of New Node Set Head to Address of New Node } INSERTING A NODE AT THE TAIL OF A DOUBLY-LINKED LIST 1. 2. 3. 4. Create a new node for the element Set the data field of the new node to the value to be inserted Set the right link field of the new node to NULL. Set the left link field of the new node to address of TAIL

5. Set the right link field of the current TAIL node in the list to the address of the new node. 6. Set the TAIL to the address of the new node Example: 15H Null Lita 09H 09H 15H Che 19H 09H 19H Renbert Null

HEAD Suppose we want to procedure set above: insert Romel at the end of the list.

TAIL Following the

1. Create a new node for the element Romel 05H <Left Link> <Data> <Right Link>

2. Set the data field to Romel 05H Romel 3. Set the right link field of the new node to NULL. 05H Romel NULL

4. Set the right link field of the current TAIL node in the list to the address of the new node. Since the address of the TAIL is 19H, it will be placed to the left link pointer of the new node. 05H 19H Romel NULL

5. Set the right link field of the current TAIL node in the list to the address of the new node.

Address of the new node 15H Null Lita 09H 09H 15H Che 19H 09H 19H Renbert 05H

HEAD 6. Set the HEAD to the address of the new node 15H Null Lita 19H 09H 09H HEAD 15H Che 19H
19H

TAIL

09H

Renbert

05H 05H Romel NULL

TAIL This will signify that the end of the list is the new created node Romel. Insert_Node_At_Tail(Tail, New_Value) { Create New Node Set Address of New Node -> Node.Data to New_Value Set Address of New Node -> Node.RightLink to NULL Set Tail->Node.RightLink to Address of New Node Set Tail to Address of New Node }

DELETING THE NODE FROM A DOUBLY-LINKED LIST The process of deleting a node from a singly-linked list, is the same procedure applied in deleting a node from a doubly-linked list.

DELETING THE NODE AT THE HEAD OF A DOUBLY-LINKED LIST To delete a node at the head of the list, set the variable HEAD to the address stored in the right link field of the node to be deleted. Example: 15H Null Luisa 09H 09H 15H Che 19H 09H 19H Renbert Null

HEAD (BOTTOM)

TAIL (TOP)

1. Set the HEAD to the address referenced by the right link field of the HEAD. Since we would like to delete Luisa we would assign 09H to be the HEAD. HEAD = HEAD -> Node.RightLink HEAD = 15H -.> 09H

2. Set the left link field of the new HEAD node to NULL. The new HEAD is Che. The left link field value of the Che will change from 15H to NULL. Therefore the value of the HEAD is 09H and the list would be: 15H Null Luisa 09H 09H NULL Che 19H 09H 19H Renbert Null

HEAD

TAIL

As you have noticed, the linked between Luisa and Che was removed but the node Luisa is still there. If we want to physically delete Luisa in the list, we must release the node through using the function FREE(). After freeing the node, the list would be: 09H NULL Che 19H 09H 19H Renbert Null

HEAD Delete_Head(Head) { Set Node_Address to HEAD Set HEAD to HEAD -> Node.RightLink Set HEAD->Node.LeftLink to NULL FREE(Node_Address) }

TAIL

DELETING A NODE AT THE TAIL OF A DOUBLY-LINKED LIST To delete a node at the head of the list, set the variable HEAD to the address stored in the right link field of the node to be deleted. Example: 05H Null Sweet 19H 19H 05H Owel 08H 19H 08H Leng Null

HEAD (BOTTOM)

TAIL (TOP)

1. Set the TAIL to the address referenced by the left link field of the TAIL. Since we would like to delete the node Leng we would assign 19H to be the new TAIL.

TAIL = TAIL -> Node.LeftLink TAIL = 08H -.> 19H

2. Set the right link field of the new TAIL node to NULL. The new TAIL is Owel. The right link field value of the Owel will change from 08H to NULL. Therefore the value of the TAIL is 19H and the list would be: 05H Null Sweet 19H 19H HEAD 05H Owel NULL 19H 08H Leng Null

TAIL As you have noticed, the linked between Owel and Leng was removed but the node Leng is still there. If we want to physically delete Leng in the list, we must release the node through using the function FREE(). After freeing the node, the list would be: 05H Null 19H Sweet 19H 05H Owel NULL

HEAD

TAIL

Delete_Tail(Tail) { Set Node_Address to TAIL Set TAIL to TAIL -> Node.LeftLink Set TAIL->Node.RightLink to NULL FREE(Node_Address) }

Вам также может понравиться