70 likes | 208 Views
Data Structures CSCI 132, Spring 2019 Lecture 21 Doubly Linked Lists. Doubly Linked Lists. Doubly linked lists have pointers to both the next node and the previous node in the list. The data members for this class would be: int count; mutable int current_position;
E N D
Data StructuresCSCI 132, Spring 2019Lecture 21Doubly Linked Lists
Doubly Linked Lists Doubly linked lists have pointers to both the next node and the previous node in the list. The data members for this class would be: int count; mutable int current_position; mutable Node<List_entry> *current; entry 0 entry 1 entry 2 current
Nodes for doubly linked lists template <class Node_entry> struct Node { //Data members Node_entry entry; Node<Node_entry> *next; Node<Node_entry> *back; //Constructors Node(); Node(Node_entry, Node<Node_entry> *link_back = NULL Node<Node_entry> *link_next = NULL); };
set_position( ) for doubly linked lists 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 fill this out in class. }
Advantages of linked lists Advantages: Disadvantages:
Linked vs. Contiguous lists Use contiguous lists when: Use linked lists when:
Linked Lists in Arrays Store items in one array. Store "pointers" in a second, parallel array. Each "pointer" is the index of the next item in the list.