1 / 9

Data Structures CSCI 132, Spring19 Lecture 20 Linked Lists

Data Structures CSCI 132, Spring19 Lecture 20 Linked Lists. Contiguous implementation of lists. Disadvantages: Advantages:. Nodes (with templates). template <class Node_entry> struct Node { //Data members Node_entry entry; Node<Node_entry> *next; //Constructors Node();

elloyd
Download Presentation

Data Structures CSCI 132, Spring19 Lecture 20 Linked Lists

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 StructuresCSCI 132, Spring19Lecture 20Linked Lists

  2. Contiguous implementation of lists Disadvantages: Advantages:

  3. Nodes (with templates) template <class Node_entry> struct Node { //Data members Node_entry entry; Node<Node_entry> *next; //Constructors Node(); Node(Node_entry item, Node<Node_entry> *link = NULL); };

  4. Linked Lists (with Templates) template <class List_entry> class List { public: List( ); //All the list methods from List ADT ~List( ); List(const List<List_entry> &copy); void operator = (const List<List_entry> &copy); protected: // data members for a linked list implementation int count; Node<List_entry> *head; Node<List_entry> *set_position(int position) const; };

  5. Finding a List position template <class List_entry> Node<List_entry> *List<List_entry> :: set_position(int position) const { Node<List_entry> *q = head; //we will fill this out in class return q; }

  6. Inserting a Node template <class List_entry> Error_code List<List_entry> :: insert(int position, const List_entry &x) { if ((position < 0) || (position > count)) return range_error; Node<List_entry> *new_node, *previous, *following; // we will fill this out in class }

  7. Execution times for linked list List, empty, full, size-- clear, retrieve, replace, insert, remove-- Traverse--

  8. Gaining efficiency for linked lists Some efficiency can be gained by keeping track of the most recently accessed member of the list. This means the functions don't have to start from the beginning of the list to access sequential items. To do this, we add two new data members to the List class: mutable int current_position; mutable Node<List_entry> *current; The keyword, mutable, allows these data members to be changed, even by functions that are declared as const.

  9. A new set_position template <class List_entry> void List<List_entry> :: set_position(int position) const // Set current to point to node at position specified in parameter { //We will work this out in class }

More Related