linked list

97
MIT – 594 Data Structures and Algorithms Masters of Information Technology Ms. Marife S. Edu

Upload: milton

Post on 07-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Linked List. MIT – 594 Data Structures and Algorithms Masters of Information Technology Ms. Marife S. Edu. In Computer Science. Consists of a sequence of nodes each containing arbitrary data fields and one or two references (“links”) pointing to the next and/or previous nodes. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Linked List

MIT – 594 Data Structures and AlgorithmsMasters of Information Technology

Ms. Marife S. Edu

Page 2: Linked List

Consists of a sequence of nodes each containing arbitrary data fields and one or two references (“links”) pointing to the next and/or previous nodes.

Page 3: Linked List

The order of the linked items may be different from the order that the data items are stored in memory or on disk, allowing the list of items to be traversed in a different order.

Page 4: Linked List

Self-referential data type because it contains a pointer or link to another datum of the same type.

Permits insertion and removal of nodes at any point in the list in constant time, but do not allow random access.

Types: Singly-linked lists, Doubly-linked lists, and Circularly-linked lists

Page 5: Linked List

Linked list can be implemented in most languages.

Languages such as Lisp and Scheme have the data structure built in, along with operations access the linked list.

Procedural or object oriented languages such as C, C++, and Java typically rely on mutable references to create linked list

Page 6: Linked List

Developed in 1955-56 by Allen Newell, Cliff Shaw and Herbert Simon at RAND Corporation as the primary data structure for their Information Processing Language (IPL).

IPL was used by the authors to develop several early artificial intelligence programs, including the Logic Theory Machine, the General Problem Solver, and a computer chess program.

Page 7: Linked List

Reports on their work appeared in IRE Transactions on Information Theory in 1956, and several conference proceedings from 1957-1959, including Proceedings of the Western Joint Computer Conference in 1957 and 1958, and Information Processing (Proceedings of the first UNESCO International Conference on Information Processing) in 1959.

Page 8: Linked List

The now-diagram consisting of blocks representing list nodes with arrows pointing to successive list nodes appear in “Programming the Logic Theory Machine” by Newell and Shaw in Proc. WJCC, February 1957.

Page 9: Linked List

The problem of machine translation for natural language processing led Victor Yngve at Massachussets Institute of Technology (MIT) to use linked list as data structures in his COMIT programming language for computer research in the field of linguistics.

Page 10: Linked List

LISP, standing for list processor, was created by John McCarthy in 1958 while he was at MIT and in 1960 he published its design in a paper in the Communications of the ACM, entitled “Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part 1”. One of LISP’s major data structures is the Linked List.

Page 11: Linked List

Singly – Linked Lists Doubly – Linked Lists

Page 12: Linked List

or slist for short The simplest kind of linked list Has one line per node This link points to the next node in the list, or to

a null value or empty list if it is the final node. Containing two values: the value of the current

node and a link to the next node The first part holds or points to information

about the node, and the second part holds the address of the next node.

Travels one way

Page 13: Linked List

Data Pointer

Figure 1: Node

Data field Pointer field

First node in the list

Last node in the list

Next node in the list

Page 14: Linked List

Goldilocks

05H

Figure 2: Singly Linked List with 4 Nodes

HEAD

01H 05H 03H

Papa Bear

03HMama Bear

20HBaby Bear

Null

20H

Page 15: Linked List

There is no limit to the size of a singly-linked list. Adding a nodes to a linked list is simply a matter of:

1.Creating a new node2.Setting the data field of the new node to the

value to be inserted into the list.3.Assigning the address of the new node to the

pointer field of the current last node in the list.4.Setting the pointer field of the new node to NULL.

Page 16: Linked List

The only way to determine how many nodes are contained in the list is to traverse it.

This means that we will have to “read” the list.

It is only possible to read a singly-linked list from left to right die to the order of the addresses stored in the pointer field.

Page 17: Linked List

The following psuedocode accepts as input a variable named Head which contains the address of the first node in the list.

Pointer_Value Node.Data - Reference the Data field of the node whose address is Pointer_Value

Pointer_Value Node.Pointer - Reference the Pointer field of the node whose address is Pointer_Value

Page 18: Linked List

Read_List(Head){

If(Head = NULL) then{

Print “The list is empty”Exit

}

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

Print Head Node.Data /* Print contents of Data Field*/

While(Next_Node is not NULL){Increment Num_NodesPrint Next_Node Node.DataSet Next_Node to Next_Node Node.Pointer }Print “”Print “The number of nodes in the list is” Num_Nodes

}

Page 19: Linked List

Philip 09H

To simulate the Read_List pseudocode.HEAD

05H

James of Zebedee

02H John 04H Peter 06H

09H 02H 04H

Bartholomew 21H

06H

Matthew 31H

21H

James 27H Thaddeus 15H

Simon 18H

31H 27H

15H 18H

Andrew 22H Thomas 25H

22H 25H

JudasNULL

Figure 3. Singly-linked list with 12 nodes

Page 20: Linked List

Num_Nodes = 1Next_Node = address of “James of Zebedee” (09H)Print “Philip”While Loop: 1st IterationNum_Nodes = 2Print “James of Zebedee”Next_Node = address of “John” (02H)While Loop: 2nd IterationNum_Nodes = 3Print “John”Next_Node = address of “Peter” (04H)While Loop: 3rd IterationNum_Nodes = 4Print “Peter”Next_Node = address of “Bartholomew” (06H)While Loop: 4th IterationNum_Nodes = 5Print “Bartholomew”Next_Node = address of “Matthew” (21H)

Page 21: Linked List

While Loop: 5th IterationNum_Nodes = 6Print “Matthew”Next_Node = address of “James” (31H)While Loop: 6th IterationNum_Nodes = 7Print “James”Next_Node = address of “Thaddeus” (27H)While Loop: 7th IterationNum_Nodes = 8Print “Thaddeus”Next_Node = address of “Simon” (15H)While Loop: 8th IterationNum_Nodes = 9Print “Simon”Next_Node = address of “Andrew” (18H)

Page 22: Linked List

While Loop: 9th IterationNum_Nodes = 10Print “Andrew”Next_Node = address of “Thomas” (22H)While Loop: 10th IterationNum_Nodes = 11Print “Thomas”Next_Node = address of “Judas” (25H)While Loop: 11th IterationNum_Nodes = 12Print “Judas”Next_Node = NULL

Print “”Print “The number of nodes in the list is 12”

Page 23: Linked List

SUMMARY of OUTPUT

PhilipJames of ZebedeeJohnPeterBartholomewMatthewJamesThaddeusSimonAndrewThomasJudas

The number of nodes in the list is 12

Page 24: Linked List

To retrieve a specific element in a singly linked list, we would have to traverse the list until we reach desired node.

Page 25: Linked List

Retrieve_Node(Head, Node_Num){

If (Head = NULL) then /* the list is empty */

{

Print “Error: The list is empty”Exit

}

Set I to 1Set Next_Node to HeadWhile ((Next_Node is not NULL) AND (I < Node_Num)){

Increment ISet Next_Node to Next_Node Node.Pointer

}

Page 26: Linked List

If (Next_Node = NULL) then{

Print “Error: Element number exceeds length of list”

}

Else{

Print Next_Node Node.Data

}

}

Figure 4. Algorithm for Retrieving the ith Element In A Singly Linked List

Page 27: Linked List

To simulate the Retrieve_Node algorithm, let us try to retrieve the 3rd node in the list presented in Figure 3.

I = 1Next_Node = address of “Philip” (05H)

While Loop: 1st IterationI = 2Next_Node = address of “James of Zebedee” (09H)

While Loop: 2nd IterationI = 3Next_Node = address of “John” (02H)

/* Else Clause: Next_Node is NOT NULL */Print “John”

Page 28: Linked List

As with the previous algorithm, in order to store a new value into a specific element in a singly-linked list, we would have to traverse the list until we reach desired node.

The procedure accepts as input the address of the Head node – Head, the number of the node to be modified – Node_Num, and the new value to be stored into the node – New Value.

Page 29: Linked List

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 1Set Next_Node to HeadWhile ((Next_Node is not NULL) AND (I < Node_Num)){

Increment ISet Next_Node to Next_Node Node.Pointer

}

Page 30: Linked List

If (Next_Node = NULL) then{

Print “Error: Element number exceeds length of list”

}

Else{

Set Next_Node Node.Data to New_Value

}

}

Figure 5. Algorithm for Storing A New Value Into the ith Node In A Singly Linked List

Page 31: Linked List

To simulate the algorithm we will once again use the list in Figure 3. Let us assume that the data field of the 4th node will be modified from “Peter” to “The Rock”

I = 1Next_Node = address of “Philip” (05H)While Loop: 1st IterationI = 2Next_Node = address of “James of Zebedee” (09H)While Loop: 2nd IterationI = 3Next_Node = address of “John” (02H)While Loop: 3rd IterationI = 4Next_Node = address of “Peter” (04H)/*Else Clause: Next Node is NOT NULL */

Data Field of node 4 = “The Rock”

Page 32: Linked List

Philip 09H

HEAD

05H

James of Zebedee

02H John 04H The Rock 06H

09H 02H 04H

Bartholomew 21H

06H

Matthew 31H

21H

James 27H Thaddeus 15H

Simon 18H

31H 27H

15H 18H

Andrew 22H Thomas 25H

22H 25H

JudasNULL

Figure 6. Singly-linked After Replacing “Peter” with “The Rock”

Page 33: 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. Insert the node at the start of the list (i = 1)2. Insert the node at the end of the list (i >

length of the list)3. Insert the node at position i of the list (i <

length of the list)

Page 34: Linked List

Set the pointer field of the new node to the value of HEAD

Set HEAD to the address of the new node.

Suppose that we would like to insert “Voltes V” at the beginning of the following list

Daimos 10HHEAD

20H

Mazinger Z 05H VoltronNULL

10H 05H

Figure 7. Singly-linked With 3 nodes

Page 35: Linked List

Voltes V

30H

Figure 8. New Created Node “Voltes V”

In the next step, we set the pointer field of the node “Voltes V” to the address contained in the variable HEAD. This will effectively link “Voltes V” to “Daimos”.

Set Address of New Node Node.Pointer to Head

Daimos 10H

HEAD

20H

Mazinger Z 05H VoltronNULL

10H 05H

Voltes V 20H

30H

Figure 9. Singly-Linked list after inserting “Voltes V”

Page 36: Linked List

Daimos 10H

HEAD

20H

Mazinger Z 05H VoltronNULL

10H 05H

Voltes V 20H

30H

Figure 10. Singly-Linked list after Reassigning HEAD

Set HEAD to Address of New Node

The Final linked list will look like the diagram below

Page 37: Linked List

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

Set the pointer field of the new node to NULL.

For example, let us suppose that we would like to insert “Voltes V” at the end of the list.

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

Set the pointer field of “Voltes V” to NULL. This will signal that “Voltes V is the new last node in list”

Set Address of New Node Node.Pointer to NULL

Page 38: Linked List

Daimos 10H

HEAD

20H

Mazinger Z 05H Voltron 30H

10H 05H

Voltes VNULL

30H

Figure 11. Singly-Linked list after Inserting “Voltes V” At The End

The Resulting linked list

Page 39: Linked List

Locate the node at position i –1 Set the pointer field of the new node to the

value of the pointer field of node i –1 Set the pointer field of node i –1 to the

address of the new node.

Suppose that we would like “Voltes V” to be the 3rd node in our list. Once again perform the first two steps of our general procedure and create the new node. The next step requires us to locate the node at position i –1.

Page 40: Linked List

Since i = 3, this means that we are interested in node 2 which is “Mazinger Z”. We will then 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

Daimos 10H

HEAD

20H

Mazinger Z 05H VoltronNULL

10H 05H

Voltes V 05H

30H

Figure 12. Singly-Linked list after Setting the Pointer Field of “Voltes V”

Page 41: Linked List

As can be seen in the figure, the last step causes both “Voltes V” and “Mazinger Z” to point to “Voltron”. In the next step, we set the pointer field of “Mazinger Z” to the address of “Voltes V”. Set Address of “Mazinger Z” Node.Pointer to Address of sNew Node

Daimos 10H

HEAD

20H

Mazinger Z 30H VoltronNULL

10H 05H

Voltes V 05H

30H

Figure 13. Singly-Linked list after Inserting “Voltes V” At Position 3

Page 42: Linked List

Head – The address of the first node in the list

I – The position where the new node will be inserted

New_Value – the new value to be inserted into the list

Page 43: Linked List

Insert_Node(Head, I, New_Value){

Create New NodeSet Address of New_Node Node.Data to New_Value

If (I = 1) then /* Insert New Node at start of list */{

Set Address of New Node Node.Pointer to Head

Set Head to Address of New Node

Exit

}

Decrement I

Set Ctr to 1

Set Next Node to Head

Page 44: Linked List

While ((Ctr < 1) AND (Next_Node is not NULL)) /* Locate i –1 */

{

Increment CtrSet Last_Node to Next_Node /* Store address of the previous node */

Set Next_Node to Next_Node Node.Pointer}

If (Next_Node is NULL) then /* Insert New Node at end of list */{

Set Last_Node Node.Pointer to Address of New Node

Set Address of New_Node Node.Pointer to NULL

}

Else /* Node i –1 has been found */

{

Set Address of New Node Node.Pointer to Next_Node Node.Pointer

Set Next_Node Node.Pointer to Address of New Node

}

}

Page 45: Linked List

1. Insert the node “Voltes V” at the start of the listInput the Insert_Node: (Address of “Daimos”, 1, “Voltes V”)

Create node for “Voltes V”

Address of “Voltes V” Node.Data = “Voltes V”

Voltes V

30H

Result of Statements

/* First IF Statement */

Address of “Voltes V” Node.Pointer = Address of “Daimos”

Head = Address of “Voltes V”

Page 46: Linked List

2. Insert the node “Voltes V” at the end of the listInput the Insert_Node: (Address of “Daimos”, 9999, “Voltes V”)

Create node for “Voltes V”

Address of “Voltes V” Node.Data = “Voltes V”

Voltes V

30H

Result of Statements

I = 9998

Ctr = 1

Next_Node = Address of “Daimos”

While Loop: 1st Iteration

Ctr = 2

Last _Node = Address of “Daimos”

Next_Node = Address of “Mazinger Z”

Page 47: Linked List

While Loop: 2nd Iteration

Ctr = 3

Last _Node = Address of “Mazinger Z”

Next_Node = Address of “Voltron”

While Loop: 3rd Iteration

Ctr = 4

Last _Node = Address of “Voltron”

Next_Node = NULL

/* Second IF Statement */

Address of “Voltron” Node.Pointer = Address of “Voltes V”

Address of “Voltes V” Node.Pointer = NULL

Daimos 10H

HEAD

20H

Mazinger Z 05H Voltron 30H

10H 05H

Voltes VNULL

30H

Result of Statements

Page 48: Linked List

There are some applications that frequently insert nodes at the end if the linked list. To eliminate the amount of time wasted traversing the list each time a new node is added, a variable can be created to hold the address of the current last node. Let us call this variable TAIL.

Page 49: Linked List

Create a new node for the element Set the data field of the new node to the

value to be inserted Set the pointer field of the new node to the

value of NULL Set the pointer field of the node referenced

by TAIL to the address of the new node Set TAIL to the address of the new node

Page 50: Linked List

Create a new node Set the data field of the new node to the

value to be insertedSet Address of New Node Node.Data to “Voltes V”

Set the pointer field of the new node to the value of NULL

Set Address of New Node Node.Pointer to NULL

Set the pointer field of the node referenced by TAIL to the address of the new node

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

Page 51: Linked List

Daimos 10H

HEAD

20H

Mazinger Z 05H Voltron 30H

10H 05H

Voltes VNULL

30H

Figure 14. Singly-Linked list after setting The Pointer Field of “Voltron”

Set TAIL to the address of the new nodeSet TAIL to the Address of New Node

Daimos 10H

HEAD

20H

Mazinger Z 05H Voltron 30H

10H 05H

Voltes VNULL

30H

TAIL

Figure 15. Singly-Linked list after setting The TAIL

Page 52: Linked List

Insert_End(Tail, New_Value){

Create New NodeSet Address of New Node Node.Data to New_Value

Set Address of New Node Node.Pointer to NULL

Set TAIL Node.Pointer to Address of New Node

Set TAIL to Address of New Node

}

The following is the corresponding pseudocode that inserts a node at the end of a singly-linked list with the use of the variable TAIL. It accepts as input the address of the last node in the list – Tail, and the value to be inserted into the list – New_Value.

Figure 16. Algorithm for Inserting A Node At the End of A Singly-Linked List Using TAIL

Page 53: Linked List

Input to Insert_End(Address of “Voltron”, “Voltes V”)

Create a node for “Voltes V” Address of “Voltes V” Node.Data = “Voltes V” Address of “Voltes V” Node.Pointer = NULL

Voltes V

30H

Result of Statements

Page 54: Linked List

Address of “Voltron” Node.Pointer = Address of “Voltes V”

Result of Statements

Daimos 10H

HEAD

20H

Mazinger Z 05H Voltron 30H

10H 05H

Voltes VNULL

30H

TAIL

Tail = Address of “Voltes V”

Result of Statements

Daimos 10H

HEAD

20H

Mazinger Z 05H Voltron 30H

10H 05H

Voltes VNULL

30H

TAIL

Page 55: Linked List

By Position – The position of the node with respect to the start of the list is specified

By Value – The contents of the data field of the node is identified. This method may only be used if the nodes in the list are unique.

Assumptions: 1. The singly-linked list has a length of n nodes2. The node to be deleted is at position i, where 1 < i < n

Page 56: Linked List

Locate the node i Delete the node

This step may be further subdivided into two separate classes

a. if the node i is at the HEAD of the list (i = 1)b. if the node i is not at the HEAD of the list ( 1 < i <

n)

Release the node from memory

Page 57: Linked List

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

Set HEAD to HEAD Node.Pointer

HEAD

Mazinger Z 05H VoltronNULL

10H 05H

Figure 17. Singly-Linked List After Deleting “Daimos”

Page 58: Linked List

Locate the preceding node (i -1) Set the pointer field of the preceding node (i

-1) to the value in the pointer field of the node to be deleted.

Daimos 10H

HEAD

20H

Mazinger Z 05H VoltronNULL

10H 05H

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

Page 59: Linked List

Set Address of “Daimos” Node.Pointer to Address of “Mazinger Z” Node.Pointer

Daimos 05H

HEAD

20H

VoltronNULL

05H

Releasing 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. To avoid wasting unnecessary storage, we must FREE a node every time it is deleted.

FREE (ADDR)

Page 60: Linked List

In spite of the benefit in memory requirements when using singly-linked list, we notice one main disadvantage: It is only possible to traverse the list from left to right.

In some applications, this type of processing is sufficient

However, there are applications that require the capability of reading a linked list in both directions.

An example of this is an application that performs a number of deletions.

Page 61: Linked List

DataRight

PointerLeft Pointer

Figure 18. Format Of A Doubly-Linked List Node

HEAD

15H

Snoopy 01HNULL Woodstock 25H15H

01H

Charlie Brown

NULL01H

25H

Figure 19. Doubly-Linked List With 3 Node

TAIL

Page 62: Linked List

From Left to Right (Head to Tail) From Right to Left (Tail to Head)

Pointer_Value Node.Data – References the Data field of the node whose address is Pointer_Value

Pointer_Value Node.Left_Pointer – References the Left Pointer field of the node whose address is Pointer_Value

Pointer_Value Node. Right_Pointer – References the Right Pointer field of the node whose address is Pointer_Value

Page 63: Linked List

Read_Dbl_List_LR(Head){

If(Head = NULL){

Print “The List is Empty” Exit

}

Set Num_Nodes to 1

Set Next_Node to Head Node.Right_Pointer /* Get the address of the next node */

Print Head Node.Data /* Print contents of Data Field */

While(Next_Node is not NULL){

Increment Num_NodesPrint Next_Node Node.Data

Set Next_Node to Next_Node Node.Right_Pointer }

Print “” Print “The Number of nodes in the list is” Num_Nodes

}

Page 64: Linked List

HEAD

15H

Archie Andrews

01HNULLBetty

Cooper25H15H

01H

Veronica Lodge

NULL01H

25H

Figure 20. Doubly-Linked List With 3 Nodes

TAIL

Page 65: Linked List

Num_Nodes = 1

Next_Node = address of “Betty Cooper” (01H)

Print “Archie Andrews”

While Loop: 1st Iteration

Num_Nodes = 2

Print “Betty Cooper”

Next_Node = address of “Veronica Lodge” (25H)

While Loop: 2nd Iteration

Num_Nodes = 3

Print “Veronica Lodge”

Next_Node = NULL

Print “”

Print “The number of nodes in the list is 3”

Summary of Output

Archie Andrews

Betty Cooper

Veronica Lodge

The number of nodes in the list is 3

Page 66: Linked List

Read_Dbl_List_RL(Tail){

If(Tail = NULL){

Print “The List is Empty” Exit

}

Set Num_Nodes to 1Set Next_Node to Tail Node.Left_Pointer /* Get the address of the next node */

Print Tail Node.Data /* Print contents of Data Field */

While(Prev_Node is not NULL){

Increment Num_NodesPrint Prev_Node Node.DataSet Prev_Node to Next_Node Node.Left_Pointer

}

Print “” Print “The Number of nodes in the list is” Num_Nodes

}

Page 67: Linked List

Num_Nodes = 1

Prev_Node = address of “Betty Cooper” (01H)

Print “Veronica Lodge”

While Loop: 1st Iteration

Num_Nodes = 2

Print “Betty Cooper”

Prev_Node = address of “Archie Andrews” (15H)

While Loop: 2nd Iteration

Num_Nodes = 3

Print “Archie Andrews”

Prev_Node = NULL

Print “”

Print “The number of nodes in the list is 3”

Summary of Output

Veronica Lodge

Betty Cooper

Archie Andrews

The number of nodes in the list is 3

Page 68: Linked List

Head – The address of the first node in the list Tail – The address of the last node in the list Node_Pos – The position of the node in the list Num_Nodes – The number of nodes in the list

Page 69: Linked List

Fast_Retrieve(Head, Tail, Node_Pos, Num_Nodes){

If ((Num_Nodes = 0) OR (Tail = NULL) OR (Head = NULL)) then {

Display_Error_Message (1) Exit

}

If ((Node_Pos = 0) OR (Node_Pos > Num_Nodes)) then {

Display_Error_Message (2) Exit

}/* Get the position of the middle node */

If (Num_Nodes is Odd) then {

Set Mid_Pos to ((Num_Nodes + 1) / 2 )}

Else {

Set Mid_Pos to (Num_Nodes / 2 )}

Page 70: Linked List

/* Verify if the node to be retrieved is at the latter part of the list */

If (Node_Pos > Mid_Pos) then {

Traverse_From_Tail () }

Else {

Traverse_From_Head () }

}

Traverse_From_Head () {

Set I to 1

Set Next_Node to Head

While ((Next_Node is not NULL) AND (I < Node_Pos){

Increment I

Set Next_Node to Next_Node Node.Right_Pointer }

Page 71: Linked List

If (Next_Node = NULL ) then {

Display_Error_Message (3) }

Else {

Print Next_Node Node.Data}

}

Traverse_From_Tail () {

Set I to Num_Nodes

Set Prev_Node to Tail

While ((Prev_Node is not NULL) AND (I > Node_Pos){

Decrement I

Set Prev_Node to Prev_Node Node.Left_Pointer }

Page 72: Linked List

If (Prev_Node = NULL ) then {

Display_Error_Message (3) }

Else {

Print Prev_Node Node.Data}

}

Display_Error_Message(Message_Type) /* Prints a message on the screen based on the Message_Type */

{

Switch (Message_Type)

{

Case (1)

{

Print “Error: List is Empty”

Page 73: Linked List

Case (2)

{

Print “Error: Position of node to be retrieved should be Greater than 0 and must not be greater than the number of nodes in the list”

}

Default{

Print “Error: Position of node to be retrieved exceeds ACTUAL length of List ”

Print “The number of nodes specified is greater than ACTUAL number of nodes in list”

}

}

}

Page 74: Linked List

HEAD72H

A Partridge in a Pear Tree

NULL

23H Two Turtle Doves72H 40H

Three French Hens23H 01H

Five Gold Rings01H 46H

Seven Swans A Swimming

46H 35H

Nine Ladies Dancing35H 55H

Eleven Pipers Piping55H 13H

Four Calling Birds 40H 19H

Six Geese A Laying19H 08H

Eight Maids A Milking

08H 28H

Ten Lords A Leaping28H 90H

Twelve Drummers Drumming

90HNULL

TAIL

23H

40H 01H

19H 46H

08H 35H

28H 55H

90H 13H

Figure 21. Doubly-Linked List With 12 Nodes

Page 75: Linked List

By applying the algorithm Fast_Retrieve, let us try to see what “ My True Love ” sent on the third day of Christmas. Parameters passed to Fast_Retrieve(72H, 13H, 3, 12)

Mid_Pos = 6

/* Traverse_From_Head */I = 1

Next_Node = Head (72H)

While Loop: 1st Iteration

I = 2

Next_Node = Address of Node 2 (23H)

While Loop: 2nd Iteration

I = 3

Next_Node = Address of Node 3 (40H)

Print “Three French Hens ”

Page 76: Linked List

Create a new node for the element Set the data field for the new node value to

be inserted Determine the position of the node in the

list based on its value Insert

Page 77: Linked List

Insert_Node_Dbl (Head, Tail, New_Value) {

If (Head = NULL ) then

{

Print “Error: List is Empty”

Exit

}

Create New Node

Set Address of New Node Node.Data to New_Value

If (Head Node.Data > New_Value) then /* Insert node at head of list */

{

Insert_Dbl_Head()}

Else{

If (Tail Node.Data > New_Value) then {

Insert_Dbl_Tail()}

Page 78: Linked List

Else

{

Insert_Dbl_Within()}

}

}

Insert_Dbl_Head () {

Set Address of New Node Node.Left_Pointer to NULL

Set Address of New Node Node.Right_Pointer to Head

Set Head Node.Left_Pointer to Address of New Node

Set Head to Address of New Node}

Insert_Dbl_Tail () {

Set Address of New Node Node.Right_Pointer to NULL

Set Address of New Node Node.Left_Pointer to Tail

Set Tail Node.Right_Pointer to Address of New Node

Set Tail to Address of New Node

}

Page 79: Linked List

Insert_Dbl_Within() {

Set Displaced_Node to Head Node.Right_Pointer /* Determine Position of Node */

While(Displaced_Node Node.Data < New_Value ) {

Set Displaced_Node to Displaced_Node Node.Right_Pointer

}

Set Address of New Node Node.Left_Pointer to Displaced_Node Node.Left_Pointer

Set Address of New Node Node.Right_Pointer to Displaced_Node

Set (Displaced_Node Node.Left_Pointer) Node.Right_Pointer to Address of New Node

Set Displaced_Node Node.Left_Pointer to Address of New Node

}

Page 80: Linked List

HEAD

20H

Batman 80HNULL Spiderman 04H20H Superman NULL80H

80H 04H

1. Let us insert “Aquaman” into the list.

Parameters passed to Insert_Node_Dbl (20H, 04H, “Aquaman”)

Create new node

Address of New Node (33H) Node.Data = “Aquaman”

Aquaman

33H

Page 81: Linked List

HEAD

20H

Batman 80HNULL Spiderman 04H20H Superman NULL80H

80H 04H

/* First IF Statement Insert Node at Head of List */

/* Insert_Dbl_Head */

Address of New Node (33H) Node.Left_Pointer = NULL

Address of New Node (33H) Node.Right_Pointer = Head (20H)

Aquaman 20HNULL

33H

TAIL

Page 82: Linked List

HEAD

20H

Batman 80H33H

Superman NULL80HSpiderman 04H20H

80H 04H

Head (20H) Node.Left_Pointer = Address of New Node (33H)

Aquaman 20HNULL

33HTAIL

Head = Address of New Node (33H)

20H

Batman 80H33H

Superman NULL80HSpiderman 04H20H

80H 04H

Aquaman 20HNULL

33HTAIL

HEAD Final Result

Page 83: Linked List

HEAD

20H

Batman 80HNULL Spiderman 04H20H Superman NULL80H

80H 04H

2. Let us insert “Wonder Woman” into the list.

Parameters passed to Insert_Node_Dbl (20H, 04H, “Wonder Woman”)

Create new node

Address of New Node (33H) Node.Data = “Wonder Woman”

Wonder Woman

33H

Page 84: Linked List

HEAD

20H

Batman 80HNULL Spiderman 04H20H Superman NULL80H

80H 04H

/* Second IF Statement Insert Node at End of List */

/* Insert_Dbl_Tail */

Address of New Node (33H) Node.Right_Pointer = NULL

Address of New Node (33H) Node.Left_Pointer = Tail (04H)

Wonder Woman

NULL04H

33H

TAIL

Page 85: Linked List

HEAD

20H

Batman 80HNULL Superman 33H80HSpiderman 04H20H

80H 04H

Tail (04H) Node.Right_Pointer = Address of New Node (33H)

Wonder Woman

NULL04H

33H

TAIL

Tail = Address of New Node (33H)

Final Result

20H

Batman 80HNULL Superman 33H80HSpiderman 04H20H

80H 04H

Wonder Woman

NULL04H

33H

TAIL

HEAD

Page 86: Linked List

HEAD

20H

Batman 80HNULL Spiderman 04H20H Superman NULL80H

80H 04H

3. Let us insert “Flash” into the list.

Parameters passed to Insert_Node_Dbl (20H, 04H, “Flash”)

Create new node

Address of New Node (33H) Node.Data = “Flash”

Flash

33H

Page 87: Linked List

HEAD

20H

Batman 80HNULL Spiderman 04H20H Superman NULL80H

80H 04H

/* Last IF Statement Insert Node Within the List */

/* Insert_Dbl_Within */

Displaced Node = Address of “Spiderman” (80H)

Address of New Node (33H) Node.Left_Pointer = Address of “Batman” (20H)

Address of New Node (33H) Node.Right_Pointer = Address of “Spiderman” (80H)

Flash 80H20H

33H

TAIL

Page 88: Linked List

HEAD

20H

Batman 33HNULL Spiderman 04H20H Superman NULL80H

80H 04H

Address of “Batman” (20H) Node.Right_Pointer = Address of New Node (33H)

Flash 80H20H

33H

TAIL

Page 89: Linked List

HEAD

20H

Batman 33HNULL

Spiderman 04H33H Superman NULL80H

80H 04H

Address of “Spiderman” (80H) Node.Left_Pointer = Address of New Node (33H)

Flash 80H20H

33H

TAIL

Final Result

Page 90: Linked List

Locate the node1. At the head of the list2. Within the list

Delete the node Release the node from the memory

HEAD

02H

B 08HNULL

08H

D 40H02H E NULL08H

40H

TAIL

Page 91: Linked List

Set the variable HEAD to the address of the second node in the list, node “D”.Note that the address of node “D” is contained in the right pointer field of the HEAD node, node “B”

HEAD

02H

B 08HNULL

08H

D 40H02H E NULL08H

40H

TAIL

Doubly-Linked List after Reassigning HEAD

Page 92: Linked List

Set the left pointer field of the new HEAD node, node “D”, to NULL

HEAD

08H

D 40HNULL E NULL08H

40H

TAIL

Doubly-Linked List after Setting The Left Pointer Field of Node “D”

Page 93: Linked List

Set the right pointer field of node “B” to the address of node “E”.Note that the address of node “B” is contained in the left pointer field of node “D” and the address of node “E” is contained in the right pointer field of node “D”.

HEAD

02H

B 40HNULL

08H

D 40H02H

E NULL08H

40H

TAIL

Doubly-Linked List Setting The Right Pointer Field of Node “B”

Page 94: Linked List

Set the left pointer field of node “E” to the address of node “B”

HEAD

02H

B 40HNULL E NULL02H

40H

TAIL

Doubly-Linked List after Setting The Left Pointer Field of Node “E”

Page 95: Linked List

A circular list is formed by making use of variables which would otherwise be null. The last element of the list is made the predecessor of the first element; the first element, the successor of the last.

The upshot is that e no longer need both a head and tail variable to keep track of the list.

Even if only a single variable is used, both the first and the last list elements can be found in constant time.

Page 96: Linked List

02H

May 08HNULL

08H

June 40H02H July NULL08H

40H

Page 97: Linked List