1 / 97

Linked List

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.

milton
Télécharger la présentation

Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. Linked List vs. Conventional Array • 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.

  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

  5. Implementation • 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

  6. HISTORY • 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. Types of Linked LIst • Singly – Linked Lists • Doubly – Linked Lists

  12. Singly – Linked Lists • 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

  13. Data Pointer Figure 1: Node Data field Pointer field Head First node in the list Tail Last node in the list Successor Next node in the list

  14. Goldilocks Papa Bear Mama Bear Baby Bear 05H 03H 20H Null HEAD 01H 05H 03H 20H Figure 2: Singly Linked List with 4 Nodes

  15. There is no limit to the size of a singly-linked list. Adding a nodes to a linked list is simply a matter of: • Creating a new node • Setting the data field of the new node to the value to be inserted into the list. • Assigning the address of the new node to the pointer field of the current last node in the list. • Setting the pointer field of the new node to NULL.

  16. Finding the Length of the List/Reading the List from Left to Right • 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.

  17. 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

  18. Read_List(Head) { If(Head = NULL) then { Print “The list is empty” Exit } Set Num_Nodes to 1 Set 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_Nodes Print Next_Node  Node.Data Set Next_Node to Next_Node  Node.Pointer } Print “” Print “The number of nodes in the list is” Num_Nodes }

  19. To simulate the Read_List pseudocode. HEAD 05H 09H 02H 04H James of Zebedee John Peter Andrew Bartholomew Matthew James Thaddeus Simon Judas Thomas Philip 21H 06H 09H 25H 18H 02H 31H 27H 04H NULL 22H 15H 06H 21H 31H 27H 15H 18H 22H 25H Figure 3. Singly-linked list with 12 nodes

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

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

  22. While Loop: 9th Iteration Num_Nodes = 10 Print “Andrew” Next_Node = address of “Thomas” (22H) While Loop: 10th Iteration Num_Nodes = 11 Print “Thomas” Next_Node = address of “Judas” (25H) While Loop: 11th Iteration Num_Nodes = 12 Print “Judas” Next_Node = NULL Print “” Print “The number of nodes in the list is 12”

  23. SUMMARY of OUTPUT Philip James of Zebedee John Peter Bartholomew Matthew James Thaddeus Simon Andrew Thomas Judas The number of nodes in the list is 12

  24. Retrieving the ith Node In A Singly-Linked List • To retrieve a specific element in a singly linked list, we would have to traverse the list until we reach desired node.

  25. 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  Node.Pointer }

  26. 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

  27. To simulate the Retrieve_Node algorithm, let us try to retrieve the 3rd node in the list presented in Figure 3. I = 1 Next_Node = address of “Philip” (05H) While Loop: 1st Iteration I = 2 Next_Node = address of “James of Zebedee” (09H) While Loop: 2nd Iteration I = 3 Next_Node = address of “John” (02H) /* Else Clause: Next_Node is NOT NULL */ Print “John”

  28. Storing A New Value Into the ith Node of A Singly-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.

  29. 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  Node.Pointer }

  30. 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

  31. 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 = 1 Next_Node = address of “Philip” (05H) While Loop: 1st Iteration I = 2 Next_Node = address of “James of Zebedee” (09H) While Loop: 2nd Iteration I = 3 Next_Node = address of “John” (02H) While Loop: 3rd Iteration I = 4 Next_Node = address of “Peter” (04H) /*Else Clause: Next Node is NOT NULL */ Data Field of node 4 = “The Rock”

  32. HEAD 05H 09H 02H 04H James of Zebedee John The Rock Andrew Bartholomew Matthew Thaddeus Simon Judas Thomas Philip James 18H 21H 06H 09H 25H 02H 31H 27H 04H NULL 22H 15H 06H 21H 31H 27H 15H 18H 22H 25H Figure 6. Singly-linked After Replacing “Peter” with “The Rock”

  33. Inserting A Node In A Singly-Linked List • Create a new node for the element. • Set the data field of the new node to the value to be inserted. • Insert the node. Three locations in which a node may be inserted: • Insert the node at the start of the list (i = 1) • Insert the node at the end of the list (i > length of the list) • Insert the node at position i of the list (i < length of the list)

  34. Inserting A Node At The Start of A Singly-Linked List (i = 1) Daimos Mazinger Z Voltron 10H 05H NULL • 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 20H 10H 05H HEAD Figure 7. Singly-linked With 3 nodes

  35. The first two steps in our general procedure require us to create a new node for the element and set the data field to “Voltes V” : Voltes V Daimos Mazinger Z Voltron Voltes V 05H NULL 10H 20H 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 30H 20H 10H 05H Figure 9. Singly-Linked list after inserting “Voltes V” HEAD

  36. In order to reference the start of the list we must now set the variable HEAD to the address of the newly created “Voltes V” node. Daimos Voltes V Voltron Mazinger Z 10H 05H 20H NULL Set HEAD to Address of New Node The Final linked list will look like the diagram below HEAD 30H 20H 10H 05H Figure 10. Singly-Linked list after Reassigning HEAD

  37. Inserting A Node At The End of A Singly-Linked List (i > Length of 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

  38. The Resulting linked list Daimos Mazinger Z Voltron Voltes V 10H 05H 30H NULL HEAD 30H 20H 10H 05H Figure 11. Singly-Linked list after Inserting “Voltes V” At The End

  39. Inserting A Node At Position i(i < Length of 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.

  40. 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”. Daimos Mazinger Z Voltron Voltes V 10H 05H NULL 05H Set Address of New Node Node.Pointer to Address of “Mazinger Z”  Node.Pointer 30H HEAD 20H 10H 05H Figure 12. Singly-Linked list after Setting the Pointer Field of “Voltes V”

  41. 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”. Daimos Mazinger Z Voltron Voltes V 10H 30H NULL 05H Set Address of “Mazinger Z”  Node.Pointer to Address of sNew Node HEAD 20H 10H 30H 05H Figure 13. Singly-Linked list after Inserting “Voltes V”At Position 3

  42. Insertions in a Singly-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

  43. Insert_Node(Head, I, New_Value) { Create New Node Set 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

  44. While ((Ctr < 1) AND (Next_Node is not NULL)) /* Locate i –1 */ { Increment Ctr Set 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 } }

  45. Voltes V Create node for “Voltes V” Address of “Voltes V”  Node.Data = “Voltes V” Result of Statements 30H /* First IF Statement */ Address of “Voltes V”  Node.Pointer = Address of “Daimos” Head = Address of “Voltes V” 1. Insert the node “Voltes V” at the start of the list Input the Insert_Node: (Address of “Daimos”, 1, “Voltes V”)

  46. Voltes V Create node for “Voltes V” Address of “Voltes V”  Node.Data = “Voltes V” Result of Statements 30H 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” 2. Insert the node “Voltes V” at the end of the list Input the Insert_Node: (Address of “Daimos”, 9999, “Voltes V”)

  47. 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 Mazinger Z Voltron Voltes V 10H 05H 30H NULL HEAD 20H 10H 05H 30H Result of Statements

  48. Insertion Using the TAIL 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.

  49. Adding a new node will then simply consist of the following steps • 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

  50. Insert “Voltes V” at the end of the list by following the steps. • Create a new node • Set the data field of the new node to the value to be inserted Set 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

More Related