1 / 79

The Linked List ADT

The Linked List ADT. CS 1037a --Topic 11. Related materials. from Main and Savitch “Data Structures & other objects using C++”. Sec. 5.1, 5.2: Nodes and the Linked-List toolkit (our approach is somewhat different) Sec. 5.5: Dynamic Arrays vs. Linked-List

una
Télécharger la présentation

The Linked List ADT

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. The Linked List ADT CS 1037a --Topic 11

  2. Related materials from Main and Savitch “Data Structures & other objects using C++” • Sec. 5.1, 5.2: Nodes and the Linked-List toolkit (our approach is somewhat different) • Sec. 5.5: Dynamic Arrays vs. Linked-List • Sec. 6.3: Iterators from CS1037 labs • Lab8

  3. Linked List • Consists of items that are linked to each other • Each item on the list is associated with a reference (pointer) that indicates where the next item is found • A dynamic data structure: grows and shrinks as necessary at runtime • There are no unused/empty locations

  4. Singly Linked List • Simplest form of linked list • Linked list object holds a pointer to the first node in the list • Each node object consists of the data object being stored, and a pointer to the next node in the list

  5. Singly Linked List Linked List object head size 3 Node objects . Data objects can be simple data (int, double, etc) or pointers

  6. Nodes in Singly Linked Lists • Nodes for our linked lists will be objects dynamically created or deleted in the HEAP • Each Node object in a singly linked list will contain two member variables: Item value Node* next

  7. Class Node can be conveniently declared within the scope of the class LinkedList template <class Item> class LinkedList { private: class Node { public: Node(Item v, Node * n=NULL) : value(v), next(n) { } Item value; Node * next; }; Node * m_head; unsigned intm_size; // and other members of class LinkedList,…. later in the notes

  8. Class Node can be conveniently declared within the scope of the class LinkedList template <class Item> class LinkedList { private: class Node { .... };// privately declared class Node cannot be used outside of scope of LinkedList Node * m_head; .... } Note how name Node is used for declarations within the scope of class LinkedList NOTE: If class Node was declared in the public part of LinkedList, then it would be possible to use objects of this class outside the scope of class LinkedList as follows: #include “LinkedList.h” ... LinkedList<int> :: Node a; LinkedList<int> :: Node * b = &a; ...

  9. Singly Linked List • To find the nth item in a linked list: • Follow the head pointer to the first node • Find the reference to the next node • Follow it to the second node • Find the reference to the next node • Follow it to the third node • Etc, until nth node is reached • The nth item is the data item in this node

  10. Singly Linked List Variation Linked List object head size tail tail reference makes it easier to add to the end of the linked list 3 .

  11. To Add an Item to the Front of a Linked List Build the new node, and put the new data item in it, make it point an the current front node head size tail 3 .

  12. To Add an Item to the Front of a Linked List Reset head so that it points at the new node, and increment size of the linked list head size tail 4 .

  13. To Add an Item to the End of a Linked List head size tail Build the new node, and put the new data item in it 3 . . null reference

  14. To Add an Item to the End of a Linked List head size tail Make the current tail node point to the new node 3 .

  15. To Add an Item to the End of a Linked List Reset tail so that it points to the new node, and increment the list size head size tail 4

  16. To Add an Item to an Empty Linked List Build the new node, and put the new data item in it . . head size tail 0 .

  17. To Add an Item to an Empty Linked List Make bothhead and tail point at the new node, and increment the list’s size head size tail 1 .

  18. To Add an Item Before the Tail Build the new node, and put the new data item in it head size tail 3 Make the new node point at the tail node .

  19. To Add an Item Before the Tail Beginning at head, traverse the list until we reach the node that points at the tail node head size tail 3 1 2

  20. To Add an Item Before the Tail Make this node point at the new node, and increment the list’s size head size tail 4

  21. To Add an Item Before the Tail head size tail This is the resulting sequence of linked Nodes 4 .

  22. To Remove Front Item From a Linked List Save the value of pointer head, so that we can delete the first node later head size tail 4 .

  23. To Remove Front Item From a Linked List Save a copy of the data item in the first node head size tail 4 . (Copy of data item from first node)

  24. To Remove Front Item From a Linked List Replace the value in head with a reference to the second node on the list head size tail 4 .

  25. To Remove Front Item From a Linked List Get rid of the (original) first node, decrement the size of the list, and return the reference to the removed item head size tail 3 .

  26. To Remove the Only Item From a Linked List Save a reference to the node, and a copy of the data item found in the node head size tail 1 . (Copy of data item from the node)

  27. To Remove the Only Item From a Linked List Set head and tail to NULL, and decrement the size of the list . . head size tail 0 .

  28. To Remove the Only Item From a Linked List Get rid of the node, and return the data item . . head size tail 0 .

  29. To Remove Last Item From a Linked List Save the Item value stored in the tail node, so that we will be able to return it head size tail 4 . (Copy of data item from the node being removed)

  30. To Remove Last Item From a Linked List Beginning at head, traverse the list until we reach the node that points at the tail node head size tail 4 1 2 3 .

  31. To Remove Last Item From a Linked List Set the link reference in this node to NULL Delete the tail node head size tail 4 .

  32. To Remove Last Item From a Linked List Update pointer tail Update counter size head size tail 3 .

  33. Implementing Other Data Structures Using Linked Lists • We could rewrite the our array-based List implementation with one based on a linked list • Some operations would be faster because array contents would not have to be shifted: eg: adding or removing the first item • Other operations would be slower because we cannot access items, other than the first or last, directly: retrieving or replacing these items requires a traversal of the nodes

  34. Implementing Other Data Structures Using Linked Lists • Node in a linked list requires more memory than a location in a list-based array (4 extra bytes for pointer to next node) • But, an array may contain many unused (but allocated) memory locations; the number of nodes in a linked list is always equal to the number of items currently being stored

  35. A Singly Linked List ADT • Our first linked list implementation will be designed for use in applications that do not need to modify the middle of the list – all insertions and deletions will take place at the head or tail • A good way to implement Stacks; later, we’ll also see that linked lists are good for implementing Queues

  36. Possible Linked List Operations more on next slide…

  37. Possible Linked List Operations

  38. UML Diagram for LinkedList ADT Member variables are protected (#) rather than private (-) so that we can extend this class later

  39. LinkedList.h template <class Item> class LinkedList { private: class Node { public: Node( Item val, Node * nx=NULL ) : value(val), next(nx) { } Item value; Node * next; }; public: LinkedList( ); // construct an empty linked list ~LinkedList( ); // destroy a linked list unsigned int size( ) const; // return number of items in the list boolisEmpty( )const; // true if list is empty, false otherwise // cont’d..

  40. LinkedList.h void addHead( Item item ); // add itemto front of list Item removeHead( ); // remove front node, and return the data value found in it; // precondition: list must not be empty already Item retrieveHead( ) const; // return data value in front node; precondition: list must not // be empty void addTail( Item item ); // add itemto end of list Item removeTail( ); // remove last node, and return the data value found in it; // precondition: list must not be empty already // cont’d..

  41. LinkedList.h Item retrieveTail( ) const; // return data value in last node; precondition: list must not // be empty void add( unsigned int n, Item item ); // Add a new node containing itemin position non the list; // precondition:nmust be a valid list position Item replace( unsigned int n, Item item ); // Replace the data value in position non the list with item, and // return the replaced item; precondition:nmust be a valid list // position cont’d..

  42. LinkedList.h Item remove( unsigned int n ); // Remove the node at position nfrom the list, and return the data // value it contained; precondition: nmust be a valid list // position Item retrieve( unsigned int n ) const; // Return the data reference from the node in position n; // precondition: nmust be a valid list position // cont’d..

  43. LinkedList.h private: Node * m_head; Node * m_tail; unsigned intm_size; }; #include “LinkedList.template” // end of LinkedList class

  44. LinkedList.template template <class Item> LinkedList<Item> :: LinkedList( ) : m_head (NULL), m_tail (NULL), m_size (0) { } template <class Item> LinkedList<Item> :: ~LinkedList( ) { Node * here = m_head, * nextNode; while ( here != NULL ) { nextNode = here->next; delete here; here = nextNode; } } // cont’d..

  45. LinkedList.template template <class Item> unsigned int LinkedList<Item> :: size( ) const { return m_size; } template <class Item> bool LinkedList<Item> :: isEmpty( ) const { return ( m_size == 0 ); } // cont’d..

  46. LinkedList.template template <class Item> void LinkedList<Item> :: addHead( Item item ) { m_head = new Node( item, m_head ); if (m_tail == NULL) m_tail = m_head; m_size++; } // cont’d..

  47. LinkedList.template template <class item> Item LinkedList<Item> :: removeHead( ) { // precondition: list is not already empty Node * oldNode = m_head; Item returnVal = m_head->value; m_head = m_head->next; if (m_head == NULL) m_tail = NULL; m_size--; delete oldNode; return returnVal; } // cont’d..

  48. LinkedList.template template <class Item> Item LinkedList<Item> :: retrieveHead( ) const { // precondition: list is not already empty return m_head->value; } template <class Item> void LinkedList<Item> :: addTail( Item item ) { if ( isEmpty( ) ) addHead( item ); else { m_tail= m_tail->next = new Node( item ); m_size++; } } // cont’d..

  49. LinkedList.template template <class Item> Item LinkedList<Item> :: removeTail( ) { // precondition: list is not already empty if ( m_head == m_tail ) return removeHead( ); Node * preTail = m_head; Item returnVal = m_tail->value; while ( preTail->next != m_tail ) preTail= preTail->next; preTail->next = NULL ; // cont’d..

  50. LinkedList.template delete m_tail; m_tail = preTail; m_size--; return returnVal; } template <class Item> Item LinkedList<Item> :: retrieveTail( ) const { // precondition: list is not already empty return m_tail->value; } // cont’d..

More Related