1 / 58

DATA STRUCTURE & ALGORITHMS

DATA STRUCTURE & ALGORITHMS. CHAPTER 2: LINKED LIST. What is linked list?. A method of organizing stored data in a computers’ memory or on storage medium based on the logical order of the data

jania
Download Presentation

DATA STRUCTURE & ALGORITHMS

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. DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST

  2. What is linked list? • A method of organizing stored data in a computers’ memory or on storage medium based on the logical order of the data • All stored data records are assigned a physical address in memory that that the computer uses to locate the information • A linked list arranges the data by logic rather than by physical address

  3. What is linked list? • 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. Data fields Links Node

  4. What is linked list? • The last node has a reference to null • The entry point into a linked list is called the head of the list

  5. What is linked list? Table 1

  6. What is linked list? • In the Table 1, each data record is assigned a memory address • The first field holds the physical memory address of the record and the last field holds the physical memory address of the next logical record • The list is said linked because each record is linked to the next based on the last field

  7. What is linked list? • What happen if new record is added? Assumed with a numerical ID of “2222”. • When this record added to the list, the list change reflect the new linking logic, based on numerical ID • The “Next Name” field changes for ID 1111 to accommodate the added record with ID 2222 as in Table 2

  8. What is linked list? Table 2

  9. What is linked list? • In the Table 3, the same data is organized numerically by the ID number • The linked list still connects each record to the next using the “Next Name” field.

  10. What is linked list? Table 3

  11. Linked List vs Array

  12. Types of linked list? • Singly linked list • Doubly linked list • Circularly linked list

  13. Singly linked list • The simplest kind of linked list, which has one link 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. • Divided into two parts: the first part holds or points to information about the node, and second part holds the address of next node. • Travels one way.

  14. Doubly linked list • Two-way linked list. • Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.

  15. Circularly linked list • The first and final nodes are linked together. • To traverse a circular linked list, you begin at any node and follow the list in either direction until you return to the original node. • Circularly-linked lists can be seen as having no beginning or end. • This type of list is most useful for managing buffers for data ingest, and in cases where you have one object in a list and wish to iterate through all other objects in the list in no particular order. • The pointer pointing to the whole list may be called the access pointer.

  16. Declaration of linked list

  17. Declarations for Linked Lists • For this presentation, nodes in a linked list are objects, as shown here. 15 data_field class node { public: typedef double value_type; ... private value_typedata_field; node *link_field; }; link_field 10 data_field 7 data_field link_field null link_field

  18. Declarations for Linked Lists • The data_field of each node is a type called value_type, defined by a typedef. 15 data_field class node { public: typedefintvalue_type; ... private value_typedata_field; node *link_field; }; link_field 10 data_field 7 data_field link_field null link_field

  19. Declarations for Linked Lists • Each node also contains a link_field which is a pointer to another node. 15 data_field class node { public: typedefintvalue_type; ... private value_typedata_field; node *link_field; }; link_field 10 data_field 7 data_field link_field null link_field

  20. Declarations for Linked Lists • A program can keep track of the front node by using a pointer variable such as head_ptr in this example. • Notice that head_ptr is not a node -- it is a pointer to a node. 15 data_field link_field 10 data_field 7 data_field link_field null head_ptr link_field

  21. Declarations for Linked Lists • A program can keep track of the front node by using a pointer variable such as head_ptr. • Notice that head_ptr is not a node -- it is a pointer to a node. • We represent the empty list by storing nullin the head pointer. null head_ptr

  22. void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front We want to add a new entry, 13, to the front of the linked list shown here. 15 10 7 13 null entry head_ptr

  23. void list_head_insert(node*& head_ptr, const node::value_type& entry); 13 entry Inserting a Node at the Front • Create a new node, pointed to by a local variable insert_ptr. insert_ptr 15 10 7 null head_ptr

  24. void list_head_insert(node*& head_ptr, const node::value_type& entry); 13 entry Inserting a Node at the Front insert_ptr = new node; insert_ptr 15 10 7 null head_ptr

  25. void list_head_insert(node*& head_ptr, const node::value_type& entry); • insert_ptr = new node; • Place the data in the new node's data_field. Inserting a Node at the Front insert_ptr 13 15 10 7 13 null entry head_ptr

  26. void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front • insert_ptr = new node; • Place the data in the new node's data_field. • Connect the new node to the front of the list. insert_ptr 13 15 10 7 13 null entry head_ptr

  27. void list_head_insert(node*& head_ptr, const node::value_type& entry); • insert_ptr = new node(entry, head_ptr); Inserting a Node at the Front The correct new node can be completely created in one step by calling an appropriate node constructor. insert_ptr 13 15 10 7 13 null entry head_ptr

  28. void list_head_insert(node*& head_ptr, const node::value_type& entry); • insert_ptr = new node(entry, head_ptr); • Make the old head pointer point to the new node. Inserting a Node at the Front insert_ptr 13 15 10 7 13 null entry head_ptr

  29. void list_head_insert(node*& head_ptr, const node::value_type& entry); • insert_ptr = new node(entry, head_ptr); • head_ptr = insert_ptr; Inserting a Node at the Front insert_ptr 13 15 10 7 13 null entry head_ptr

  30. void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front • insert_ptr = new node(entry, head_ptr); • head_ptr = insert_ptr; 13 When the function returns, the linked list has a new node at the front. 15 10 7 null head_ptr

  31. Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; }

  32. Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } Does the function work correctly for the empty list ?

  33. Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } Does the function work correctly for the empty list ? 13 null entry head_ptr

  34. Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } insert_ptr 13 13 null entry null head_ptr

  35. Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } insert_ptr 13 13 entry null head_ptr

  36. Inserting a Node at the Front void list_head_insert(node*& head_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, head_ptr); head_ptr = insert_ptr; } When the function returns, the linked list has one node. 13 null head_ptr

  37. Pseudocode for Inserting Nodes • Nodes are often inserted at places other than the front of a linked list. • There is a general pseudocode that you can follow for any insertion function. . .

  38. list_head_insert(head_ptr, entry); Pseudocode for Inserting Nodes Determine whether the new node will be the first node in the linked list. If so, then there is only one step:

  39. list_head_insert(head_ptr, entry); The function we already wrote Pseudocode for Inserting Nodes • Determine whether the new node will be the first node in the linked list. If so, then there is only one step:

  40. list_head_insert(head_ptr, entry); Pseudocode for Inserting Nodes • Determine whether the new node will be the first node in the linked list. If so, then there is only one step: A pointer to the head of the list

  41. list_head_insert(head_ptr, entry); The data to put in the new node Pseudocode for Inserting Nodes • Determine whether the new node will be the first node in the linked list. If so, then there is only one step:

  42. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. Pseudocode for Inserting Nodes

  43. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. In this example, the new node will be the second node Pseudocode for Inserting Nodes 10 previous_ptr 15 7 null head_ptr

  44. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position Look at the pointer which is in the node *previous_ptr Pseudocode for Inserting Nodes What is the name of this orange pointer ? 10 previous_ptr 15 7 null head_ptr

  45. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position This pointer is called previous_ptr->link_field (although this name may be private to the node) Pseudocode for Inserting Nodes What is the name of this orange pointer ? 10 previous_ptr 15 7 null head_ptr

  46. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position previous_ptr->link_field points to the head of a small linked list, with 10 and 7 Pseudocode for Inserting Nodes 10 previous_ptr 15 7 null head_ptr

  47. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. The new node must be inserted at the front of this small linked list. Pseudocode for Inserting Nodes Write one C++ statement which will do the insertion. 13 10 previous_ptr 15 7 null head_ptr

  48. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. list_head_insert(previous_ptr->link_field, entry); Pseudocode for Inserting Nodes What might cause this statement to fail to compile? 13 10 previous_ptr 15 7 null head_ptr

  49. Otherwise (if the new node will not be first): • Start by setting a pointer named previous_ptr to point to the node which is just before the new node's position. list_head_insert(previous_ptr->link( ), entry); Pseudocode for Inserting Nodes Use a node member function to get the link field if needed. 13 10 previous_ptr 15 7 null head_ptr

  50. list_head_insert(head_ptr, entry); list_head_insert(previous_ptr->link( ), entry); Pseudocode for Inserting Nodes • Determine whether the new node will be the first node in the linked list. If so, then there is only one step: • Otherwise (if the new node will not be first): • Set a pointer named previous_ptr to point to the node which is just before the new node's position. • Make the function call:

More Related