DATA STRUCTURE ALGORITHMS CHAPTER 2: LINKED LIST

Embed Size (px)

DESCRIPTION

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 fieldsLinks Node

Citation preview

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 fieldsLinks 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? Address of record IDNamePhone No.Next Name Aziz Johan Suraya Malik eof 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? Address of record IDNamePhone No.Next Name Aziz Jenab Johan Suraya Malik eof 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? Address of record IDNamePhone No.Next Name Aziz Jenab Johan Malik Suraya eof Table 3 Linked List vs Array AdvantagesDisadvantages Linked listThe number of nodes can grow and shrink on demand Not allowed direct access to the individual elements Uses more memory to store a reference to the next node ArraySimple codingCannot be easily extended or reduced to fit the data set Less memoryExpensive to maintain new insertions & deletion 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. Declaration of linked list Declarations for Linked Lists For this presentation, nodes in a linked list are objects, as shown here. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef double value_type;... private value_type data_field; node *link_field; }; Declarations for Linked Lists The data_field of each node is a type called value_type, defined by a typedef. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef int value_type;... private value_type data_field; node *link_field; }; Declarations for Linked Lists Each node also contains a link_field which is a pointer to another node. data_field link_field 10 data_field link_field 15 data_field link_field 7 null class node { public: typedef int value_type;... private value_type data_field; node *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. head_ptr data_field link_field 10 data_field link_field 15 data_field link_field 7 null 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 null in the head pointer. head_ptr null 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 null head_ptr entry 13 Inserting a Node at the Front Create a new node, pointed to by a local variable insert_ptr null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front insert_ptr = new node; null head_ptr entry 13 insert_ptr void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node; Place the data in the new node's data_field. void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 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. Connect the new node to the front of the list. Inserting a Node at the Front The correct new node can be completely created in one step by calling an appropriate node constructor null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); Make the old head pointer point to the new node. void list_head_insert(node*& head_ptr, const node::value_type& entry); Inserting a Node at the Front null head_ptr entry 13 insert_ptr 13 insert_ptr = new node(entry, head_ptr); head_ptr = insert_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; null head_ptr 13 When the function returns, the linked list has a new node at the front. void list_head_insert(node*& head_ptr, const node::value_type& entry); 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 ? head_ptr entry 13 null Inserting a Node at the Front Does the function work correctly for the empty list ? 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 head_ptr entry 13 null insert_ptr 13 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; } null Inserting a Node at the Front head_ptr entry 13 insert_ptr 13 null 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 head_ptr 13 null 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. 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... 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: The function we already wrote 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); A pointer to the head of the list 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 data to put in the new node 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. Pseudocode for Inserting Nodes 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. In this example, the new node will be the second node previous_ptr Pseudocode for Inserting Nodes What is the name of this orange pointer ? 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 previous_ptr Pseudocode for Inserting Nodes What is the name of this orange pointer ? 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) previous_ptr Pseudocode for Inserting Nodes 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 previous_ptr Pseudocode for Inserting Nodes Write one C++ statement which will do the insertion 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. 13 previous_ptr Pseudocode for Inserting Nodes What might cause this statement to fail to compile? 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. 13 previous_ptr list_head_insert(previous_ptr->link_field, entry); Pseudocode for Inserting Nodes Use a node member function to get the link field if needed 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. 13 previous_ptr 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: list_head_insert(head_ptr, entry); 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: list_head_insert(previous_ptr->link( ), entry); Pseudocode for Removing Nodes Nodes often need to be removed from a linked list. As with insertion, there is a technique for removing a node from the front of a list, and a technique for removing a node from elsewhere. Well look at the pseudocode for removing a node from the front of a linked list. Removing the Head Node null head_ptr 13 Start by setting up a temporary pointer named remove_ptr to the head node. remove_ptr Removing the Head Node null head_ptr 13 Set up. Set up remove_ptr. head_ptr = remove_ptr->link( ); remove_ptr Draw the change that this statement will make to the linked list. Removing the Head Node null head_ptr 13 Set up. Set up remove_ptr. head_ptr = remove_ptr->link( ); remove_ptr Removing the Head Node Set up. Set up remove_ptr. head_ptr = remove_ptr->link( ); delete remove_ptr; // Return the node's memory to heap null head_ptr 13 remove_ptr Removing the Head Node Heres what the linked list looks like after the removal finishes null head_ptr Summary It is easy to insert a node at the front of a list. The linked list toolkit also provides a function for inserting a new node elsewhere It is easy to remove a node at the front of a list. The linked list toolkit also provides a function for removing a node elsewhere--you should read about this function and the other functions of the toolkit. References Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch.