Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.

Similar presentations


Presentation on theme: "1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists."— Presentation transcript:

1 1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists

2 2 Contiguous implementation of lists Disadvantages: Static amount of memory allocated--may waste memory or cause overflows. It takes time to insert or delete from beginning or middle of the list, because you must shift list elements. Advantages: Can easily access items anywhere in the list (random access).

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

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

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

6 6 Finding a List position template Node *List :: set_position(int position) const { Node *q = head; for (int i =0; i < position; i++ ) { q = q -> next; } return q; }

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

8 Inserting a Node template Error_code List :: insert(int position, const List_entry &x) { if ((position count)) return range_error; Node *new_node, *previous, *following; if (position > 0) { previous = set_position(position -1); following = previous -> next; } else following = head; new_node = new Node (x, following); if (new_node == NULL ) return overflow; if (position == 0) head = new_node; else previous -> next = new_node; count ++; return success; }

9 9 Execution times for linked list List, empty, full, size-- All these have the same execution time independent of the size of the list. They have constant running time. clear, retrieve, replace, insert, remove-- All of these must traverse list to find desired position. For example, on the average, insert must traverse n/2 items for a list of length n. So, the running time of each of these functions is proportional to the length of the list (n). Traverse-- The running time of traverse depends on the time needed to execute the parameter function. But, since traverse must visit each of the n entries in the list, it can never be better than a running time that is proportional to the length of the list (n).

10 10 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 *current; The keyword, mutable, allows these data members to be changed, even by functions that are declared as const.

11 11 A new set_position template void List :: set_position(int position) const // Set current to point to node at position specified in parameter { //We will work this out in class if (position < current_position) { current_position = 0; current = head; } for ( ; current_position != position; current_position++ ) { current = curent->next; } }


Download ppt "1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists."

Similar presentations


Ads by Google