590 likes | 786 Views
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
E N D
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 • 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
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
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
What is linked list? Table 1
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
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
What is linked list? Table 2
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.
What is linked list? Table 3
Types of linked list? • Singly linked list • Doubly linked list • Circularly linked list
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.
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.
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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; }
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 ?
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
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
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
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
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. . .
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:
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:
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
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:
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
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
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
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
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
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
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
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
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: