90 likes | 108 Views
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();
E N D
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(); Node(Node_entry item, Node<Node_entry> *link = NULL); };
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> ©); void operator = (const List<List_entry> ©); protected: // data members for a linked list implementation int count; Node<List_entry> *head; Node<List_entry> *set_position(int position) const; };
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; }
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 }
Execution times for linked list List, empty, full, size-- clear, retrieve, replace, insert, remove-- Traverse--
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.
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 }