70 likes | 218 Views
This lecture introduces doubly linked lists, a critical data structure in computer science that allows for efficient insertion and deletion of nodes. Each node contains pointers to both its next and previous nodes, enabling bidirectional traversal. We discuss the implementation details, including the structure of nodes and the functions necessary for managing the list, such as setting positions. Additionally, we compare the advantages and disadvantages of linked lists versus contiguous lists, providing insights into when to use each type of structure.
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.